##// END OF EJS Templates
Run thread update when connecting to websocket to get missed posts if the...
Run thread update when connecting to websocket to get missed posts if the browser was closed

File last commit:

r812:4507f9f3 default
r892:6155490b 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