move URLItem lookup to models URLItem.search()
authorTero Marttila <terom@fixme.fi>
Sat, 07 Sep 2013 15:35:56 +0300
changeset 80 3aaac91a6654
parent 79 b125b3fcd52d
child 81 3a2fdc820c41
move URLItem lookup to models URLItem.search()
qrurls/models.py
qrurls/views.py
--- a/qrurls/models.py	Fri Sep 06 01:36:33 2013 +0300
+++ b/qrurls/models.py	Sat Sep 07 15:35:56 2013 +0300
@@ -1,5 +1,6 @@
 import datetime
 import hashlib
+import logging
 import os.path
 
 from django.db import models
@@ -9,6 +10,8 @@
 from django.contrib.sites.models import get_current_site
 from django.utils import timezone
 
+log = logging.getLogger('qrurls.models')
+
 QRCODE_API = 'https://chart.googleapis.com/chart?cht=qr&chs={width}x{height}&chl={url}&chld=H'
 IMAGES_MEDIA = 'qrurls-images'
 
@@ -179,6 +182,41 @@
         verbose_name_plural = u"URL Items"
         ordering = ['published']
 
+    @classmethod
+    def search (cls, shorturl=None, shorturl_id=None, item_id=None, related=()) :
+        """
+            Return the URLItem for a given shorturl, either the given specific one,
+            or the latest, from the database.
+
+            Raises URLItem.NotFound
+        """
+        # JOIN against shorturl, urlimage
+        url_item = cls.objects.select_related(*related)
+
+        if shorturl:
+            url_item = url_item.filter(shorturl__shorturl=shorturl)
+        elif shorturl_id:
+            shorturl_id = int(shorturl_id)
+            url_item = url_item.filter(shorturl__id=shorturl_id)
+        else:
+            raise cls.DoesNotExist()
+        
+        # match for published items
+        now = timezone.now()
+        url_item = url_item.filter(published__lt=now).order_by('-published')
+        
+        log.info("Search URLItem @ %s", now)
+       
+        if item_id :
+            # specific, but still the published on
+            return url_item.get(id=int(item_id)) # raises DoesNotExist
+        else :
+            # most recent
+            try:
+                return url_item[0]
+            except IndexError:
+                raise DoesNotExist()
+
     def get_absolute_url (self) :
         if self.url :
             return self.url
--- a/qrurls/views.py	Fri Sep 06 01:36:33 2013 +0300
+++ b/qrurls/views.py	Sat Sep 07 15:35:56 2013 +0300
@@ -1,3 +1,6 @@
+import calendar # timegm
+import logging
+
 from django.core.cache import cache
 from django.http import HttpResponse, HttpResponseRedirect, Http404
 from django.shortcuts import render
@@ -5,7 +8,7 @@
 
 from qrurls.models import URL, URLItem
 
-import calendar # timegm
+log = logging.getLogger('qrurls.views')
 
 """
     Public frontend UI.
@@ -20,34 +23,6 @@
 def http_datetime (dt) :
     return http.http_date(calendar.timegm(dt.utctimetuple()))
 
-def _get_url_item (shorturl=None, shorturl_id=None, item_id=None, related=()) :
-    # JOIN against shorturl, urlimage
-    url_item = URLItem.objects.select_related(*related)
-
-    if shorturl:
-        url_item = url_item.filter(shorturl__shorturl=shorturl)
-    elif shorturl_id:
-        shorturl_id = int(shorturl_id)
-        url_item = url_item.filter(shorturl__id=shorturl_id)
-    else:
-        raise URLNotFound()
-    
-    # match for published items
-    now = timezone.now()
-    url_item = url_item.filter(published__lt=now).order_by('-published')
-   
-    if item_id :
-        try :
-            return url_item.get(id=int(item_id))
-        except URLItem.DoesNotExist:
-            raise URLItemNotFound()
-    else :
-        # most recent
-        try:
-            return url_item[0]
-        except IndexError:
-            raise URLItemNotFound()
-
 def index (request) :
     urls = URL.objects.all()
     return render(request, 'qrurls/index.html', dict(
@@ -64,12 +39,14 @@
     
     if data :
         url, modified = data
+        log.info("get cache: %s: %s", key, url)
     else:
-        url_item = _get_url_item(shorturl=shorturl, shorturl_id=shorturl_id)
+        url_item = URLItem.search(shorturl=shorturl, shorturl_id=shorturl_id)
 
         modified = url_item.last_modified()
         url = url_item.get_absolute_url()
-
+        
+        log.info("set cache: %s: %s", key, url)
         cache.set(key, (url, modified)) # XXX: expiry
 
     # redirect, either directly, or to image()