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:
parent
4bf0c7e260
commit
368dfd4a83
@ -23,18 +23,9 @@ class FactCategory(models.Model):
|
|||||||
"""String representation."""
|
"""String representation."""
|
||||||
return "{0:s}".format(self.name)
|
return "{0:s}".format(self.name)
|
||||||
|
|
||||||
|
def random_fact(self, regex=None):
|
||||||
class FactManager(models.Manager):
|
"""Get a random fact in this category."""
|
||||||
"""Queries against Fact."""
|
facts = self.fact_set.all()
|
||||||
|
|
||||||
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)
|
|
||||||
if regex:
|
if regex:
|
||||||
facts = facts.filter(fact__iregex=regex)
|
facts = facts.filter(fact__iregex=regex)
|
||||||
|
|
||||||
@ -44,6 +35,19 @@ class FactManager(models.Manager):
|
|||||||
return None
|
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):
|
class Fact(models.Model):
|
||||||
"""Define facts."""
|
"""Define facts."""
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user