From afcd25178e563fcf1615bdc0bfed6c1bfb056ccc Mon Sep 17 00:00:00 2001 From: Danielle Tear Date: Wed, 1 May 2024 19:18:38 -0400 Subject: [PATCH] Updated the routes and finished the ones specified in the requirements. --- poem_server/app/views.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/poem_server/app/views.py b/poem_server/app/views.py index 8729d72..80b5a84 100644 --- a/poem_server/app/views.py +++ b/poem_server/app/views.py @@ -16,7 +16,7 @@ def get_line_about_topic(params): raise NotFound("No lines with that topic") @route_get('lines/rhyme', args={'rhyme': str}) -def get_line_about_topic(params): +def get_line_with_rhyme(params): try: line = Rhyme.get_rhyme_for_word(params['rhyme']).lines.exclude(clean_text__endswith=params['rhyme']).random() return {'lines' : line, 'rhyme': params['rhyme']} @@ -31,14 +31,30 @@ def get_random_couplet(params): if line.rhyming_lines().count() > 0: flag = False rhyming_line = line.rhyming_lines().random() - return {'couple' : line + '\n' + rhyming_line} + return {'couplet' : [line, rhyming_line]} @route_get('/couplets/about', args={'topic' : str}) def get_couplet_about_topic(params): flag = True while flag: - line = Line.objects.random() + try: + line = Line.objects.filter(clean_text__contains=params['topic']).random() + except Line.DoesNotExist: + raise NotFound("No lines with that topic") if line.rhyming_lines().count() > 0: flag = False rhyming_line = line.rhyming_lines().random() - return {'couplet' : [line, rhyming_line]} + return {'couplet' : [line, rhyming_line], 'topic': params['topic']} + +@route_get('/couplets/rhyme', args={'rhyme' : str}) +def get_couplet_with_rhyme(params): + flag = True + while flag: + try: + line = Rhyme.get_rhyme_for_word(params['rhyme']).lines.exclude(clean_text__endswith=params['rhyme']).random() + except Line.DoesNotExist: + raise NotFound("No lines with that rhyme") + if line.rhyming_lines().count() > 0: + flag = False + rhyming_line = line.rhyming_lines().random() + return {'couplet' : [line, rhyming_line], 'rhyme' : params['rhyme']} \ No newline at end of file