generated from mwc/lab_server
Finished implementations of paths/routes.
This commit is contained in:
@@ -6,3 +6,76 @@ 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}) #done
|
||||
def get_line_topic(params):
|
||||
try:
|
||||
topic = params['topic']
|
||||
foundline = Line.objects.filter(clean_text__contains=topic).random().text
|
||||
return {'line':foundline,'topic':topic}
|
||||
except Line.DoesNotExist:
|
||||
raise NotFound("No line with this topic can be found.")
|
||||
|
||||
@route_get('lines/rhyme', args={'word':str}) #done
|
||||
def get_line_rhyme(params):
|
||||
try:
|
||||
word = params['word']
|
||||
foundline = Rhyme.get_rhyme_for_word(word).lines.exclude(clean_text__endswith=" "+word).random().text
|
||||
return {'line':foundline,'word':word}
|
||||
except Rhyme.DoesNotExist:
|
||||
raise NotFound("No line that rhymes with this word can be found.")
|
||||
|
||||
@route_get('couplets/random', args={}) #done
|
||||
def get_rhyming_couplet(params):
|
||||
line = Line.objects.random()
|
||||
if line.rhyming_lines().count() != 0:
|
||||
rhymedline = line.rhyming_lines().random().text
|
||||
couplet = [line.text,rhymedline]
|
||||
return {"line":couplet}
|
||||
else:
|
||||
return get_rhyming_couplet() #my idea here is that if the random line I pick has no lines that rhyme with it, the function will start over and pick a new random line
|
||||
|
||||
def pick_new_line_with_rhyme(word): #i added this helper function so I could do some recursion and avoid getting lines that don't have rhymes
|
||||
line = Line.objects.filter(clean_text__contains=word).random()
|
||||
if line.rhyming_lines() == []:
|
||||
line = pick_new_line_with_rhyme(word)
|
||||
return line
|
||||
|
||||
@route_get('couplets/about', args={'topic':str}) #done (sometimes will return a false NotFound error when there is in fact a line with that topic?)
|
||||
def get_rhyming_couplet_about(params):
|
||||
try:
|
||||
word = params['topic']
|
||||
line = Line.objects.filter(clean_text__contains=word).random()
|
||||
if line.rhyming_lines() == []:
|
||||
line = pick_new_line_with_rhyme(word)
|
||||
rhymedline = line.rhyming_lines().random().text
|
||||
couplet = [line.text,rhymedline]
|
||||
return {"line":couplet,"topic":word}
|
||||
except Line.DoesNotExist:
|
||||
raise NotFound("No line with that topic exists.")
|
||||
|
||||
@route_get('couplets/rhyme', args={'word':str}) #done
|
||||
def get_rhyming_couplet_for_word(params):
|
||||
word = params['word']
|
||||
line1 = Rhyme.get_rhyme_for_word(word).lines.exclude(clean_text__endswith=" "+word).random()
|
||||
line2 = line1.rhyming_lines().random()
|
||||
return {"line":[line1.text,line2.text],'word':word}
|
||||
|
||||
def find_line_with_lastword(): #helper function for recursion
|
||||
line1 = Line.objects.random()
|
||||
lastword = line1.last_word()
|
||||
try:
|
||||
line2 = Line.objects.filter(clean_text__contains=lastword).random()
|
||||
return [line1.text,line2.text],lastword
|
||||
except Line.DoesNotExist:
|
||||
return find_line_with_lastword()
|
||||
|
||||
#this will return a random couplet where the second line includes the last word of the first line
|
||||
@route_get('couplets/includes_lastword', args={}) #done
|
||||
def get_includes_lastword(params):
|
||||
newtuple = find_line_with_lastword()
|
||||
couplet = newtuple[0]
|
||||
lastword = newtuple[1]
|
||||
return {"lines":couplet, "lastword":lastword}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user