|
|
from django.core.files.images import get_image_dimensions
|
|
|
from django.db import models
|
|
|
from django.template.defaultfilters import filesizeformat
|
|
|
|
|
|
from boards import thumbs, utils
|
|
|
import boards
|
|
|
from boards.models.base import Viewable
|
|
|
from boards.models import STATUS_ARCHIVE
|
|
|
from boards.utils import get_upload_filename
|
|
|
|
|
|
|
|
|
__author__ = 'neko259'
|
|
|
|
|
|
|
|
|
IMAGE_THUMB_SIZE = (200, 150)
|
|
|
HASH_LENGTH = 36
|
|
|
|
|
|
CSS_CLASS_IMAGE = 'image'
|
|
|
CSS_CLASS_THUMB = 'thumb'
|
|
|
|
|
|
|
|
|
class PostImageManager(models.Manager):
|
|
|
def create_with_hash(self, image):
|
|
|
image_hash = utils.get_file_hash(image)
|
|
|
existing = self.filter(hash=image_hash)
|
|
|
if len(existing) > 0:
|
|
|
post_image = existing[0]
|
|
|
else:
|
|
|
post_image = PostImage.objects.create(image=image)
|
|
|
|
|
|
return post_image
|
|
|
|
|
|
def get_random_images(self, count, tags=None):
|
|
|
images = self.exclude(post_images__thread__status=STATUS_ARCHIVE)
|
|
|
if tags is not None:
|
|
|
images = images.filter(post_images__threads__tags__in=tags)
|
|
|
return images.order_by('?')[:count]
|
|
|
|
|
|
|
|
|
class PostImage(models.Model, Viewable):
|
|
|
objects = PostImageManager()
|
|
|
|
|
|
class Meta:
|
|
|
app_label = 'boards'
|
|
|
ordering = ('id',)
|
|
|
|
|
|
image = thumbs.ImageWithThumbsField(upload_to=get_upload_filename,
|
|
|
blank=True, sizes=(IMAGE_THUMB_SIZE,))
|
|
|
hash = models.CharField(max_length=HASH_LENGTH)
|
|
|
alias = models.TextField(unique=True, null=True, blank=True)
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
"""
|
|
|
Saves the model and computes the image hash for deduplication purposes.
|
|
|
"""
|
|
|
|
|
|
if not self.pk and self.image:
|
|
|
self.hash = utils.get_file_hash(self.image)
|
|
|
super(PostImage, self).save(*args, **kwargs)
|
|
|
|
|
|
def __str__(self):
|
|
|
return self.image.url
|
|
|
|
|
|
def get_view(self):
|
|
|
metadata = '{}, {}'.format(self.image.name.split('.')[-1],
|
|
|
filesizeformat(self.image.size))
|
|
|
width, height = get_image_dimensions(self.image.file)
|
|
|
preview_path = self.image.path.replace('.', '.200x150.')
|
|
|
pre_width, pre_height = get_image_dimensions(preview_path)
|
|
|
return '<div class="{}">' \
|
|
|
'<a class="{}" href="{full}">' \
|
|
|
'<img class="post-image-preview"' \
|
|
|
' src="{}"' \
|
|
|
' alt="{}"' \
|
|
|
' width="{}"' \
|
|
|
' height="{}"' \
|
|
|
' data-width="{}"' \
|
|
|
' data-height="{}" />' \
|
|
|
'</a>' \
|
|
|
'<div class="image-metadata">'\
|
|
|
'<a href="{full}" download>{image_meta}</a>'\
|
|
|
'</div>' \
|
|
|
'</div>'\
|
|
|
.format(CSS_CLASS_IMAGE, CSS_CLASS_THUMB,
|
|
|
self.image.url_200x150,
|
|
|
str(self.hash), str(pre_width),
|
|
|
str(pre_height), str(width), str(height),
|
|
|
full=self.image.url, image_meta=metadata)
|
|
|
|
|
|
def get_random_associated_post(self):
|
|
|
posts = boards.models.Post.objects.filter(images__in=[self])
|
|
|
return posts.order_by('?').first()
|
|
|
|