1 from collections import Counter
3 from django.apps import apps
4 from django.db import models
5 from django.template.loader import render_to_string
6 from django.urls import reverse
7 from django.utils.translation import gettext_lazy as _
8 from admin_ordering.models import OrderableModel
9 from wikidata.client import Client
10 from .constants import WIKIDATA
11 from .wikidata import WikidataModel
12 from .wikimedia import WikiMedia
15 class Author(WikidataModel):
16 slug = models.SlugField(max_length=255, null=True, blank=True, unique=True)
17 first_name = models.CharField(_("first name"), max_length=255, blank=True)
18 last_name = models.CharField(_("last name"), max_length=255, blank=True)
20 name_de = models.CharField(_("name (de)"), max_length=255, blank=True)
21 name_lt = models.CharField(_("name (lt)"), max_length=255, blank=True)
23 gender = models.CharField(_("gender"), max_length=255, blank=True)
24 nationality = models.CharField(_("nationality"), max_length=255, blank=True)
25 year_of_birth = models.SmallIntegerField(_("year of birth"), null=True, blank=True)
26 year_of_birth_inexact = models.BooleanField(_("inexact"), default=False)
27 year_of_birth_range = models.SmallIntegerField(_("year of birth, range end"), null=True, blank=True)
28 date_of_birth = models.DateField(_("date_of_birth"), null=True, blank=True)
29 place_of_birth = models.ForeignKey(
30 'Place', models.PROTECT, null=True, blank=True,
31 verbose_name=_('place of birth'),
32 related_name='authors_born'
34 year_of_death = models.SmallIntegerField(_("year of death"), null=True, blank=True)
35 year_of_death_inexact = models.BooleanField(_("inexact"), default=False)
36 year_of_death_range = models.SmallIntegerField(_("year of death, range end"), null=True, blank=True)
37 date_of_death = models.DateField(_("date_of_death"), null=True, blank=True)
38 place_of_death = models.ForeignKey(
39 'Place', models.PROTECT, null=True, blank=True,
40 verbose_name=_('place of death'),
41 related_name='authors_died'
43 status = models.PositiveSmallIntegerField(
54 notes = models.TextField(_("notes"), blank=True)
56 gazeta_link = models.CharField(_("gazeta link"), max_length=255, blank=True)
57 culturepl_link = models.CharField(_("culture.pl link"), max_length=255, blank=True)
58 plwiki = models.CharField(blank=True, max_length=255)
59 photo = models.ImageField(blank=True, null=True, upload_to='catalogue/author/')
60 photo_source = models.CharField(blank=True, max_length=255)
61 photo_attribution = models.CharField(max_length=255, blank=True)
63 description = models.TextField(_("description"), blank=True)
65 priority = models.PositiveSmallIntegerField(
67 default=0, choices=[(0, _("Low")), (1, _("Medium")), (2, _("High"))]
69 collections = models.ManyToManyField("Collection", blank=True, verbose_name=_("collections"))
72 verbose_name = _('author')
73 verbose_name_plural = _('authors')
74 ordering = ("last_name", "first_name", "year_of_death")
77 first_name = WIKIDATA.GIVEN_NAME
78 last_name = WIKIDATA.LAST_NAME
79 date_of_birth = WIKIDATA.DATE_OF_BIRTH
80 year_of_birth = WIKIDATA.DATE_OF_BIRTH
81 place_of_birth = WIKIDATA.PLACE_OF_BIRTH
82 date_of_death = WIKIDATA.DATE_OF_DEATH
83 year_of_death = WIKIDATA.DATE_OF_DEATH
84 place_of_death = WIKIDATA.PLACE_OF_DEATH
85 gender = WIKIDATA.GENDER
88 photo = WikiMedia.download(WIKIDATA.IMAGE)
89 photo_source = WikiMedia.descriptionurl(WIKIDATA.IMAGE)
90 photo_attribution = WikiMedia.attribution(WIKIDATA.IMAGE)
93 if not obj.first_name and not obj.last_name:
94 yield 'first_name', 'label'
97 name = f"{self.first_name} {self.last_name}"
98 if self.year_of_death is not None:
99 name += f' (zm. {self.year_of_death})'
102 def get_absolute_url(self):
103 return reverse("catalogue_author", args=[self.slug])
107 return f"{self.last_name}, {self.first_name}"
111 if self.year_of_death:
112 return self.year_of_death + 71
113 elif self.year_of_death == 0:
118 def generate_description(self):
119 t = render_to_string(
120 'catalogue/author_description.html',
125 class NotableBook(OrderableModel):
126 author = models.ForeignKey(Author, models.CASCADE)
127 book = models.ForeignKey('Book', models.CASCADE)
130 class Category(WikidataModel):
131 name = models.CharField(_("name"), max_length=255)
132 slug = models.SlugField(max_length=255, unique=True)
140 class Epoch(Category):
142 verbose_name = _('epoch')
143 verbose_name_plural = _('epochs')
146 class Genre(Category):
148 verbose_name = _('genre')
149 verbose_name_plural = _('genres')
152 class Kind(Category):
154 verbose_name = _('kind')
155 verbose_name_plural = _('kinds')
158 class Book(WikidataModel):
159 slug = models.SlugField(max_length=255, blank=True, null=True, unique=True)
160 authors = models.ManyToManyField(Author, blank=True, verbose_name=_("authors"))
161 translators = models.ManyToManyField(
163 related_name="translated_book_set",
164 related_query_name="translated_book",
166 verbose_name=_("translators")
168 epochs = models.ManyToManyField(Epoch, blank=True, verbose_name=_("epochs"))
169 kinds = models.ManyToManyField(Kind, blank=True, verbose_name=_("kinds"))
170 genres = models.ManyToManyField(Genre, blank=True, verbose_name=_("genres"))
171 title = models.CharField(_("title"), max_length=255, blank=True)
172 language = models.CharField(_("language"), max_length=255, blank=True)
173 based_on = models.ForeignKey(
174 "self", models.PROTECT, related_name="translation", null=True, blank=True,
175 verbose_name=_("based on")
177 scans_source = models.CharField(_("scans source"), max_length=255, blank=True)
178 text_source = models.CharField(_("text source"), max_length=255, blank=True)
179 notes = models.TextField(_("notes"), blank=True)
180 priority = models.PositiveSmallIntegerField(
182 default=0, choices=[(0, _("Low")), (1, _("Medium")), (2, _("High"))]
184 original_year = models.IntegerField(_('original publication year'), null=True, blank=True)
185 pd_year = models.IntegerField(_('year of entry into PD'), null=True, blank=True)
186 gazeta_link = models.CharField(_("gazeta link"), max_length=255, blank=True)
187 collections = models.ManyToManyField("Collection", blank=True, verbose_name=_("collections"))
189 estimated_chars = models.IntegerField(_("estimated number of characters"), null=True, blank=True)
190 estimated_verses = models.IntegerField(_("estimated number of verses"), null=True, blank=True)
191 estimate_source = models.CharField(_("source of estimates"), max_length=2048, blank=True)
193 free_license = models.BooleanField(_('free license'), default=False)
194 polona_missing = models.BooleanField(_('missing on Polona'), default=False)
197 ordering = ("title",)
198 verbose_name = _('book')
199 verbose_name_plural = _('books')
202 authors = WIKIDATA.AUTHOR
203 translators = WIKIDATA.TRANSLATOR
204 title = WIKIDATA.TITLE
205 language = WIKIDATA.LANGUAGE
206 based_on = WIKIDATA.BASED_ON
207 original_year = WIKIDATA.PUBLICATION_DATE
208 notes = "description"
212 if self.original_year:
213 txt = f"{txt} ({self.original_year})"
214 astr = self.authors_str()
216 txt = f"{txt}, {astr}"
217 tstr = self.translators_str()
219 txt = f"{txt}, tłum. {tstr}"
222 def get_absolute_url(self):
223 return reverse("catalogue_book", args=[self.slug])
227 return f'https://wolnelektury.pl/katalog/lektura/{self.slug}/'
229 def authors_str(self):
230 return ", ".join(str(author) for author in self.authors.all())
231 authors_str.admin_order_field = 'authors__last_name'
232 authors_str.short_description = _('Author')
234 def translators_str(self):
235 return ", ".join(str(author) for author in self.translators.all())
236 translators_str.admin_order_field = 'translators__last_name'
237 translators_str.short_description = _('Translator')
239 def get_estimated_costs(self):
241 work_type: work_type.calculate(self)
242 for work_type in WorkType.objects.all()
246 class CollectionCategory(models.Model):
247 name = models.CharField(_("name"), max_length=255)
248 parent = models.ForeignKey('self', models.SET_NULL, related_name='children', null=True, blank=True, verbose_name=_("parent"))
249 notes = models.TextField(_("notes"), blank=True)
252 ordering = ('parent__name', 'name')
253 verbose_name = _('collection category')
254 verbose_name_plural = _('collection categories')
258 return f"{self.parent} / {self.name}"
263 class Collection(models.Model):
264 name = models.CharField(_("name"), max_length=255)
265 slug = models.SlugField(max_length=255, unique=True)
266 category = models.ForeignKey(CollectionCategory, models.SET_NULL, null=True, blank=True, verbose_name=_("category"))
267 notes = models.TextField(_("notes"), blank=True)
270 ordering = ('category', 'name')
271 verbose_name = _('collection')
272 verbose_name_plural = _('collections')
276 return f"{self.category} / {self.name}"
280 def get_estimated_costs(self):
282 for book in self.book_set.all():
283 for k, v in book.get_estimated_costs().items():
286 for author in self.author_set.all():
287 for book in author.book_set.all():
288 for k, v in book.get_estimated_costs().items():
290 for book in author.translated_book_set.all():
291 for k, v in book.get_estimated_costs().items():
296 class WorkType(models.Model):
297 name = models.CharField(_("name"), max_length=255)
301 verbose_name = _('work type')
302 verbose_name_plural = _('work types')
304 def get_rate_for(self, book):
305 for workrate in self.workrate_set.all():
306 if workrate.matches(book):
309 def calculate(self, book):
310 workrate = self.get_rate_for(book)
311 if workrate is not None:
312 return workrate.calculate(book)
316 class WorkRate(models.Model):
317 priority = models.IntegerField(_("priority"), default=1)
318 per_normpage = models.DecimalField(_("per normalized page"), decimal_places=2, max_digits=6, null=True, blank=True)
319 per_verse = models.DecimalField(_("per verse"), decimal_places=2, max_digits=6, null=True, blank=True)
320 work_type = models.ForeignKey(WorkType, models.CASCADE, verbose_name=_("work type"))
321 epochs = models.ManyToManyField(Epoch, blank=True, verbose_name=_("epochs"))
322 kinds = models.ManyToManyField(Kind, blank=True, verbose_name=_("kinds"))
323 genres = models.ManyToManyField(Genre, blank=True, verbose_name=_("genres"))
324 collections = models.ManyToManyField(Collection, blank=True, verbose_name=_("collections"))
327 ordering = ('priority',)
328 verbose_name = _('work rate')
329 verbose_name_plural = _('work rates')
331 def matches(self, book):
332 for category in 'epochs', 'kinds', 'genres', 'collections':
333 oneof = getattr(self, category).all()
335 if not set(oneof).intersection(
336 getattr(book, category).all()):
340 def calculate(self, book):
342 if book.estimated_verses:
343 return book.estimated_verses * self.per_verse
344 elif self.per_normpage:
345 if book.estimated_chars:
346 return (decimal.Decimal(book.estimated_chars) / 1800 * self.per_normpage).quantize(decimal.Decimal('1.00'), rounding=decimal.ROUND_HALF_UP)
349 class Place(WikidataModel):
350 name = models.CharField(_('name'), max_length=255, blank=True)
351 locative = models.CharField(_('locative'), max_length=255, blank=True, help_text=_('in…'))
354 verbose_name = _('place')
355 verbose_name_plural = _('places')