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
86 notes = WikiMedia.append("description")
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):
232 return ", ".join(str(author) for author in self.authors.all())
233 authors_str.admin_order_field = 'authors__last_name'
234 authors_str.short_description = _('Author')
236 def translators_str(self):
239 return ", ".join(str(author) for author in self.translators.all())
240 translators_str.admin_order_field = 'translators__last_name'
241 translators_str.short_description = _('Translator')
243 def authors_first_names(self):
244 return ', '.join(a.first_name for a in self.authors.all())
246 def authors_last_names(self):
247 return ', '.join(a.last_name for a in self.authors.all())
249 def translators_first_names(self):
250 return ', '.join(a.first_name for a in self.translators.all())
252 def translators_last_names(self):
253 return ', '.join(a.last_name for a in self.translators.all())
255 def get_estimated_costs(self):
257 work_type: work_type.calculate(self)
258 for work_type in WorkType.objects.all()
262 class CollectionCategory(models.Model):
263 name = models.CharField(_("name"), max_length=255)
264 parent = models.ForeignKey('self', models.SET_NULL, related_name='children', null=True, blank=True, verbose_name=_("parent"))
265 notes = models.TextField(_("notes"), blank=True)
268 ordering = ('parent__name', 'name')
269 verbose_name = _('collection category')
270 verbose_name_plural = _('collection categories')
274 return f"{self.parent} / {self.name}"
279 class Collection(models.Model):
280 name = models.CharField(_("name"), max_length=255)
281 slug = models.SlugField(max_length=255, unique=True)
282 category = models.ForeignKey(CollectionCategory, models.SET_NULL, null=True, blank=True, verbose_name=_("category"))
283 notes = models.TextField(_("notes"), blank=True)
284 description = models.TextField(_("description"), blank=True)
287 ordering = ('category', 'name')
288 verbose_name = _('collection')
289 verbose_name_plural = _('collections')
293 return f"{self.category} / {self.name}"
297 def get_estimated_costs(self):
299 for book in self.book_set.all():
300 for k, v in book.get_estimated_costs().items():
303 for author in self.author_set.all():
304 for book in author.book_set.all():
305 for k, v in book.get_estimated_costs().items():
307 for book in author.translated_book_set.all():
308 for k, v in book.get_estimated_costs().items():
313 class WorkType(models.Model):
314 name = models.CharField(_("name"), max_length=255)
318 verbose_name = _('work type')
319 verbose_name_plural = _('work types')
321 def get_rate_for(self, book):
322 for workrate in self.workrate_set.all():
323 if workrate.matches(book):
326 def calculate(self, book):
327 workrate = self.get_rate_for(book)
328 if workrate is not None:
329 return workrate.calculate(book)
333 class WorkRate(models.Model):
334 priority = models.IntegerField(_("priority"), default=1)
335 per_normpage = models.DecimalField(_("per normalized page"), decimal_places=2, max_digits=6, null=True, blank=True)
336 per_verse = models.DecimalField(_("per verse"), decimal_places=2, max_digits=6, null=True, blank=True)
337 work_type = models.ForeignKey(WorkType, models.CASCADE, verbose_name=_("work type"))
338 epochs = models.ManyToManyField(Epoch, blank=True, verbose_name=_("epochs"))
339 kinds = models.ManyToManyField(Kind, blank=True, verbose_name=_("kinds"))
340 genres = models.ManyToManyField(Genre, blank=True, verbose_name=_("genres"))
341 collections = models.ManyToManyField(Collection, blank=True, verbose_name=_("collections"))
344 ordering = ('priority',)
345 verbose_name = _('work rate')
346 verbose_name_plural = _('work rates')
348 def matches(self, book):
349 for category in 'epochs', 'kinds', 'genres', 'collections':
350 oneof = getattr(self, category).all()
352 if not set(oneof).intersection(
353 getattr(book, category).all()):
357 def calculate(self, book):
359 if book.estimated_verses:
360 return book.estimated_verses * self.per_verse
361 elif self.per_normpage:
362 if book.estimated_chars:
363 return (decimal.Decimal(book.estimated_chars) / 1800 * self.per_normpage).quantize(decimal.Decimal('1.00'), rounding=decimal.ROUND_HALF_UP)
366 class Place(WikidataModel):
367 name = models.CharField(_('name'), max_length=255, blank=True)
368 locative = models.CharField(_('locative'), max_length=255, blank=True, help_text=_('in…'))
371 verbose_name = _('place')
372 verbose_name_plural = _('places')