1 from collections import Counter
3 from django.apps import apps
4 from django.db import models
5 from django.urls import reverse
6 from django.utils.translation import gettext_lazy as _
7 from admin_ordering.models import OrderableModel
8 from wikidata.client import Client
9 from .constants import WIKIDATA
10 from .wikidata import WikidataModel
13 class Author(WikidataModel):
14 slug = models.SlugField(max_length=255, null=True, blank=True, unique=True)
15 first_name = models.CharField(_("first name"), max_length=255, blank=True)
16 last_name = models.CharField(_("last name"), max_length=255, blank=True)
18 name_de = models.CharField(_("name (de)"), max_length=255, blank=True)
19 name_lt = models.CharField(_("name (lt)"), max_length=255, blank=True)
21 gender = models.CharField(_("gender"), max_length=255, blank=True)
22 nationality = models.CharField(_("nationality"), max_length=255, blank=True)
23 year_of_birth = models.SmallIntegerField(_("year of birth"), null=True, blank=True)
24 year_of_birth_inexact = models.BooleanField(_("inexact"), default=False)
25 year_of_birth_range = models.SmallIntegerField(_("year of birth, range end"), null=True, blank=True)
26 date_of_birth = models.DateField(_("date_of_birth"), null=True, blank=True)
27 place_of_birth = models.ForeignKey(
28 'Place', models.PROTECT, null=True, blank=True,
29 verbose_name=_('place of birth'),
30 related_name='authors_born'
32 year_of_death = models.SmallIntegerField(_("year of death"), null=True, blank=True)
33 year_of_death_inexact = models.BooleanField(_("inexact"), default=False)
34 year_of_death_range = models.SmallIntegerField(_("year of death, range end"), null=True, blank=True)
35 date_of_death = models.DateField(_("date_of_death"), null=True, blank=True)
36 place_of_death = models.ForeignKey(
37 'Place', models.PROTECT, null=True, blank=True,
38 verbose_name=_('place of death'),
39 related_name='authors_died'
41 status = models.PositiveSmallIntegerField(
52 notes = models.TextField(_("notes"), blank=True)
53 gazeta_link = models.CharField(_("gazeta link"), max_length=255, blank=True)
54 culturepl_link = models.CharField(_("culture.pl link"), max_length=255, blank=True)
56 description = models.TextField(_("description"), blank=True)
58 priority = models.PositiveSmallIntegerField(
60 default=0, choices=[(0, _("Low")), (1, _("Medium")), (2, _("High"))]
62 collections = models.ManyToManyField("Collection", blank=True, verbose_name=_("collections"))
65 verbose_name = _('author')
66 verbose_name_plural = _('authors')
67 ordering = ("last_name", "first_name", "year_of_death")
70 first_name = WIKIDATA.GIVEN_NAME
71 last_name = WIKIDATA.LAST_NAME
72 date_of_birth = WIKIDATA.DATE_OF_BIRTH
73 year_of_birth = WIKIDATA.DATE_OF_BIRTH
74 place_of_birth = WIKIDATA.PLACE_OF_BIRTH
75 date_of_death = WIKIDATA.DATE_OF_DEATH
76 year_of_death = WIKIDATA.DATE_OF_DEATH
77 place_of_death = WIKIDATA.PLACE_OF_DEATH
78 gender = WIKIDATA.GENDER
82 if not obj.first_name and not obj.last_name:
83 yield 'first_name', 'label'
86 name = f"{self.first_name} {self.last_name}"
87 if self.year_of_death is not None:
88 name += f' (zm. {self.year_of_death})'
91 def get_absolute_url(self):
92 return reverse("catalogue_author", args=[self.slug])
96 return f"{self.last_name}, {self.first_name}"
100 if self.year_of_death:
101 return self.year_of_death + 71
102 elif self.year_of_death == 0:
108 class NotableBook(OrderableModel):
109 author = models.ForeignKey(Author, models.CASCADE)
110 book = models.ForeignKey('Book', models.CASCADE)
113 class Category(WikidataModel):
114 name = models.CharField(_("name"), max_length=255)
115 slug = models.SlugField(max_length=255, unique=True)
123 class Epoch(Category):
125 verbose_name = _('epoch')
126 verbose_name_plural = _('epochs')
129 class Genre(Category):
131 verbose_name = _('genre')
132 verbose_name_plural = _('genres')
135 class Kind(Category):
137 verbose_name = _('kind')
138 verbose_name_plural = _('kinds')
141 class Book(WikidataModel):
142 slug = models.SlugField(max_length=255, blank=True, null=True, unique=True)
143 authors = models.ManyToManyField(Author, blank=True, verbose_name=_("authors"))
144 translators = models.ManyToManyField(
146 related_name="translated_book_set",
147 related_query_name="translated_book",
149 verbose_name=_("translators")
151 epochs = models.ManyToManyField(Epoch, blank=True, verbose_name=_("epochs"))
152 kinds = models.ManyToManyField(Kind, blank=True, verbose_name=_("kinds"))
153 genres = models.ManyToManyField(Genre, blank=True, verbose_name=_("genres"))
154 title = models.CharField(_("title"), max_length=255, blank=True)
155 language = models.CharField(_("language"), max_length=255, blank=True)
156 based_on = models.ForeignKey(
157 "self", models.PROTECT, related_name="translation", null=True, blank=True,
158 verbose_name=_("based on")
160 scans_source = models.CharField(_("scans source"), max_length=255, blank=True)
161 text_source = models.CharField(_("text source"), max_length=255, blank=True)
162 notes = models.TextField(_("notes"), blank=True)
163 priority = models.PositiveSmallIntegerField(
165 default=0, choices=[(0, _("Low")), (1, _("Medium")), (2, _("High"))]
167 pd_year = models.IntegerField(_('year of entry into PD'), null=True, blank=True)
168 gazeta_link = models.CharField(_("gazeta link"), max_length=255, blank=True)
169 collections = models.ManyToManyField("Collection", blank=True, verbose_name=_("collections"))
171 estimated_chars = models.IntegerField(_("estimated number of characters"), null=True, blank=True)
172 estimated_verses = models.IntegerField(_("estimated number of verses"), null=True, blank=True)
173 estimate_source = models.CharField(_("source of estimates"), max_length=2048, blank=True)
175 free_license = models.BooleanField(_('free license'), default=False)
176 polona_missing = models.BooleanField(_('missing on Polona'), default=False)
179 ordering = ("title",)
180 verbose_name = _('book')
181 verbose_name_plural = _('books')
184 authors = WIKIDATA.AUTHOR
185 translators = WIKIDATA.TRANSLATOR
186 title = WIKIDATA.TITLE
187 language = WIKIDATA.LANGUAGE
188 based_on = WIKIDATA.BASED_ON
189 notes = "description"
193 astr = self.authors_str()
195 txt = f"{txt}, {astr}"
196 tstr = self.translators_str()
198 txt = f"{txt}, tłum. {tstr}"
201 def get_absolute_url(self):
202 return reverse("catalogue_book", args=[self.slug])
206 return f'https://wolnelektury.pl/katalog/lektura/{self.slug}/'
208 def authors_str(self):
209 return ", ".join(str(author) for author in self.authors.all())
210 authors_str.admin_order_field = 'authors__last_name'
211 authors_str.short_description = _('Author')
213 def translators_str(self):
214 return ", ".join(str(author) for author in self.translators.all())
215 translators_str.admin_order_field = 'translators__last_name'
216 translators_str.short_description = _('Translator')
218 def get_estimated_costs(self):
220 work_type: work_type.calculate(self)
221 for work_type in WorkType.objects.all()
225 class CollectionCategory(models.Model):
226 name = models.CharField(_("name"), max_length=255)
227 parent = models.ForeignKey('self', models.SET_NULL, related_name='children', null=True, blank=True, verbose_name=_("parent"))
228 notes = models.TextField(_("notes"), blank=True)
231 ordering = ('parent__name', 'name')
232 verbose_name = _('collection category')
233 verbose_name_plural = _('collection categories')
237 return f"{self.parent} / {self.name}"
242 class Collection(models.Model):
243 name = models.CharField(_("name"), max_length=255)
244 slug = models.SlugField(max_length=255, unique=True)
245 category = models.ForeignKey(CollectionCategory, models.SET_NULL, null=True, blank=True, verbose_name=_("category"))
246 notes = models.TextField(_("notes"), blank=True)
249 ordering = ('category', 'name')
250 verbose_name = _('collection')
251 verbose_name_plural = _('collections')
255 return f"{self.category} / {self.name}"
259 def get_estimated_costs(self):
261 for book in self.book_set.all():
262 for k, v in book.get_estimated_costs().items():
265 for author in self.author_set.all():
266 for book in author.book_set.all():
267 for k, v in book.get_estimated_costs().items():
269 for book in author.translated_book_set.all():
270 for k, v in book.get_estimated_costs().items():
275 class WorkType(models.Model):
276 name = models.CharField(_("name"), max_length=255)
280 verbose_name = _('work type')
281 verbose_name_plural = _('work types')
283 def get_rate_for(self, book):
284 for workrate in self.workrate_set.all():
285 if workrate.matches(book):
288 def calculate(self, book):
289 workrate = self.get_rate_for(book)
290 if workrate is not None:
291 return workrate.calculate(book)
295 class WorkRate(models.Model):
296 priority = models.IntegerField(_("priority"), default=1)
297 per_normpage = models.DecimalField(_("per normalized page"), decimal_places=2, max_digits=6, null=True, blank=True)
298 per_verse = models.DecimalField(_("per verse"), decimal_places=2, max_digits=6, null=True, blank=True)
299 work_type = models.ForeignKey(WorkType, models.CASCADE, verbose_name=_("work type"))
300 epochs = models.ManyToManyField(Epoch, blank=True, verbose_name=_("epochs"))
301 kinds = models.ManyToManyField(Kind, blank=True, verbose_name=_("kinds"))
302 genres = models.ManyToManyField(Genre, blank=True, verbose_name=_("genres"))
303 collections = models.ManyToManyField(Collection, blank=True, verbose_name=_("collections"))
306 ordering = ('priority',)
307 verbose_name = _('work rate')
308 verbose_name_plural = _('work rates')
310 def matches(self, book):
311 for category in 'epochs', 'kinds', 'genres', 'collections':
312 oneof = getattr(self, category).all()
314 if not set(oneof).intersection(
315 getattr(book, category).all()):
319 def calculate(self, book):
321 if book.estimated_verses:
322 return book.estimated_verses * self.per_verse
323 elif self.per_normpage:
324 if book.estimated_chars:
325 return (decimal.Decimal(book.estimated_chars) / 1800 * self.per_normpage).quantize(decimal.Decimal('1.00'), rounding=decimal.ROUND_HALF_UP)
328 class Place(WikidataModel):
329 name = models.CharField(_('name'), max_length=255, blank=True)
330 locative = models.CharField(_('locative'), max_length=255, blank=True, help_text=_('in…'))
333 verbose_name = _('place')
334 verbose_name_plural = _('places')