initial commit
This commit is contained in:
51
poem_server/app/models.py
Normal file
51
poem_server/app/models.py
Normal file
@@ -0,0 +1,51 @@
|
||||
from banjo.models import Model, StringField, IntegerField, ForeignKey
|
||||
from banjo.http import BadRequest
|
||||
import pronouncing
|
||||
import re
|
||||
|
||||
class Line(Model):
|
||||
text = StringField()
|
||||
clean_text = StringField()
|
||||
line_number = IntegerField()
|
||||
poem = ForeignKey("Poem", related_name="lines")
|
||||
rhyme = ForeignKey("Rhyme", related_name="lines", null=True)
|
||||
|
||||
def __str__(self):
|
||||
"The string representation of a line is just its text"
|
||||
return self.text
|
||||
|
||||
"Returns lowercase text without any punctuation"
|
||||
return re.sub('[^a-z ]', '', self.text.lower())
|
||||
|
||||
def last_word(self):
|
||||
"Gets the last word from a line, lower case and without punctuation."
|
||||
parts = self.clean_text.split()
|
||||
if parts:
|
||||
return parts[-1]
|
||||
|
||||
def rhyming_lines(self):
|
||||
if self.rhyme:
|
||||
return self.rhyme.lines.exclude(clean_text__endswith=self.last_word())
|
||||
else:
|
||||
return []
|
||||
|
||||
class Poem(Model):
|
||||
gutenberg_id = IntegerField(unique=True)
|
||||
|
||||
def __str__(self):
|
||||
"The string representation of a poem is all its lines, in order"
|
||||
return '\n'.join(line.text for line in self.lines.order_by('line_number'))
|
||||
|
||||
class Rhyme(Model):
|
||||
phones = StringField()
|
||||
|
||||
@classmethod
|
||||
def get_rhyme_for_word(self, word):
|
||||
phones = pronouncing.phones_for_word(word)
|
||||
if not phones:
|
||||
raise BadRequest(f"Couldn't figure out how to pronounce {word}")
|
||||
rhyming_phones = pronouncing.rhyming_part(phones[0])
|
||||
try:
|
||||
return Rhyme.objects.get(phones=rhyming_phones)
|
||||
except Rhyme.DoesNotExist:
|
||||
raise BadRequest(f"Sorry, no lines rhyme with {word}")
|
8
poem_server/app/views.py
Normal file
8
poem_server/app/views.py
Normal file
@@ -0,0 +1,8 @@
|
||||
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}
|
Reference in New Issue
Block a user