--- a/qrurls/models.py Sat Sep 07 15:38:24 2013 +0300
+++ b/qrurls/models.py Sat Sep 07 15:48:23 2013 +0300
@@ -3,11 +3,12 @@
import logging
import os.path
-from django.db import models
import django.core.files.storage
-from django.core.urlresolvers import reverse
import django.utils.http
from django.contrib.sites.models import get_current_site
+from django.core.cache import cache
+from django.core.urlresolvers import reverse
+from django.db import models
from django.utils import timezone
log = logging.getLogger('qrurls.models')
@@ -183,7 +184,7 @@
ordering = ['published']
@classmethod
- def search (cls, shorturl=None, item_id=None, related=()) :
+ def get_item (cls, shorturl, item_id=None, related=()) :
"""
Return the URLItem for a given shorturl, either the given specific one,
or the latest, from the database.
@@ -204,19 +205,48 @@
# 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
+ item_id = int(item_id)
+ log.debug("search @ %d", item_id)
+
+ return url_item.get(id=item_id) # raises DoesNotExist
else :
# most recent
+ log.debug("search @ %s", now)
try:
return url_item[0]
except IndexError:
raise DoesNotExist()
+ @classmethod
+ def get_url (cls, shorturl) :
+ """
+ Return the current URL for a given shorturl, from cache or DB.
+
+ Returns url:str, modified:datetime
+ Raises URLItem.NotFound
+ """
+
+ key = 'qrurls/url/{shorturl}'.format(shorturl=shorturl)
+ get = cache.get(key)
+
+ if get :
+ url, modified = get
+ log.debug("get cache: %s: %s", key, url)
+ else:
+ # from db
+ url_item = cls.get_item(shorturl=shorturl)
+
+ url = url_item.get_absolute_url()
+ modified = url_item.last_modified()
+
+ log.debug("set cache: %s: %s", key, url)
+ cache.set(key, (url, modified)) # XXX: expiry
+
+ return url, modified
+
def get_absolute_url (self) :
if self.url :
return self.url
--- a/qrurls/views.py Sat Sep 07 15:38:24 2013 +0300
+++ b/qrurls/views.py Sat Sep 07 15:48:23 2013 +0300
@@ -1,7 +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
from django.utils import timezone, http
@@ -14,12 +13,6 @@
Public frontend UI.
"""
-class URLNotFound (Http404):
- pass
-
-class URLItemNotFound (Http404):
- pass
-
def http_datetime (dt) :
return http.http_date(calendar.timegm(dt.utctimetuple()))
@@ -33,21 +26,11 @@
"""
Primary frontend for redirecting based on current time.
"""
-
- key = 'qrurls/urlfeed/{shorturl}'.format(shorturl=shorturl) # format as dict
- data = cache.get(key)
-
- if data :
- url, modified = data
- log.info("get cache: %s: %s", key, url)
- else:
- url_item = URLItem.search(shorturl=shorturl)
- 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
+ try:
+ url, modified = URLItem.get_url(shorturl)
+ except URLItem.DoesNotExist:
+ raise Http404()
# redirect, either directly, or to image()
response = HttpResponseRedirect(url)
@@ -60,10 +43,12 @@
Frontend for a specific item.
"""
- url_item = _get_url_item(
- shorturl=shorturl, item_id=item_id,
- related=('shorturl', 'image')
- )
+ try :
+ url_item = URLItem.get_item(shorturl, item_id=item_id,
+ related=('shorturl', 'image'),
+ )
+ except URLItem.DoesNotExist:
+ raise Http404()
if url_item.url :
response = HttpResponseRedirect(url_item.url)