From 8e9bdd0991a026af807ab91cce9f4ed97b0a54c4 Mon Sep 17 00:00:00 2001 From: Pat Wick Date: Fri, 3 May 2024 21:15:42 -0400 Subject: [PATCH] Finished lab_server routes in views.py, not except Still having issues handling exceptions to get the 400 error when a topic or rhyme is not found. Was able to finish the rhyming couplet though, so that's progress. --- poem_server/app/views.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/poem_server/app/views.py b/poem_server/app/views.py index 70c2a5a..0fd358e 100644 --- a/poem_server/app/views.py +++ b/poem_server/app/views.py @@ -11,17 +11,18 @@ def get_random_line(params): def get_about_line(params): try: line = Line.objects.filter(clean_text__contains=params['topic']).random() - return line.to_dict() - except: - raise NotFound("Line not found") + return {'line': line.text, 'topic': params['topic']} + except Line.DoesNotExist: + raise BadRequest("Line not found with that topic") @route_get('lines/rhyme', args={'word': str}) def get_rhyme_line(params): try: rhyme = Rhyme.get_rhyme_for_word(params['word']).lines.exclude(clean_text__endswith=" "+params['word']).first() + return {'line': rhyme.text, 'word': params['word']} return rhyme.to_dict() except Rhyme.DoesNotExist: - raise NotFound("Rhyme not found") + raise BadRequest("Rhyme not found") @route_get('couplets/random', args={}) def get_random_couplet(params): @@ -32,7 +33,7 @@ def get_random_couplet(params): #print(rhyme.text) return {'lines': [line.text, rhyme.text]} except Line.DoesNotExist: - raise NotFound("Rhyme not found") + raise BadRequest("Rhyme not found") @route_get('couplets/about', args={'topic': str}) def get_about_couplet(params): @@ -40,17 +41,20 @@ def get_about_couplet(params): line = Line.objects.filter(clean_text__contains=params['topic']).random() for rl in line.rhyming_lines().sample(1): rhyme = rl - return {'lines': [line.text, rhyme.text]} - except: - pass + return {'lines': [line.text, rhyme.text], 'topic': params['topic']} + except Line.DoesNotExist: + raise BadRequest("Couplet not found with given topic") @route_get('couplets/rhyme', args={'word': str}) def get_rhyme_couplet(params): try: toRhyme = Rhyme.get_rhyme_for_word(params['word']).lines.exclude(clean_text__endswith=" " + params['word']).first() - line = Line. - for rl in line.rhyming_lines().sample(1): + #line = Line. + for rl in toRhyme.rhyming_lines().sample(1): rhyme = rl - return {'lines': [line.text, rhyme.text]} - except: - pass \ No newline at end of file + return {'lines': [rhyme.text, toRhyme.text], 'word': params['word']} + except Rhyme.DoesNotExist: + raise BadRequest("Did not find a couplet that rhymes with given word") + except Exception as e: + if isinstance(e, BadRequest): + raise BadRequest(f"Couldn't figure out how to pronounce {params['word']}") \ No newline at end of file