More work on the routes for poem server.

This commit is contained in:
Danielle Tear 2024-05-01 18:33:39 -04:00
parent 2440383984
commit 22780b46a8
1 changed files with 25 additions and 11 deletions

View File

@ -5,26 +5,40 @@ from random import choice, sample
@route_get('lines/random', args={})
def get_random_line(params):
return {'line': Line.objects.random().text}
return {'line': Line.objects.random()}
@route_get('lines/about', args={'topic': str})
def get_line_about_topic(params):
try:
lines = Line.objects.filter(clean_text__contains=params['topic'])
if len(lines) == 1:
return {'line' : lines[0], 'topic': params['topic']}
else:
return {'lines' : lines, 'topic': params['topic']}
line = Line.objects.filter(clean_text__contains=params['topic']).random()
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_about_topic(params):
try:
lines = Rhyme.get_rhyme_for_word(params['rhyme']).lines.exclude(clean_text__endswith=params['rhyme'])
if len(lines) == 1:
return {'line' : lines[0], 'rhyme': params['rhyme']}
else:
return {'lines' : lines, 'rhyme': params['rhyme']}
line = Rhyme.get_rhyme_for_word(params['rhyme']).lines.exclude(clean_text__endswith=params['rhyme']).random()
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 line.rhyming_lines().count() > 0:
flag = False
rhyming_line = line.rhyming_lines().random()
return {'couple' : line + '\n' + rhyming_line}
@route_get('/couplets/about', args={'topic' : str})
def get_couplet_about_topic(params):
flag = True
while flag:
line = Line.objects.random()
if line.rhyming_lines().count() > 0:
flag = False
rhyming_line = line.rhyming_lines().random()
return {'couplet' : [line, rhyming_line]}