X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/c06d9009a2f0caff4557384179078b4e45edfb8d..1c013c31745b335962a07057f962e47212faf80f:/src/cover/models.py diff --git a/src/cover/models.py b/src/cover/models.py index 60f1bca5..2c452a27 100644 --- a/src/cover/models.py +++ b/src/cover/models.py @@ -8,9 +8,12 @@ from django.db import models from django.db.models.signals import post_save from django.dispatch import receiver from django.urls import reverse -from django.utils.translation import ugettext_lazy as _ +from django.utils.translation import gettext_lazy as _ from django.contrib.sites.models import Site from PIL import Image as PILImage +from librarian.dcparser import BookInfo +from librarian.meta.types.person import Person +from librarian.cover import make_cover from cover.utils import URLOpener @@ -27,9 +30,10 @@ class Image(models.Model): license_name = models.CharField(max_length=255, verbose_name=_('license name')) license_url = models.URLField(max_length=255, blank=True, verbose_name=_('license URL')) source_url = models.URLField(verbose_name=_('source URL'), null=True, blank=True) - download_url = models.URLField(unique=True, verbose_name=_('image download URL'), null=True, blank=True) + download_url = models.URLField(max_length=4096, unique=True, verbose_name=_('image download URL'), null=True, blank=True) file = models.ImageField( upload_to='cover/image', + max_length=1024, storage=OverwriteStorage(), verbose_name=_('file') ) @@ -39,6 +43,13 @@ class Image(models.Model): editable=True, verbose_name=_('file for use') ) + + example = models.ImageField( + upload_to='cover/example', + storage=OverwriteStorage(), + editable=False, + ) + cut_top = models.IntegerField(default=0, ) cut_bottom = models.IntegerField(default=0) cut_left = models.IntegerField(default=0) @@ -52,6 +63,7 @@ class Image(models.Model): return u"%s - %s" % (self.author, self.title) def save(self, **kwargs): + super().save(**kwargs) img = self.file if self.cut_top or self.cut_bottom or self.cut_left or self.cut_right: img = PILImage.open(img) @@ -72,7 +84,28 @@ class Image(models.Model): img, save=False ) - super().save(**kwargs) + + super().save(update_fields=['use_file']) + + self.example.save( + "%d.jpg" % self.pk, + ContentFile(self.build_example().get_bytes()), + save=False + ) + super().save(update_fields=['example']) + + + def build_example(self): + class A: + pass + info = A() + info.authors = [] + info.translators = [] + info.cover_class = None + info.cover_box_position = None + info.title = '?' + info.cover_url = 'file://' + self.use_file.path + return make_cover(info, width=200).output_file() def get_absolute_url(self): return reverse('cover_image', args=[self.id]) @@ -80,6 +113,41 @@ class Image(models.Model): def get_full_url(self): return "http://%s%s" % (Site.objects.get_current().domain, self.get_absolute_url()) + def cut_percentages(self): + img = PILImage.open(self.file) + max_w, max_h = 600, 600 + w, h = img.size + scale = min(max_w / w, max_h / h) + ws, hs = round(w * scale), round(h * scale) + + return { + 'left': 100 * self.cut_left / w, + 'right': 100 * self.cut_right / w, + 'top': 100 * self.cut_top / h, + 'bottom': 100 * self.cut_bottom / h, + 'width': ws, + 'height': hs, + 'th': f'{ws}x{hs}', + } + + @property + def etag(self): + return f'{self.cut_top}.{self.cut_bottom}.{self.cut_left}.{self.cut_right}' + + @property + def attribution(self): + pieces = [] + if self.title: + pieces.append(self.title) + if self.author: + pieces.append(self.author) + if self.license_name: + pieces.append(self.license_name) + if self.source_url: + pieces.append(self.source_url.split('/')[2]) + return ', '.join(pieces) + + @receiver(post_save, sender=Image) def download_image(sender, instance, **kwargs):