##// END OF EJS Templates
Scroll to bottom after posting
Scroll to bottom after posting

File last commit:

r812:4507f9f3 default
r857:7e00dc33 default
Show More
image.py
62 lines | 1.8 KiB | text/x-python | PythonLexer
import hashlib
import os
from random import random
import time
from django.db import models
from boards import thumbs
__author__ = 'neko259'
IMAGE_THUMB_SIZE = (200, 150)
IMAGES_DIRECTORY = 'images/'
FILE_EXTENSION_DELIMITER = '.'
class PostImage(models.Model):
class Meta:
app_label = 'boards'
ordering = ('id',)
def _update_image_filename(self, filename):
"""
Gets unique image filename
"""
path = IMAGES_DIRECTORY
new_name = str(int(time.mktime(time.gmtime())))
new_name += str(int(random() * 1000))
new_name += FILE_EXTENSION_DELIMITER
new_name += filename.split(FILE_EXTENSION_DELIMITER)[-1:][0]
return os.path.join(path, new_name)
width = models.IntegerField(default=0)
height = models.IntegerField(default=0)
pre_width = models.IntegerField(default=0)
pre_height = models.IntegerField(default=0)
image = thumbs.ImageWithThumbsField(upload_to=_update_image_filename,
blank=True, sizes=(IMAGE_THUMB_SIZE,),
width_field='width',
height_field='height',
preview_width_field='pre_width',
preview_height_field='pre_height')
hash = models.CharField(max_length=36)
def save(self, *args, **kwargs):
"""
Saves the model and computes the image hash for deduplication purposes.
"""
if not self.pk and self.image:
md5 = hashlib.md5()
for chunk in self.image.chunks():
md5.update(chunk)
self.hash = md5.hexdigest()
super(PostImage, self).save(*args, **kwargs)
def __str__(self):
return self.image.url