from banjo.urls import route_get, route_post from banjo.http import BadRequest, NotFound from app.models import Line, Poem, Rhyme from random import choice, sample @route_get('lines/random', args={}) def get_random_line(params): return {'line': Line.objects.random().text} @route_get('lines/about', args={'topic': str}) def get_lines_about(params): try: return {'line': Line.objects.filter(clean_text__contains=params['topic']).random().text ,"topic": params['topic'] } except Line.DoesNotExist: raise NotFound("No lines with that topic") @route_get('lines/rhyme', args={'word': str}) def get_lines_that_rhyme(params): try: return{'line': Rhyme.get_rhyme_for_word(params['word']).lines.exclude(clean_text__endswith=params['word']).random().text, "word": params['word']} except Line.DoesNotExist: raise NotFound("No lines rhyme with that") @route_get('couplets/random', args={}) def get_random_couplet(params): try: lineOne = Line.objects.random() lineTwo = lineOne.rhyming_lines().random() return {'lines': [lineOne.text, lineTwo.text]} except Line.DoesNotExist: raise NotFound("No line rhymed with the random first one") @route_get('couplets/about', args={'topic':str}) def get_couplet_about(params): try: lineOne = Line.objects.filter(clean_text__contains=params['topic']).random() lineTwo = lineOne.rhyming_lines().random() return {'lines': [lineOne.text, lineTwo.text], "topic": params['topic']} except Line.DoesNotExist: raise NotFound("No lines with that topic or No line rhymed with the random first one") @route_get('couplets/rhyme', args={'word': str}) def get_couplet_rhymes_with(params): try: lineOne = Rhyme.get_rhyme_for_word(params['word']).lines.exclude(clean_text__endswith=params['word']).random() lineTwo = lineOne.rhyming_lines().random() return {'lines': [lineOne.text, lineTwo.text], "word": params['word']} except Line.DoesNotExist: raise NotFound("Zero or One lines rhymed with your word. Not enough for a couplet!") @route_get('couplets/start', args={'word': str}) def couplets_start_with(params): try: lineOne = Line.objects.filter(clean_text__startswith=params['word']).random() lineTwo = lineOne.rhyming_lines().random() return{'lines': [lineOne.text, lineTwo.text], "word": params['word']} except Line.DoesNotExist: raise NotFound("No lines start with that word or rhyme with one that does")