Checkpoint 3

This commit is contained in:
Thomas Naber 2024-05-05 13:54:11 -04:00
parent e021bd1bab
commit ad0f4ee655
3 changed files with 59 additions and 0 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

BIN
poem_server/.DS_Store vendored Normal file

Binary file not shown.

View File

@ -6,3 +6,62 @@ 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")