changeset 192:341759e1cda1

Implementing #67: use a denormalized count field on download categories to reduce database queries.
author Brian Neal <bgneal@gmail.com>
date Sat, 03 Apr 2010 01:10:00 +0000
parents 046e6ef0ff45
children fa7d82bfb100
files gpp/downloads/__init__.py gpp/downloads/admin.py gpp/downloads/models.py gpp/templates/downloads/index.html
diffstat 4 files changed, 23 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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
--- 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'))
--- 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 %}
 <dt>
 <a href="{% url downloads-category category=category.pk,sort="title",page=1 %}">{{ category.title }}</a>
-({{ category.num_downloads }})
+({{ category.count }})
 </dt>
 <dd><p>{{ category.description }}</p></dd>
 {% endfor %}