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().clean_text} @route_get('lines/about', args={'topic': str}) def get_line_about_topic(params): try: line = Line.objects.filter(clean_text__contains=params['topic']).random().clean_text return {'lines' : line, 'topic': params['topic']} except Line.DoesNotExist: raise NotFound("No lines with that topic") @route_get('lines/rhyme', args={'rhyme': str}) def get_line_with_rhyme(params): try: line = Rhyme.get_rhyme_for_word(params['rhyme']).lines.exclude(clean_text__endswith=params['rhyme']).random().clean_text return {'lines' : line, 'rhyme': params['rhyme']} except Line.DoesNotExist: raise NotFound("No lines that rhyme") @route_get('couplets/random', args={}) def get_random_couplet(params): flag = True while flag: line = Line.objects.random() if len(line.rhyming_lines()) > 0: flag = False rhyming_line = line.rhyming_lines().random().clean_text return {'couplet' : [line.clean_text, rhyming_line]} @route_get('couplets/about', args={'topic' : str}) def get_couplet_about_topic(params): flag = True while flag: try: line = Line.objects.filter(clean_text__contains=params['topic']).random() except Line.DoesNotExist: raise NotFound("No lines with that topic") if len(line.rhyming_lines()) > 0: flag = False rhyming_line = line.rhyming_lines().random().clean_text return {'couplet' : [line.clean_text, 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 len(line.rhyming_lines()) > 0: flag = False rhyming_line = line.rhyming_lines().random().clean_text return {'couplet' : [line.clean_text, rhyming_line], 'rhyme' : params['rhyme']} @route_get('sonnet/random', args={}) def get_random_sonnet(params): flag = True while flag: line1 = Line.objects.random() if len(line1.rhyming_lines()) > 0: flag = False rhyming_line1 = line1.rhyming_lines().random().clean_text couplet1 = [line1.clean_text, rhyming_line1] flag = True while flag: line2 = Line.objects.random() if len(line2.rhyming_lines()) > 0: flag = False rhyming_line2 = line2.rhyming_lines().random().clean_text couplet2 = [line2.clean_text, rhyming_line2] flag = True while flag: line3 = Line.objects.random() if len(line3.rhyming_lines()) > 0: flag = False rhyming_line3 = line3.rhyming_lines().random().clean_text couplet3 = [line3.clean_text, rhyming_line3] flag = True while flag: line4 = Line.objects.random() if len(line4.rhyming_lines()) > 0: flag = False rhyming_line4 = line4.rhyming_lines().random().clean_text couplet4 = [line4.clean_text, rhyming_line4] flag = True while flag: line5 = Line.objects.random() if len(line5.rhyming_lines()) > 0: flag = False rhyming_line5 = line5.rhyming_lines().random().clean_text couplet5 = [line5.clean_text, rhyming_line5] flag = True while flag: line6 = Line.objects.random() if len(line6.rhyming_lines()) > 0: flag = False rhyming_line6 = line6.rhyming_lines().random().clean_text couplet6 = [line6.clean_text, rhyming_line6] flag = True while flag: line7 = Line.objects.random() if len(line7.rhyming_lines()) > 0: flag = False rhyming_line7 = line7.rhyming_lines().random().clean_text couplet7 = [line7.clean_text, rhyming_line7] return {'sonnet':[couplet1[0], couplet2[0], couplet1[1], couplet2[1], couplet3[0], couplet4[0], couplet3[1], couplet4[1], couplet5[0], couplet6[0], couplet5[1], couplet6[1], couplet7[0], couplet7[1] ]}