1 import datetime |
1 import datetime |
|
2 import hashlib |
|
3 import os.path |
2 |
4 |
3 from django.db import models |
5 from django.db import models |
|
6 import django.core.files.storage |
4 from django.core.urlresolvers import reverse |
7 from django.core.urlresolvers import reverse |
5 import django.utils.http |
8 import django.utils.http |
6 from django.contrib.sites.models import get_current_site |
9 from django.contrib.sites.models import get_current_site |
7 from django.utils import timezone |
10 from django.utils import timezone |
8 |
11 |
9 QRCODE_API = 'https://chart.googleapis.com/chart?cht=qr&chs={width}x{height}&chl={url}&chld=H' |
12 QRCODE_API = 'https://chart.googleapis.com/chart?cht=qr&chs={width}x{height}&chl={url}&chld=H' |
|
13 IMAGES_MEDIA = 'qrurls-images' |
|
14 |
|
15 class SecretFileSystemStorage (django.core.files.storage.FileSystemStorage) : |
|
16 """ |
|
17 Store files named by a sha1 hash of their contents. |
|
18 """ |
|
19 |
|
20 HASH = hashlib.sha1 |
|
21 |
|
22 def _hash (self, content) : |
|
23 """Compute hash for given UploadedFile (as a hex string).""" |
|
24 hash = self.HASH() |
|
25 |
|
26 for chunk in content.chunks() : |
|
27 hash.update(chunk) |
|
28 |
|
29 return hash.hexdigest() |
|
30 |
|
31 def save (self, name, content) : |
|
32 # Get the proper name for the file, as it will actually be saved. |
|
33 if name is None: |
|
34 name = content.name |
|
35 |
|
36 dirname, filename = os.path.split(name) |
|
37 basename, fileext = os.path.splitext(filename) |
|
38 |
|
39 # hash |
|
40 name = "%s/%s%s" % (dirname, self._hash(content), fileext) |
|
41 |
|
42 return super(SecretFileSystemStorage, self).save(name, content) |
10 |
43 |
11 class URL(models.Model): |
44 class URL(models.Model): |
12 shorturl = models.SlugField(unique=True) |
45 shorturl = models.SlugField(unique=True) |
13 publishing_schedule = models.TimeField(default=datetime.time(), |
46 publishing_schedule = models.TimeField(default=datetime.time(), |
14 help_text="Default time to publish new URLItems") |
47 help_text="Default time to publish new URLItems") |
62 |
95 |
63 def __unicode__ (self) : |
96 def __unicode__ (self) : |
64 return self.shorturl |
97 return self.shorturl |
65 |
98 |
66 class URLImage(models.Model): |
99 class URLImage(models.Model): |
67 image = models.ImageField(upload_to='qrurls-images') |
100 image = models.ImageField(upload_to=IMAGES_MEDIA, storage=SecretFileSystemStorage()) |
68 title = models.CharField(max_length=1024) |
101 title = models.CharField(max_length=1024) |
69 uploaded = models.DateTimeField(auto_now_add=True) |
102 uploaded = models.DateTimeField(auto_now_add=True) |
70 |
103 |
71 class Meta: |
104 class Meta: |
72 verbose_name = u"URL Image" |
105 verbose_name = u"URL Image" |