Luke Plant avatar Luke Plant committed 039d148

Used experimental 'prefetch_related' Django API to reduce query count on lots of pages

With tests for the many camp list page

Comments (0)

Files changed (5)

cciw/cciwmain/common.py

     if _thisyear is None or _thisyear_timestamp is None \
         or (datetime.datetime.now() - _thisyear_timestamp).seconds > 3600:
         from cciw.cciwmain.models import Camp
-        lastcamp = Camp.objects.order_by('-end_date')[0]
+        lastcamp = Camp.objects.prefetch_related().order_by('-end_date')[0]
         if lastcamp.is_past():
             _thisyear = lastcamp.year + 1
         else:

cciw/cciwmain/fixtures/htmlchunks.json

         }, 
         "model": "sitecontent.htmlchunk", 
         "pk": "home_page"
+    },
+    {
+        "fields": {
+            "menu_link": null,
+            "html": ""
+        },
+        "model": "sitecontent.htmlchunk",
+        "pk": "camp_dates_intro_text"
+    },
+    {
+        "fields": {
+            "menu_link": null,
+            "html": ""
+        },
+        "model": "sitecontent.htmlchunk",
+        "pk": "camp_dates_outro_text"
     }
 ]

cciw/cciwmain/models.py

 class CampManager(models.Manager):
     use_for_related_fields = True
     def get_query_set(self):
-        return super(CampManager, self).get_query_set().select_related('chaplain')
+        return super(CampManager, self).get_query_set().select_related('chaplain').prefetch_related('leaders')
 
     def get_by_natural_key(self, year, number):
         return self.get(year=year, number=number)
         unique_together = (('year', 'number'),)
 
 
-
-
 import cciw.cciwmain.hooks

cciw/cciwmain/tests/__init__.py

+from cciw.cciwmain.tests.camps import *
 from cciw.cciwmain.tests.members import *
 from cciw.cciwmain.tests.forums import *
 from cciw.cciwmain.tests.news import *

cciw/cciwmain/tests/camps.py

+from datetime import date
+
+from django.core.urlresolvers import reverse
+from django.test import TestCase
+
+from cciw.cciwmain.common import get_thisyear
+from cciw.cciwmain.models import Camp, Site, Person
+from cciw.cciwmain.tests.utils import init_query_caches, FuzzyInt
+from cciw.sitecontent.models import HtmlChunk
+
+
+class ThisyearPage(TestCase):
+
+    fixtures = ['basic.json', 'htmlchunks.json']
+
+    def test_get(self):
+        init_query_caches()
+        y = get_thisyear()
+        site = Site.objects.get(id=1)
+
+        for i in range(1, 20):
+            c = Camp.objects.create(year=y, number=i, site=site,
+                                    minimum_age=11,
+                                    maximum_age=17,
+                                    start_date=date(y, 6, 1),
+                                    end_date=date(y, 6, 8))
+            p = Person.objects.create(name="Leader %s" % i)
+            c.leaders.add(p)
+
+        with self.assertNumQueries(FuzzyInt(1, 6)):
+            resp = self.client.get(reverse('cciw.cciwmain.views.camps.thisyear'))
+
+        for c in Camp.objects.filter(year=y):
+            self.assertContains(resp, c.get_absolute_url())
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.