facts: refactor random_fact onto FactCategory

since the views will have a FactCategory, not a Fact, we'll move the
convenience random_fact() there and use it in the FactManager

bss/dr.botzo#15
This commit is contained in:
Brian S. Stephan 2017-02-11 09:22:32 -06:00
parent 4bf0c7e260
commit 368dfd4a83
1 changed files with 16 additions and 12 deletions

View File

@ -23,18 +23,9 @@ class FactCategory(models.Model):
"""String representation."""
return "{0:s}".format(self.name)
class FactManager(models.Manager):
"""Queries against Fact."""
def random_fact(self, category, regex=None):
"""Get a random fact from the database."""
try:
fact_category = FactCategory.objects.get(name=category)
except FactCategory.DoesNotExist:
return None
facts = Fact.objects.filter(category=fact_category)
def random_fact(self, regex=None):
"""Get a random fact in this category."""
facts = self.fact_set.all()
if regex:
facts = facts.filter(fact__iregex=regex)
@ -44,6 +35,19 @@ class FactManager(models.Manager):
return None
class FactManager(models.Manager):
"""Queries against Fact."""
def random_fact(self, category, regex=None):
"""Get a random fact from the database."""
try:
factcategory = FactCategory.objects.get(name=category)
except FactCategory.DoesNotExist:
return None
return factcategory.random_fact(regex)
class Fact(models.Model):
"""Define facts."""