##// 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 import hashlib
1 import hashlib
2 import os
2 import os
3 from random import random
3 from random import random
4 import time
4 import time
5 from django.db import models
5 from django.db import models
6 from boards import thumbs
6 from boards import thumbs
7
7
8 __author__ = 'neko259'
8 __author__ = 'neko259'
9
9
10
10
11 IMAGE_THUMB_SIZE = (200, 150)
11 IMAGE_THUMB_SIZE = (200, 150)
12 IMAGES_DIRECTORY = 'images/'
12 IMAGES_DIRECTORY = 'images/'
13 FILE_EXTENSION_DELIMITER = '.'
13 FILE_EXTENSION_DELIMITER = '.'
14
14
15
15
16 class PostImage(models.Model):
16 class PostImage(models.Model):
17 class Meta:
17 class Meta:
18 app_label = 'boards'
18 app_label = 'boards'
19 ordering = ('id',)
19 ordering = ('id',)
20
20
21 def _update_image_filename(self, filename):
21 def _update_image_filename(self, filename):
22 """
22 """
23 Gets unique image filename
23 Gets unique image filename
24 """
24 """
25
25
26 path = IMAGES_DIRECTORY
26 path = IMAGES_DIRECTORY
27 new_name = str(int(time.mktime(time.gmtime())))
27 new_name = str(int(time.mktime(time.gmtime())))
28 new_name += str(int(random() * 1000))
28 new_name += str(int(random() * 1000))
29 new_name += FILE_EXTENSION_DELIMITER
29 new_name += FILE_EXTENSION_DELIMITER
30 new_name += filename.split(FILE_EXTENSION_DELIMITER)[-1:][0]
30 new_name += filename.split(FILE_EXTENSION_DELIMITER)[-1:][0]
31
31
32 return os.path.join(path, new_name)
32 return os.path.join(path, new_name)
33
33
34 width = models.IntegerField(default=0)
34 width = models.IntegerField(default=0)
35 height = models.IntegerField(default=0)
35 height = models.IntegerField(default=0)
36
36
37 pre_width = models.IntegerField(default=0)
37 pre_width = models.IntegerField(default=0)
38 pre_height = models.IntegerField(default=0)
38 pre_height = models.IntegerField(default=0)
39
39
40 image = thumbs.ImageWithThumbsField(upload_to=_update_image_filename,
40 image = thumbs.ImageWithThumbsField(upload_to=_update_image_filename,
41 blank=True, sizes=(IMAGE_THUMB_SIZE,),
41 blank=True, sizes=(IMAGE_THUMB_SIZE,),
42 width_field='width',
42 width_field='width',
43 height_field='height',
43 height_field='height',
44 preview_width_field='pre_width',
44 preview_width_field='pre_width',
45 preview_height_field='pre_height')
45 preview_height_field='pre_height')
46 hash = models.CharField(max_length=36)
46 hash = models.CharField(max_length=36)
47
47
48 def save(self, *args, **kwargs):
48 def save(self, *args, **kwargs):
49 """
49 """
50 Saves the model and computes the image hash for deduplication purposes.
50 Saves the model and computes the image hash for deduplication purposes.
51 """
51 """
52
52
53 if not self.pk and self.image:
53 if not self.pk and self.image:
54 md5 = hashlib.md5()
54 md5 = hashlib.md5()
55 for chunk in self.image.chunks():
55 for chunk in self.image.chunks():
56 md5.update(chunk)
56 md5.update(chunk)
57 self.hash = md5.hexdigest()
57 self.hash = md5.hexdigest()
58 super(PostImage, self).save(*args, **kwargs)
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