1 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from django.core.files.base import ContentFile
6 from django.core.files.storage import FileSystemStorage
7 from django.db import models
8 from django.db.models.signals import post_save
9 from django.dispatch import receiver
10 from django.urls import reverse
11 from django.utils.translation import gettext_lazy as _
12 from django.contrib.sites.models import Site
13 from PIL import Image as PILImage
14 from librarian.dcparser import BookInfo
15 from librarian.meta.types.person import Person
16 from librarian.cover import make_cover
17 from cover.utils import URLOpener
20 class OverwriteStorage(FileSystemStorage):
22 def get_available_name(self, name, max_length=None):
27 class Image(models.Model):
28 title = models.CharField(max_length=255, verbose_name=_('title'))
29 author = models.CharField(max_length=255, verbose_name=_('author'))
30 license_name = models.CharField(max_length=255, verbose_name=_('license name'))
31 license_url = models.URLField(max_length=255, blank=True, verbose_name=_('license URL'))
32 source_url = models.URLField(verbose_name=_('source URL'), null=True, blank=True)
33 download_url = models.URLField(max_length=4096, unique=True, verbose_name=_('image download URL'), null=True, blank=True)
34 file = models.ImageField(
35 upload_to='cover/image',
36 storage=OverwriteStorage(),
37 verbose_name=_('file')
39 use_file = models.ImageField(
40 upload_to='cover/use',
41 storage=OverwriteStorage(),
43 verbose_name=_('file for use')
46 example = models.ImageField(
47 upload_to='cover/example',
48 storage=OverwriteStorage(),
52 cut_top = models.IntegerField(default=0, )
53 cut_bottom = models.IntegerField(default=0)
54 cut_left = models.IntegerField(default=0)
55 cut_right = models.IntegerField(default=0)
58 verbose_name = _('cover image')
59 verbose_name_plural = _('cover images')
62 return u"%s - %s" % (self.author, self.title)
64 def save(self, **kwargs):
65 super().save(**kwargs)
67 if self.cut_top or self.cut_bottom or self.cut_left or self.cut_right:
68 img = PILImage.open(img)
72 img.size[0] - self.cut_right,
73 img.size[1] - self.cut_bottom,
80 img = ContentFile(buffer.getvalue())
87 super().save(update_fields=['use_file'])
91 ContentFile(self.build_example().get_bytes()),
94 super().save(update_fields=['example'])
97 def build_example(self):
102 info.translators = []
103 info.cover_class = None
104 info.cover_box_position = None
106 info.cover_url = 'file://' + self.use_file.path
107 return make_cover(info, width=200).output_file()
109 def get_absolute_url(self):
110 return reverse('cover_image', args=[self.id])
112 def get_full_url(self):
113 return "http://%s%s" % (Site.objects.get_current().domain, self.get_absolute_url())
115 def cut_percentages(self):
116 img = PILImage.open(self.file)
117 max_w, max_h = 600, 600
119 scale = min(max_w / w, max_h / h)
120 ws, hs = round(w * scale), round(h * scale)
123 'left': 100 * self.cut_left / w,
124 'right': 100 * self.cut_right / w,
125 'top': 100 * self.cut_top / h,
126 'bottom': 100 * self.cut_bottom / h,
134 return f'{self.cut_top}.{self.cut_bottom}.{self.cut_left}.{self.cut_right}'
137 def attribution(self):
140 pieces.append(self.title)
142 pieces.append(self.author)
143 if self.license_name:
144 pieces.append(self.license_name)
146 pieces.append(self.source_url.split('/')[2])
147 return ', '.join(pieces)
151 @receiver(post_save, sender=Image)
152 def download_image(sender, instance, **kwargs):
153 if instance.pk and not instance.file:
154 t = URLOpener().open(instance.download_url).read()
155 instance.file.save("%d.jpg" % instance.pk, ContentFile(t))