generated from mwc/lab_server
49 lines
1.8 KiB
Python
49 lines
1.8 KiB
Python
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_topic(params):
|
|
topic = params['topic']
|
|
return {'line': Line.objects.filter(clean_text__contains=topic).random().text}
|
|
|
|
@route_get('lines/rhyme', args={'word' : str})
|
|
def get_rhyme(params):
|
|
rhyme = params['word']
|
|
return {'line': Rhyme.get_rhyme_for_word(rhyme).lines.exclude(clean_text__endswith=rhyme).random().text}
|
|
|
|
@route_get('couplets/random', args={})
|
|
def get_couplet(params):
|
|
line = Line.objects.random()
|
|
rl = line.rhyming_lines().random()
|
|
return {"lines": [line.text, rl.text]}
|
|
|
|
@route_get('couplets/about', args={'topic' : str})
|
|
def get_couplet_topic(params):
|
|
topic = params['topic']
|
|
line = Line.objects.filter(clean_text__contains=topic).random()
|
|
rl = line.rhyming_lines()
|
|
if rl.count() == 0:
|
|
raise NotFound("Couplet not found")
|
|
else:
|
|
rl = rl.random()
|
|
return {"lines": [line.text, rl.text]}
|
|
|
|
@route_get('couplets/rhyme', args={'word' : str})
|
|
def get_couplet_rhyme(params):
|
|
rhyme = params['word']
|
|
line_objects = Rhyme.get_rhyme_for_word(rhyme).lines.exclude(clean_text__endswith=rhyme).sample(2)
|
|
lines = [line_object.text for line_object in line_objects]
|
|
return {"lines": lines}
|
|
|
|
@route_get('triplets/rhyme', args={'word' : str})
|
|
def get_triplet_rhyme(params):
|
|
rhyme = params['word']
|
|
line_objects = Rhyme.get_rhyme_for_word(rhyme).lines.exclude(clean_text__endswith=rhyme).sample(3)
|
|
lines = [line_object.text for line_object in line_objects]
|
|
return {"lines": lines} |