X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/a4f5747875aa0fb598e69d2200ef431c2fb7f1e9..1c013c31745b335962a07057f962e47212faf80f:/src/cover/models.py?ds=sidebyside diff --git a/src/cover/models.py b/src/cover/models.py index 33c5e738..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 @@ -30,6 +33,7 @@ class Image(models.Model): 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) @@ -73,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]) @@ -81,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):