From 368dfd4a8327bb88ed7758875a861f29c01aee0d Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Sat, 11 Feb 2017 09:22:32 -0600 Subject: [PATCH] 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 --- facts/models.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/facts/models.py b/facts/models.py index 5a70e21..63b1225 100644 --- a/facts/models.py +++ b/facts/models.py @@ -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."""