1 import datetime |
1 import datetime |
2 import hashlib |
2 import hashlib |
|
3 import logging |
3 import os.path |
4 import os.path |
4 |
5 |
5 from django.db import models |
6 from django.db import models |
6 import django.core.files.storage |
7 import django.core.files.storage |
7 from django.core.urlresolvers import reverse |
8 from django.core.urlresolvers import reverse |
8 import django.utils.http |
9 import django.utils.http |
9 from django.contrib.sites.models import get_current_site |
10 from django.contrib.sites.models import get_current_site |
10 from django.utils import timezone |
11 from django.utils import timezone |
|
12 |
|
13 log = logging.getLogger('qrurls.models') |
11 |
14 |
12 QRCODE_API = 'https://chart.googleapis.com/chart?cht=qr&chs={width}x{height}&chl={url}&chld=H' |
15 QRCODE_API = 'https://chart.googleapis.com/chart?cht=qr&chs={width}x{height}&chl={url}&chld=H' |
13 IMAGES_MEDIA = 'qrurls-images' |
16 IMAGES_MEDIA = 'qrurls-images' |
14 |
17 |
15 class SecretFileSystemStorage (django.core.files.storage.FileSystemStorage) : |
18 class SecretFileSystemStorage (django.core.files.storage.FileSystemStorage) : |
177 class Meta: |
180 class Meta: |
178 verbose_name = u"URL Item" |
181 verbose_name = u"URL Item" |
179 verbose_name_plural = u"URL Items" |
182 verbose_name_plural = u"URL Items" |
180 ordering = ['published'] |
183 ordering = ['published'] |
181 |
184 |
|
185 @classmethod |
|
186 def search (cls, shorturl=None, shorturl_id=None, item_id=None, related=()) : |
|
187 """ |
|
188 Return the URLItem for a given shorturl, either the given specific one, |
|
189 or the latest, from the database. |
|
190 |
|
191 Raises URLItem.NotFound |
|
192 """ |
|
193 # JOIN against shorturl, urlimage |
|
194 url_item = cls.objects.select_related(*related) |
|
195 |
|
196 if shorturl: |
|
197 url_item = url_item.filter(shorturl__shorturl=shorturl) |
|
198 elif shorturl_id: |
|
199 shorturl_id = int(shorturl_id) |
|
200 url_item = url_item.filter(shorturl__id=shorturl_id) |
|
201 else: |
|
202 raise cls.DoesNotExist() |
|
203 |
|
204 # match for published items |
|
205 now = timezone.now() |
|
206 url_item = url_item.filter(published__lt=now).order_by('-published') |
|
207 |
|
208 log.info("Search URLItem @ %s", now) |
|
209 |
|
210 if item_id : |
|
211 # specific, but still the published on |
|
212 return url_item.get(id=int(item_id)) # raises DoesNotExist |
|
213 else : |
|
214 # most recent |
|
215 try: |
|
216 return url_item[0] |
|
217 except IndexError: |
|
218 raise DoesNotExist() |
|
219 |
182 def get_absolute_url (self) : |
220 def get_absolute_url (self) : |
183 if self.url : |
221 if self.url : |
184 return self.url |
222 return self.url |
185 elif self.shorturl and self.id : |
223 elif self.shorturl and self.id : |
186 return reverse('shorturl_item', kwargs=dict(shorturl=self.shorturl, item_id=self.id)) |
224 return reverse('shorturl_item', kwargs=dict(shorturl=self.shorturl, item_id=self.id)) |