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)
19 genitive = models.CharField(
20 'dopełniacz', max_length=255, blank=True,
21 help_text='utwory … (czyje?)'
24 name_de = models.CharField(_("name (de)"), max_length=255, blank=True)
25 name_lt = models.CharField(_("name (lt)"), max_length=255, blank=True)
27 gender = models.CharField(_("gender"), max_length=255, blank=True)
28 nationality = models.CharField(_("nationality"), max_length=255, blank=True)
29 year_of_birth = models.SmallIntegerField(_("year of birth"), null=True, blank=True)
30 year_of_birth_inexact = models.BooleanField(_("inexact"), default=False)
31 year_of_birth_range = models.SmallIntegerField(_("year of birth, range end"), null=True, blank=True)
32 date_of_birth = models.DateField(_("date_of_birth"), null=True, blank=True)
33 place_of_birth = models.ForeignKey(
34 'Place', models.PROTECT, null=True, blank=True,
35 verbose_name=_('place of birth'),
36 related_name='authors_born'
38 year_of_death = models.SmallIntegerField(_("year of death"), null=True, blank=True)
39 year_of_death_inexact = models.BooleanField(_("inexact"), default=False)
40 year_of_death_range = models.SmallIntegerField(_("year of death, range end"), null=True, blank=True)
41 date_of_death = models.DateField(_("date_of_death"), null=True, blank=True)
42 place_of_death = models.ForeignKey(
43 'Place', models.PROTECT, null=True, blank=True,
44 verbose_name=_('place of death'),
45 related_name='authors_died'
47 status = models.PositiveSmallIntegerField(
58 notes = models.TextField(_("notes"), blank=True, help_text=_('private'))
60 gazeta_link = models.CharField(_("gazeta link"), max_length=255, blank=True)
61 culturepl_link = models.CharField(_("culture.pl link"), max_length=255, blank=True)
62 plwiki = models.CharField(blank=True, max_length=255)
63 photo = models.ImageField(blank=True, null=True, upload_to='catalogue/author/')
64 photo_source = models.CharField(blank=True, max_length=255)
65 photo_attribution = models.CharField(max_length=255, blank=True)
67 description = models.TextField(_("description"), blank=True, help_text=_('for publication'))
69 priority = models.PositiveSmallIntegerField(
71 default=0, choices=[(0, _("Low")), (1, _("Medium")), (2, _("High"))]
73 collections = models.ManyToManyField("Collection", blank=True, verbose_name=_("collections"))
76 verbose_name = _('author')
77 verbose_name_plural = _('authors')
78 ordering = ("last_name", "first_name", "year_of_death")
81 first_name = WIKIDATA.GIVEN_NAME
82 last_name = WIKIDATA.LAST_NAME
83 date_of_birth = WIKIDATA.DATE_OF_BIRTH
84 year_of_birth = WIKIDATA.DATE_OF_BIRTH
85 place_of_birth = WIKIDATA.PLACE_OF_BIRTH
86 date_of_death = WIKIDATA.DATE_OF_DEATH
87 year_of_death = WIKIDATA.DATE_OF_DEATH
88 place_of_death = WIKIDATA.PLACE_OF_DEATH
89 gender = WIKIDATA.GENDER
90 notes = WikiMedia.append("description")
92 photo = WikiMedia.download(WIKIDATA.IMAGE)
93 photo_source = WikiMedia.descriptionurl(WIKIDATA.IMAGE)
94 photo_attribution = WikiMedia.attribution(WIKIDATA.IMAGE)
97 if not obj.first_name and not obj.last_name:
98 yield 'first_name', 'label'
101 name = f"{self.first_name} {self.last_name}"
102 if self.year_of_death is not None:
103 name += f' (zm. {self.year_of_death})'
106 def get_absolute_url(self):
107 return reverse("catalogue_author", args=[self.slug])
111 return f"{self.last_name}, {self.first_name}"
115 if self.year_of_death:
116 return self.year_of_death + 71
117 elif self.year_of_death == 0:
122 def generate_description(self):
123 t = render_to_string(
124 'catalogue/author_description.html',
129 class NotableBook(OrderableModel):
130 author = models.ForeignKey(Author, models.CASCADE)
131 book = models.ForeignKey('Book', models.CASCADE)
134 class Category(WikidataModel):
135 name = models.CharField(_("name"), max_length=255)
136 slug = models.SlugField(max_length=255, unique=True)
145 class Epoch(Category):
146 adjective_feminine_singular = models.CharField(
147 'przymiotnik pojedynczy żeński', max_length=255, blank=True,
148 help_text='twórczość … Adama Mickiewicza'
150 adjective_nonmasculine_plural = models.CharField(
151 'przymiotnik mnogi niemęskoosobowy', max_length=255, blank=True,
152 help_text='utwory … Adama Mickiewicza'
156 verbose_name = _('epoch')
157 verbose_name_plural = _('epochs')
160 class Genre(Category):
161 plural = models.CharField(
162 'liczba mnoga', max_length=255, blank=True,
163 help_text='dotyczy gatunków'
165 is_epoch_specific = models.BooleanField(
167 help_text='Po wskazaniu tego gatunku, dodanie epoki byłoby nadmiarowe, np. „dramat romantyczny”'
171 verbose_name = _('genre')
172 verbose_name_plural = _('genres')
175 class Kind(Category):
176 collective_noun = models.CharField(
177 'określenie zbiorowe', max_length=255, blank=True,
178 help_text='np. „Liryka” albo „Twórczość dramatyczna”'
182 verbose_name = _('kind')
183 verbose_name_plural = _('kinds')
186 class Book(WikidataModel):
187 slug = models.SlugField(max_length=255, blank=True, null=True, unique=True)
188 authors = models.ManyToManyField(Author, blank=True, verbose_name=_("authors"))
189 translators = models.ManyToManyField(
191 related_name="translated_book_set",
192 related_query_name="translated_book",
194 verbose_name=_("translators")
196 epochs = models.ManyToManyField(Epoch, blank=True, verbose_name=_("epochs"))
197 kinds = models.ManyToManyField(Kind, blank=True, verbose_name=_("kinds"))
198 genres = models.ManyToManyField(Genre, blank=True, verbose_name=_("genres"))
199 title = models.CharField(_("title"), max_length=255, blank=True)
200 language = models.CharField(_("language"), max_length=255, blank=True)
201 based_on = models.ForeignKey(
202 "self", models.PROTECT, related_name="translation", null=True, blank=True,
203 verbose_name=_("based on")
205 scans_source = models.CharField(_("scans source"), max_length=255, blank=True)
206 text_source = models.CharField(_("text source"), max_length=255, blank=True)
207 notes = models.TextField(_("notes"), blank=True, help_text=_('private'))
208 priority = models.PositiveSmallIntegerField(
210 default=0, choices=[(0, _("Low")), (1, _("Medium")), (2, _("High"))]
212 original_year = models.IntegerField(_('original publication year'), null=True, blank=True)
213 pd_year = models.IntegerField(_('year of entry into PD'), null=True, blank=True)
214 gazeta_link = models.CharField(_("gazeta link"), max_length=255, blank=True)
215 collections = models.ManyToManyField("Collection", blank=True, verbose_name=_("collections"))
217 estimated_chars = models.IntegerField(_("estimated number of characters"), null=True, blank=True)
218 estimated_verses = models.IntegerField(_("estimated number of verses"), null=True, blank=True)
219 estimate_source = models.CharField(_("source of estimates"), max_length=2048, blank=True)
221 free_license = models.BooleanField(_('free license'), default=False)
222 polona_missing = models.BooleanField(_('missing on Polona'), default=False)
225 ordering = ("title",)
226 verbose_name = _('book')
227 verbose_name_plural = _('books')
230 authors = WIKIDATA.AUTHOR
231 translators = WIKIDATA.TRANSLATOR
232 title = WIKIDATA.TITLE
233 language = WIKIDATA.LANGUAGE
234 based_on = WIKIDATA.BASED_ON
235 original_year = WIKIDATA.PUBLICATION_DATE
236 notes = WikiMedia.append("description")
240 if self.original_year:
241 txt = f"{txt} ({self.original_year})"
242 astr = self.authors_str()
244 txt = f"{txt}, {astr}"
245 tstr = self.translators_str()
247 txt = f"{txt}, tłum. {tstr}"
250 def get_absolute_url(self):
251 return reverse("catalogue_book", args=[self.slug])
255 return f'https://wolnelektury.pl/katalog/lektura/{self.slug}/'
257 def authors_str(self):
260 return ", ".join(str(author) for author in self.authors.all())
261 authors_str.admin_order_field = 'authors__last_name'
262 authors_str.short_description = _('Author')
264 def translators_str(self):
267 return ", ".join(str(author) for author in self.translators.all())
268 translators_str.admin_order_field = 'translators__last_name'
269 translators_str.short_description = _('Translator')
271 def authors_first_names(self):
272 return ', '.join(a.first_name for a in self.authors.all())
274 def authors_last_names(self):
275 return ', '.join(a.last_name for a in self.authors.all())
277 def translators_first_names(self):
278 return ', '.join(a.first_name for a in self.translators.all())
280 def translators_last_names(self):
281 return ', '.join(a.last_name for a in self.translators.all())
283 def get_estimated_costs(self):
285 work_type: work_type.calculate(self)
286 for work_type in WorkType.objects.all()
290 class CollectionCategory(models.Model):
291 name = models.CharField(_("name"), max_length=255)
292 parent = models.ForeignKey('self', models.SET_NULL, related_name='children', null=True, blank=True, verbose_name=_("parent"))
293 notes = models.TextField(_("notes"), blank=True, help_text=_('private'))
296 ordering = ('parent__name', 'name')
297 verbose_name = _('collection category')
298 verbose_name_plural = _('collection categories')
302 return f"{self.parent} / {self.name}"
307 class Collection(models.Model):
308 name = models.CharField(_("name"), max_length=255)
309 slug = models.SlugField(max_length=255, unique=True)
310 category = models.ForeignKey(CollectionCategory, models.SET_NULL, null=True, blank=True, verbose_name=_("category"))
311 notes = models.TextField(_("notes"), blank=True, help_text=_('private'))
312 description = models.TextField(_("description"), blank=True)
315 ordering = ('category', 'name')
316 verbose_name = _('collection')
317 verbose_name_plural = _('collections')
321 return f"{self.category} / {self.name}"
325 def get_estimated_costs(self):
327 for book in self.book_set.all():
328 for k, v in book.get_estimated_costs().items():
331 for author in self.author_set.all():
332 for book in author.book_set.all():
333 for k, v in book.get_estimated_costs().items():
335 for book in author.translated_book_set.all():
336 for k, v in book.get_estimated_costs().items():
341 class WorkType(models.Model):
342 name = models.CharField(_("name"), max_length=255)
346 verbose_name = _('work type')
347 verbose_name_plural = _('work types')
349 def get_rate_for(self, book):
350 for workrate in self.workrate_set.all():
351 if workrate.matches(book):
354 def calculate(self, book):
355 workrate = self.get_rate_for(book)
356 if workrate is not None:
357 return workrate.calculate(book)
361 class WorkRate(models.Model):
362 priority = models.IntegerField(_("priority"), default=1)
363 per_normpage = models.DecimalField(_("per normalized page"), decimal_places=2, max_digits=6, null=True, blank=True)
364 per_verse = models.DecimalField(_("per verse"), decimal_places=2, max_digits=6, null=True, blank=True)
365 work_type = models.ForeignKey(WorkType, models.CASCADE, verbose_name=_("work type"))
366 epochs = models.ManyToManyField(Epoch, blank=True, verbose_name=_("epochs"))
367 kinds = models.ManyToManyField(Kind, blank=True, verbose_name=_("kinds"))
368 genres = models.ManyToManyField(Genre, blank=True, verbose_name=_("genres"))
369 collections = models.ManyToManyField(Collection, blank=True, verbose_name=_("collections"))
372 ordering = ('priority',)
373 verbose_name = _('work rate')
374 verbose_name_plural = _('work rates')
376 def matches(self, book):
377 for category in 'epochs', 'kinds', 'genres', 'collections':
378 oneof = getattr(self, category).all()
380 if not set(oneof).intersection(
381 getattr(book, category).all()):
385 def calculate(self, book):
387 if book.estimated_verses:
388 return book.estimated_verses * self.per_verse
389 elif self.per_normpage:
390 if book.estimated_chars:
391 return (decimal.Decimal(book.estimated_chars) / 1800 * self.per_normpage).quantize(decimal.Decimal('1.00'), rounding=decimal.ROUND_HALF_UP)
394 class Place(WikidataModel):
395 name = models.CharField(_('name'), max_length=255, blank=True)
396 locative = models.CharField(_('locative'), max_length=255, blank=True, help_text=_('in…'))
399 verbose_name = _('place')
400 verbose_name_plural = _('places')