##// END OF EJS Templates
Made post image use url as a string in admin site
neko259 -
r812:4507f9f3 default
parent child Browse files
Show More
@@ -1,59 +1,62 b''
1 1 import hashlib
2 2 import os
3 3 from random import random
4 4 import time
5 5 from django.db import models
6 6 from boards import thumbs
7 7
8 8 __author__ = 'neko259'
9 9
10 10
11 11 IMAGE_THUMB_SIZE = (200, 150)
12 12 IMAGES_DIRECTORY = 'images/'
13 13 FILE_EXTENSION_DELIMITER = '.'
14 14
15 15
16 16 class PostImage(models.Model):
17 17 class Meta:
18 18 app_label = 'boards'
19 19 ordering = ('id',)
20 20
21 21 def _update_image_filename(self, filename):
22 22 """
23 23 Gets unique image filename
24 24 """
25 25
26 26 path = IMAGES_DIRECTORY
27 27 new_name = str(int(time.mktime(time.gmtime())))
28 28 new_name += str(int(random() * 1000))
29 29 new_name += FILE_EXTENSION_DELIMITER
30 30 new_name += filename.split(FILE_EXTENSION_DELIMITER)[-1:][0]
31 31
32 32 return os.path.join(path, new_name)
33 33
34 34 width = models.IntegerField(default=0)
35 35 height = models.IntegerField(default=0)
36 36
37 37 pre_width = models.IntegerField(default=0)
38 38 pre_height = models.IntegerField(default=0)
39 39
40 40 image = thumbs.ImageWithThumbsField(upload_to=_update_image_filename,
41 41 blank=True, sizes=(IMAGE_THUMB_SIZE,),
42 42 width_field='width',
43 43 height_field='height',
44 44 preview_width_field='pre_width',
45 45 preview_height_field='pre_height')
46 46 hash = models.CharField(max_length=36)
47 47
48 48 def save(self, *args, **kwargs):
49 49 """
50 50 Saves the model and computes the image hash for deduplication purposes.
51 51 """
52 52
53 53 if not self.pk and self.image:
54 54 md5 = hashlib.md5()
55 55 for chunk in self.image.chunks():
56 56 md5.update(chunk)
57 57 self.hash = md5.hexdigest()
58 58 super(PostImage, self).save(*args, **kwargs)
59 59
60 def __str__(self):
61 return self.image.url
62
General Comments 0
You need to be logged in to leave comments. Login now