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',
37 storage=OverwriteStorage(),
38 verbose_name=_('file')
40 use_file = models.ImageField(
41 upload_to='cover/use',
42 storage=OverwriteStorage(),
44 verbose_name=_('file for use')
47 example = models.ImageField(
48 upload_to='cover/example',
49 storage=OverwriteStorage(),
53 cut_top = models.IntegerField(default=0, )
54 cut_bottom = models.IntegerField(default=0)
55 cut_left = models.IntegerField(default=0)
56 cut_right = models.IntegerField(default=0)
59 verbose_name = _('cover image')
60 verbose_name_plural = _('cover images')
63 return u"%s - %s" % (self.author, self.title)
65 def save(self, **kwargs):
66 super().save(**kwargs)
68 if self.cut_top or self.cut_bottom or self.cut_left or self.cut_right:
69 img = PILImage.open(img)
73 img.size[0] - self.cut_right,
74 img.size[1] - self.cut_bottom,
81 img = ContentFile(buffer.getvalue())
88 super().save(update_fields=['use_file'])
92 ContentFile(self.build_example().get_bytes()),
95 super().save(update_fields=['example'])
98 def build_example(self):
103 info.translators = []
104 info.cover_class = None
105 info.cover_box_position = None
107 info.cover_url = 'file://' + self.use_file.path
108 return make_cover(info, width=200).output_file()
110 def get_absolute_url(self):
111 return reverse('cover_image', args=[self.id])
113 def get_full_url(self):
114 return "http://%s%s" % (Site.objects.get_current().domain, self.get_absolute_url())
116 def cut_percentages(self):
117 img = PILImage.open(self.file)
118 max_w, max_h = 600, 600
120 scale = min(max_w / w, max_h / h)
121 ws, hs = round(w * scale), round(h * scale)
124 'left': 100 * self.cut_left / w,
125 'right': 100 * self.cut_right / w,
126 'top': 100 * self.cut_top / h,
127 'bottom': 100 * self.cut_bottom / h,
135 return f'{self.cut_top}.{self.cut_bottom}.{self.cut_left}.{self.cut_right}'
138 def attribution(self):
141 pieces.append(self.title)
143 pieces.append(self.author)
144 if self.license_name:
145 pieces.append(self.license_name)
147 pieces.append(self.source_url.split('/')[2])
148 return ', '.join(pieces)
152 @receiver(post_save, sender=Image)
153 def download_image(sender, instance, **kwargs):
154 if instance.pk and not instance.file:
155 t = URLOpener().open(instance.download_url).read()
156 instance.file.save("%d.jpg" % instance.pk, ContentFile(t))