From 3b7994a4a934644bb03f4c957d50aebd264866dc Mon Sep 17 00:00:00 2001 From: louiscooper136 Date: Tue, 14 May 2024 19:55:42 -0400 Subject: [PATCH] Finished Poem Server --- poem_server/app/views.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/poem_server/app/views.py b/poem_server/app/views.py index e5a1d0d..f45e430 100644 --- a/poem_server/app/views.py +++ b/poem_server/app/views.py @@ -13,7 +13,7 @@ def get_about_line(params): line = Line.objects.filter(clean_text_contains=params['topic']).random() return line.to_dict() except Line.DoesNotExist: - raise NotFound("Line not found") + raise BadRequest("Line not found") @route_get('lines/rhyme', args={'word' : str}) def get_rhyme_line(params): @@ -21,7 +21,7 @@ def get_rhyme_line(params): rhyme = Rhyme.get_rhyme_for_word(params['word']).lines.exclude(clean_text__endswidth=" "+params['word']).first() 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): @@ -29,9 +29,9 @@ def get_random_couplet(params): line = Line.objects.random() for rl in line.rhyming_lines().sample(1): rhyme = rl - return {'lines' : [line, rhyme]} - except Rhyme.DoesNotExist: - raise NotFound("Rhyme Not Found") + return {'lines' : [line.text, rhyme.text]} + except Line.DoesNotExist: + raise BadRequest("Rhyme Not Found") @route_get('couplets/about', args={'topic' : str}) def get_about_couplet(params): @@ -39,9 +39,9 @@ def get_about_couplet(params): line = Line.objects.filter(clean_text__contains=params['topics']).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): @@ -50,7 +50,10 @@ def get_rhyme_couplet(params): #line = Line. for rl in line.rhyming_lines().sample(1): rhyme = rl - return {lines : [lines.text, rhyme.text]} - except: - pass + return {'lines': [rhyme.text, toRhyme.text], 'word': params['word']} + except Rhyme.DoesNotExist: + raise BadRequest("Did not fnid a couplet with a Rhyme") + except Exception as e: + if isinstance(e, BadRequest): + raise BadRequest(f"Couldnt figure out how to pronounce {params['word']}")