Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Created an ```EntryManager``` class that provides helper methods for …

…filtering querysets.

 * Tags with zero associated posts are now filtered out of the {% show_tags %} templatetag.
 * Made source code more consistent.


git-svn-id: file:///Users/brian/Backups/S3/backups/Wednesday/brian/svn/djog/trunk@81 95ab7de8-677b-4562-a266-b8acc9dd993c
  • Loading branch information...
commit 5c523824c0b08116d471e68ff3ebdb8c11f4e371 1 parent 83e7a70
@brosner authored
View
15 djog/managers.py
@@ -0,0 +1,15 @@
+
+from django.db import models
+
+class EntryManager(models.Manager):
+ def of_type(self, type_):
+ """
+ Returns a queryset filtering the entries to the given type.
+ """
+ return self.filter(entry_type=type_)
+
+ def in_month(self, month):
+ """
+ Returns a queryset filtering the entries to the given month.
+ """
+ return self.filter(pub_date__month=month)
View
13 djog/models.py
@@ -9,6 +9,8 @@
from django.contrib.sites.models import Site
from django.contrib.sites.managers import CurrentSiteManager
+from djog.managers import EntryManager
+
class Blog(models.Model):
site = models.ForeignKey(Site)
title = models.CharField(_('Title'), max_length=100)
@@ -45,6 +47,8 @@ class Entry(models.Model):
help_text=_('Select what type of Entry this is, currently there are regular blog posts, and pages(such as an About page, or a contact page).'),
radio_admin=True, default=TYPE_POST)
+ objects = EntryManager()
+
def __unicode__(self):
return self.title
@@ -90,8 +94,13 @@ def __unicode__(self):
class Admin:
pass
- def num_stories(self):
- return Entry.objects.filter(tags__tag=unicode(self)).count()
+ def _get_num_stories(self):
+ if not hasattr(self, "_num_stories"):
+ self._num_stories = Entry.objects.filter(
+ tags__tag=unicode(self)
+ ).count()
+ return self._num_stories
+ num_stories = property(_get_num_stories)
def get_absolute_url(self):
return urlresolvers.reverse('djog_entries_by_tag',
View
12 djog/sites.py
@@ -26,7 +26,7 @@ def add_feed(self, url, feed_class):
def archive_index(self, request, **kwargs):
defaults = dict(
- queryset = self.get_blog().entry_set.filter(entry_type=Entry.TYPE_POST),
+ queryset = self.get_blog().entry_set.of_type(Entry.TYPE_POST),
date_field = 'pub_date',
)
defaults.update(kwargs)
@@ -34,7 +34,7 @@ def archive_index(self, request, **kwargs):
def archive_year(self, request, **kwargs):
defaults = dict(
- queryset = self.get_blog().entry_set.filter(entry_type=Entry.TYPE_POST),
+ queryset = self.get_blog().entry_set.of_type(Entry.TYPE_POST),
date_field = 'pub_date',
template_name = 'djog/archive_year.html',
)
@@ -43,7 +43,7 @@ def archive_year(self, request, **kwargs):
def archive_month(self, request, **kwargs):
defaults = dict(
- queryset = self.get_blog().entry_set.filter(entry_type=Entry.TYPE_POST),
+ queryset = self.get_blog().entry_set.of_type(Entry.TYPE_POST),
date_field = 'pub_date',
template_name = 'djog/archive_month.html',
)
@@ -52,7 +52,7 @@ def archive_month(self, request, **kwargs):
def archive_day(self, request, **kwargs):
defaults = dict(
- queryset = self.get_blog().entry_set.filter(entry_type=Entry.TYPE_POST),
+ queryset = self.get_blog().entry_set.of_type(Entry.TYPE_POST),
date_field = 'pub_date',
template_name = 'djog/archive_day.html',
)
@@ -61,7 +61,7 @@ def archive_day(self, request, **kwargs):
def post(self, request, **kwargs):
defaults = dict(
- queryset = self.get_blog().entry_set.filter(entry_type=Entry.TYPE_POST),
+ queryset = self.get_blog().entry_set.of_type(Entry.TYPE_POST),
date_field = 'pub_date',
)
defaults.update(kwargs)
@@ -82,7 +82,7 @@ def post(self, request, **kwargs):
def page(self, request, **kwargs):
defaults = dict(
- queryset = self.get_blog().entry_set.filter(entry_type=Entry.TYPE_PAGE),
+ queryset = self.get_blog().entry_set.of_type(Entry.TYPE_PAGE),
slug_field = 'slug',
template_name = 'djog/entry_detail.html',
)
View
30 djog/templatetags/djog_utils.py
@@ -9,24 +9,26 @@ def show_entry(entry):
register.inclusion_tag("entry.html")(show_entry)
def show_tags():
- tags = Tag.objects.all().order_by('tag')
- return {'tags': tags}
-register.inclusion_tag('tags.html')(show_tags)
+ tags = Tag.objects.all().order_by("tag")
+ return {"tags": [tag for tag in tags if tag.num_stories > 0]}
+register.inclusion_tag("tags.html")(show_tags)
def show_archive():
- months = Entry.objects.filter(entry_type=Entry.TYPE_POST).dates('pub_date', "month")
- counts = []
- for month in months:
- count = Entry.objects.filter(entry_type=Entry.TYPE_POST, pub_date__month=month.month).count()
- counts.append((month, count))
- return {'counts': counts}
-register.inclusion_tag('monthly.html')(show_archive)
+ entries = Entry.objects.of_type(Entry.TYPE_POST)
+ dates = []
+ for date in entries._clone().dates("pub_date", "month"):
+ dates.append(
+ (date, (entries & Entry.objects.in_month(date.month)).count())
+ )
+ # TODO: rename counts to dates. counts is not very descriptive.
+ return {"counts": dates}
+register.inclusion_tag("monthly.html")(show_archive)
def show_pages():
- Pages = Entry.objects.filter(entry_type=Entry.TYPE_PAGE)
- return {'pages': Pages}
-register.inclusion_tag('pages.html')(show_pages)
+ pages = Entry.objects.of_type(Entry.TYPE_PAGE)
+ return {"pages": pages}
+register.inclusion_tag("pages.html")(show_pages)
def searchify(term):
return "search/%s" % term
-register.filter('searchify', searchify)
+register.filter("searchify", searchify)
Please sign in to comment.
Something went wrong with that request. Please try again.