# HG changeset patch # User Brian Neal # Date 1270257000 0 # Node ID 341759e1cda1038d6fd5ac1d9e07a3a75db7b3ed # Parent 046e6ef0ff4542a0aff5145652a546d28a438ab6 Implementing #67: use a denormalized count field on download categories to reduce database queries. diff -r 046e6ef0ff45 -r 341759e1cda1 gpp/downloads/__init__.py --- a/gpp/downloads/__init__.py Thu Apr 01 02:01:33 2010 +0000 +++ b/gpp/downloads/__init__.py Sat Apr 03 01:10:00 2010 +0000 @@ -0,0 +1,1 @@ +import signals diff -r 046e6ef0ff45 -r 341759e1cda1 gpp/downloads/admin.py --- a/gpp/downloads/admin.py Thu Apr 01 02:01:33 2010 +0000 +++ b/gpp/downloads/admin.py Sat Apr 03 01:10:00 2010 +0000 @@ -9,27 +9,32 @@ from downloads.models import AllowedExtension from downloads.models import VoteRecord + +class CategoryAdmin(admin.ModelAdmin): + list_display = ('title', 'description', 'count') + readonly_fields = ('count', ) + + class DownloadAdmin(admin.ModelAdmin): - exclude = ('html', ) - list_display = ('title', 'user', 'category', 'date_added', 'ip_address', - 'hits', 'average_score', 'size', 'is_public') - list_filter = ('date_added', 'is_public', 'category', 'user', 'ip_address') - date_hierarchy = 'date_added' - ordering = ('-date_added', ) - search_fields = ('title', 'description', 'user__username') - raw_id_fields = ('user', ) - save_on_top = True + exclude = ('html', ) + list_display = ('title', 'user', 'category', 'date_added', 'ip_address', + 'hits', 'average_score', 'size', 'is_public') + list_filter = ('date_added', 'is_public', 'category', 'user', 'ip_address') + date_hierarchy = 'date_added' + ordering = ('-date_added', ) + search_fields = ('title', 'description', 'user__username') + raw_id_fields = ('user', ) + save_on_top = True class VoteRecordAdmin(admin.ModelAdmin): - list_display = ('user', 'download', 'vote_date') - list_filter = ('user', 'download') - date_hierarchy = 'vote_date' + list_display = ('user', 'download', 'vote_date') + list_filter = ('user', 'download') + date_hierarchy = 'vote_date' admin.site.register(Download, DownloadAdmin) -admin.site.register(Category) +admin.site.register(Category, CategoryAdmin) admin.site.register(AllowedExtension) admin.site.register(VoteRecord, VoteRecordAdmin) -# vim: ts=4 sw=4 diff -r 046e6ef0ff45 -r 341759e1cda1 gpp/downloads/models.py --- a/gpp/downloads/models.py Thu Apr 01 02:01:33 2010 +0000 +++ b/gpp/downloads/models.py Sat Apr 03 01:10:00 2010 +0000 @@ -15,6 +15,7 @@ """Downloads belong to categories.""" title = models.CharField(max_length=64) description = models.TextField(blank=True) + count = models.IntegerField(default=0, blank=True) class Meta: verbose_name_plural = 'Categories' @@ -23,9 +24,6 @@ def __unicode__(self): return self.title - def num_downloads(self): - return Download.public_objects.filter(category=self.pk).count() - def download_path(instance, filename): """ @@ -114,7 +112,7 @@ vote_date = models.DateTimeField(auto_now_add=True) def __unicode__(self): - return "%s voted on '%s' on %s" % ( + return u"%s voted on '%s' on %s" % ( self.user.username, self.download.title, self.vote_date.strftime('%b %d, %Y %H:%M:%S')) diff -r 046e6ef0ff45 -r 341759e1cda1 gpp/templates/downloads/index.html --- a/gpp/templates/downloads/index.html Thu Apr 01 02:01:33 2010 +0000 +++ b/gpp/templates/downloads/index.html Sat Apr 03 01:10:00 2010 +0000 @@ -14,7 +14,7 @@ {% for category in categories %}
{{ category.title }} -({{ category.num_downloads }}) +({{ category.count }})

{{ category.description }}

{% endfor %}