diff --git a/poem_server/app/views.py b/poem_server/app/views.py index 16ccdce..d46fde5 100644 --- a/poem_server/app/views.py +++ b/poem_server/app/views.py @@ -5,12 +5,12 @@ from random import choice, sample @route_get('lines/random', args={}) def get_random_line(params): - return {'line': Line.objects.random()} + 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() + 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") @@ -18,22 +18,22 @@ def get_line_about_topic(params): @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() + 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={}) +@route_get('couplets/random', args={}) def get_random_couplet(params): flag = True while flag: line = Line.objects.random() - if line.rhyming_lines().count() > 0: + if len(line.rhyming_lines()) > 0: flag = False - rhyming_line = line.rhyming_lines().random() - return {'couplet' : [line, rhyming_line]} + rhyming_line = line.rhyming_lines().random().clean_text + return {'couplet' : [line.clean_text, rhyming_line]} -@route_get('/couplets/about', args={'topic' : str}) +@route_get('couplets/about', args={'topic' : str}) def get_couplet_about_topic(params): flag = True while flag: @@ -41,12 +41,12 @@ def get_couplet_about_topic(params): line = Line.objects.filter(clean_text__contains=params['topic']).random() except Line.DoesNotExist: raise NotFound("No lines with that topic") - if line.rhyming_lines().count() > 0: + if len(line.rhyming_lines()) > 0: flag = False - rhyming_line = line.rhyming_lines().random() - return {'couplet' : [line, rhyming_line], 'topic': params['topic']} + 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}) +@route_get('couplets/rhyme', args={'rhyme' : str}) def get_couplet_with_rhyme(params): flag = True while flag: @@ -54,11 +54,78 @@ def get_couplet_with_rhyme(params): 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 line.rhyming_lines().count() > 0: + if len(line.rhyming_lines()) > 0: flag = False - rhyming_line = line.rhyming_lines().random() - return {'couplet' : [line, rhyming_line], 'rhyme' : params['rhyme']} + rhyming_line = line.rhyming_lines().random().clean_text + return {'couplet' : [line.clean_text, rhyming_line], 'rhyme' : params['rhyme']} -@route_get('poems/random', args={}) -def get_random_line(params): - return {'poem': Poem.objects.random()} +@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] + ]}