diff --git a/poem_server/app/views.py b/poem_server/app/views.py index 897e544..21bc171 100644 --- a/poem_server/app/views.py +++ b/poem_server/app/views.py @@ -9,26 +9,39 @@ def get_random_line(params): @route_get('lines/about',args={'topic': str}) def get_random_line_about(params): - return {'line': Line.objects.filter(clean_text__contains=params['topic']).random().text} + try: + return {'line': Line.objects.filter(clean_text__contains=params['topic']).random().text} + except: + raise BadRequest(f"Error 400: Sorry, there are no lines containing {params['topic']}.") @route_get('lines/rhyme',args={'rhyme': str}) def get_random_line_that_rhymes_with(params): - return {'line': Rhyme.get_rhyme_for_word(params['rhyme']).lines.exclude(clean_text__endswith=params['rhyme']).random().text} + try: + return {'line': Rhyme.get_rhyme_for_word(params['rhyme']).lines.exclude(clean_text__endswith=params['rhyme']).random().text} + except: + raise BadRequest(f'Error 400: Sorry, there are no lines that rhyme with {params['topic']}.') @route_get('couplets/random',args={}) def get_random_couplet(params): - first_line=Line.objects.random() - second_line=first_line.rhyming_lines().random() - return {'lines': [first_line.text,second_line.text]} + first_line=Line.objects.random() + second_line=first_line.rhyming_lines().random() + return {'lines': [first_line.text,second_line.text]} @route_get('couplets/about',args={'topic': str}) def get_random_couplet_about(params): - first_line=Line.objects.filter(clean_text__contains=params['topic']).random() - second_line=first_line.rhyming_lines().random() - return {'lines': [first_line.text,second_line.text]} + try: + first_line=Line.objects.filter(clean_text__contains=params['topic']).random() + second_line=first_line.rhyming_lines().random() + return {'lines': [first_line.text,second_line.text]} + except: + raise BadRequest(f'Error 400: Sorry, there are not lines that contain the word {params['topic']}.') @route_get('couplets/rhyme',args={'rhyme': str}) def get_random_couplet_that_rhymes_with(params): - first_line = Rhyme.get_rhyme_for_word(params['rhyme']).lines.exclude(clean_text__endswith=params['rhyme']).random() - second_line = first_line.rhyming_lines().random() - return {'lines': [first_line.text,second_line.text]} + try: + first_line = Rhyme.get_rhyme_for_word(params['rhyme']).lines.exclude(clean_text__endswith=params['rhyme']).random() + second_line = first_line.rhyming_lines().random() + return {'lines': [first_line.text,second_line.text]} + except: + raise BadRequest(f'Error 400: Sorry, there are not at least two lines that rhyme with {params['topic']}.') +