qrurls/views.py
changeset 82 6442e5c97b48
parent 81 3a2fdc820c41
child 84 e5d8e17f307f
equal deleted inserted replaced
81:3a2fdc820c41 82:6442e5c97b48
     1 import calendar # timegm
     1 import calendar # timegm
     2 import logging
     2 import logging
     3 
     3 
     4 from django.core.cache import cache
       
     5 from django.http import HttpResponse, HttpResponseRedirect, Http404
     4 from django.http import HttpResponse, HttpResponseRedirect, Http404
     6 from django.shortcuts import render
     5 from django.shortcuts import render
     7 from django.utils import timezone, http
     6 from django.utils import timezone, http
     8 
     7 
     9 from qrurls.models import URL, URLItem
     8 from qrurls.models import URL, URLItem
    11 log = logging.getLogger('qrurls.views')
    10 log = logging.getLogger('qrurls.views')
    12 
    11 
    13 """
    12 """
    14     Public frontend UI.
    13     Public frontend UI.
    15 """
    14 """
    16 
       
    17 class URLNotFound (Http404):
       
    18     pass
       
    19 
       
    20 class URLItemNotFound (Http404):
       
    21     pass
       
    22 
    15 
    23 def http_datetime (dt) :
    16 def http_datetime (dt) :
    24     return http.http_date(calendar.timegm(dt.utctimetuple()))
    17     return http.http_date(calendar.timegm(dt.utctimetuple()))
    25 
    18 
    26 def index (request) :
    19 def index (request) :
    31 
    24 
    32 def shorturl (request, shorturl=None) :
    25 def shorturl (request, shorturl=None) :
    33     """
    26     """
    34         Primary frontend for redirecting based on current time.
    27         Primary frontend for redirecting based on current time.
    35     """
    28     """
    36     
       
    37     key = 'qrurls/urlfeed/{shorturl}'.format(shorturl=shorturl) # format as dict
       
    38     data = cache.get(key)
       
    39     
       
    40     if data :
       
    41         url, modified = data
       
    42         log.info("get cache: %s: %s", key, url)
       
    43     else:
       
    44         url_item = URLItem.search(shorturl=shorturl)
       
    45 
    29 
    46         modified = url_item.last_modified()
    30     try:
    47         url = url_item.get_absolute_url()
    31         url, modified = URLItem.get_url(shorturl)
    48         
    32     except URLItem.DoesNotExist:
    49         log.info("set cache: %s: %s", key, url)
    33         raise Http404()
    50         cache.set(key, (url, modified)) # XXX: expiry
       
    51 
    34 
    52     # redirect, either directly, or to image()
    35     # redirect, either directly, or to image()
    53     response = HttpResponseRedirect(url)
    36     response = HttpResponseRedirect(url)
    54     response['Vary'] = ''
    37     response['Vary'] = ''
    55     response['Last-Modified'] = http_datetime(modified)
    38     response['Last-Modified'] = http_datetime(modified)
    58 def item (request, shorturl, item_id) :
    41 def item (request, shorturl, item_id) :
    59     """
    42     """
    60         Frontend for a specific item.
    43         Frontend for a specific item.
    61     """
    44     """
    62     
    45     
    63     url_item = _get_url_item(
    46     try :
    64             shorturl=shorturl, item_id=item_id,
    47         url_item = URLItem.get_item(shorturl, item_id=item_id,
    65             related=('shorturl', 'image')
    48             related=('shorturl', 'image'),
    66     )
    49         )
       
    50     except URLItem.DoesNotExist:
       
    51         raise Http404()
    67 
    52 
    68     if url_item.url :
    53     if url_item.url :
    69         response = HttpResponseRedirect(url_item.url)
    54         response = HttpResponseRedirect(url_item.url)
    70     elif url_item.image :
    55     elif url_item.image :
    71         response = render(request, 'qrurls/image.html', dict(
    56         response = render(request, 'qrurls/image.html', dict(