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 wikidata.client import Client
8 from .constants import WIKIDATA
9 from .wikidata import WikidataMixin
12 class Author(WikidataMixin, models.Model):
13 slug = models.SlugField(max_length=255, null=True, blank=True, unique=True)
14 first_name = models.CharField(_("first name"), max_length=255, blank=True)
15 last_name = models.CharField(_("last name"), max_length=255, blank=True)
17 name_de = models.CharField(_("name (de)"), max_length=255, blank=True)
18 name_lt = models.CharField(_("name (lt)"), max_length=255, blank=True)
20 gender = models.CharField(_("gender"), max_length=255, blank=True)
21 nationality = models.CharField(_("nationality"), max_length=255, blank=True)
22 year_of_birth = models.SmallIntegerField(_("year of birth"), null=True, blank=True)
23 place_of_birth = models.CharField(_('place of birth'), max_length=255, blank=True)
24 year_of_death = models.SmallIntegerField(_("year of death"), null=True, blank=True)
25 place_of_death = models.CharField(_('place of death'), max_length=255, blank=True)
26 status = models.PositiveSmallIntegerField(
37 notes = models.TextField(_("notes"), blank=True)
38 gazeta_link = models.CharField(_("gazeta link"), max_length=255, blank=True)
39 culturepl_link = models.CharField(_("culture.pl link"), max_length=255, blank=True)
41 description = models.TextField(_("description"), blank=True)
42 description_de = models.TextField(_("description (de)"), blank=True)
43 description_lt = models.TextField(_("description (lt)"), blank=True)
45 priority = models.PositiveSmallIntegerField(
47 default=0, choices=[(0, _("Low")), (1, _("Medium")), (2, _("High"))]
49 collections = models.ManyToManyField("Collection", blank=True, verbose_name=_("collections"))
52 verbose_name = _('author')
53 verbose_name_plural = _('authors')
54 ordering = ("last_name", "first_name", "year_of_death")
57 first_name = WIKIDATA.GIVEN_NAME
58 last_name = WIKIDATA.LAST_NAME
59 year_of_death = WIKIDATA.DATE_OF_DEATH
60 gender = WIKIDATA.GENDER
64 name = f"{self.first_name} {self.last_name}"
65 if self.year_of_death is not None:
66 name += f' (zm. {self.year_of_death})'
69 def get_absolute_url(self):
70 return reverse("catalogue_author", args=[self.slug])
74 return f"{self.last_name}, {self.first_name}"
78 if self.year_of_death:
79 return self.year_of_death + 71
80 elif self.year_of_death == 0:
86 class Category(WikidataMixin, models.Model):
87 name = models.CharField(_("name"), max_length=255)
88 slug = models.SlugField(max_length=255, unique=True)
96 class Epoch(Category):
98 verbose_name = _('epoch')
99 verbose_name_plural = _('epochs')
102 class Genre(Category):
104 verbose_name = _('genre')
105 verbose_name_plural = _('genres')
108 class Kind(Category):
110 verbose_name = _('kind')
111 verbose_name_plural = _('kinds')
114 class Book(WikidataMixin, models.Model):
115 slug = models.SlugField(max_length=255, blank=True, null=True, unique=True)
116 authors = models.ManyToManyField(Author, blank=True, verbose_name=_("authors"))
117 translators = models.ManyToManyField(
119 related_name="translated_book_set",
120 related_query_name="translated_book",
122 verbose_name=_("translators")
124 epochs = models.ManyToManyField(Epoch, blank=True, verbose_name=_("epochs"))
125 kinds = models.ManyToManyField(Kind, blank=True, verbose_name=_("kinds"))
126 genres = models.ManyToManyField(Genre, blank=True, verbose_name=_("genres"))
127 title = models.CharField(_("title"), max_length=255, blank=True)
128 language = models.CharField(_("language"), max_length=255, blank=True)
129 based_on = models.ForeignKey(
130 "self", models.PROTECT, related_name="translation", null=True, blank=True,
131 verbose_name=_("based on")
133 scans_source = models.CharField(_("scans source"), max_length=255, blank=True)
134 text_source = models.CharField(_("text source"), max_length=255, blank=True)
135 notes = models.TextField(_("notes"), blank=True)
136 priority = models.PositiveSmallIntegerField(
138 default=0, choices=[(0, _("Low")), (1, _("Medium")), (2, _("High"))]
140 pd_year = models.IntegerField(_('year of entry into PD'), null=True, blank=True)
141 gazeta_link = models.CharField(_("gazeta link"), max_length=255, blank=True)
142 collections = models.ManyToManyField("Collection", blank=True, verbose_name=_("collections"))
144 estimated_chars = models.IntegerField(_("estimated number of characters"), null=True, blank=True)
145 estimated_verses = models.IntegerField(_("estimated number of verses"), null=True, blank=True)
146 estimate_source = models.CharField(_("source of estimates"), max_length=2048, blank=True)
148 free_license = models.BooleanField(_('free license'), default=False)
149 polona_missing = models.BooleanField(_('missing on Polona'), default=False)
152 ordering = ("title",)
153 verbose_name = _('book')
154 verbose_name_plural = _('books')
157 authors = WIKIDATA.AUTHOR
158 translators = WIKIDATA.TRANSLATOR
159 title = WIKIDATA.TITLE
160 language = WIKIDATA.LANGUAGE
161 based_on = WIKIDATA.BASED_ON
162 notes = "description"
166 astr = self.authors_str()
168 txt = f"{astr} – {txt}"
169 tstr = self.translators_str()
171 txt = f"{txt} (tłum. {tstr})"
174 def get_absolute_url(self):
175 return reverse("catalogue_book", args=[self.slug])
179 return f'https://wolnelektury.pl/katalog/lektura/{self.slug}/'
181 def authors_str(self):
182 return ", ".join(str(author) for author in self.authors.all())
183 authors_str.admin_order_field = 'authors__last_name'
184 authors_str.short_description = _('Author')
186 def translators_str(self):
187 return ", ".join(str(author) for author in self.translators.all())
188 translators_str.admin_order_field = 'translators__last_name'
189 translators_str.short_description = _('Translator')
191 def get_estimated_costs(self):
193 work_type: work_type.calculate(self)
194 for work_type in WorkType.objects.all()
198 class CollectionCategory(models.Model):
199 name = models.CharField(_("name"), max_length=255)
200 parent = models.ForeignKey('self', models.SET_NULL, related_name='children', null=True, blank=True, verbose_name=_("parent"))
201 notes = models.TextField(_("notes"), blank=True)
204 ordering = ('parent__name', 'name')
205 verbose_name = _('collection category')
206 verbose_name_plural = _('collection categories')
210 return f"{self.parent} / {self.name}"
215 class Collection(models.Model):
216 name = models.CharField(_("name"), max_length=255)
217 slug = models.SlugField(max_length=255, unique=True)
218 category = models.ForeignKey(CollectionCategory, models.SET_NULL, null=True, blank=True, verbose_name=_("category"))
219 notes = models.TextField(_("notes"), blank=True)
222 ordering = ('category', 'name')
223 verbose_name = _('collection')
224 verbose_name_plural = _('collections')
228 return f"{self.category} / {self.name}"
232 def get_estimated_costs(self):
234 for book in self.book_set.all():
235 for k, v in book.get_estimated_costs().items():
238 for author in self.author_set.all():
239 for book in author.book_set.all():
240 for k, v in book.get_estimated_costs().items():
242 for book in author.translated_book_set.all():
243 for k, v in book.get_estimated_costs().items():
248 class WorkType(models.Model):
249 name = models.CharField(_("name"), max_length=255)
253 verbose_name = _('work type')
254 verbose_name_plural = _('work types')
256 def get_rate_for(self, book):
257 for workrate in self.workrate_set.all():
258 if workrate.matches(book):
261 def calculate(self, book):
262 workrate = self.get_rate_for(book)
263 if workrate is not None:
264 return workrate.calculate(book)
268 class WorkRate(models.Model):
269 priority = models.IntegerField(_("priority"), default=1)
270 per_normpage = models.DecimalField(_("per normalized page"), decimal_places=2, max_digits=6, null=True, blank=True)
271 per_verse = models.DecimalField(_("per verse"), decimal_places=2, max_digits=6, null=True, blank=True)
272 work_type = models.ForeignKey(WorkType, models.CASCADE, verbose_name=_("work type"))
273 epochs = models.ManyToManyField(Epoch, blank=True, verbose_name=_("epochs"))
274 kinds = models.ManyToManyField(Kind, blank=True, verbose_name=_("kinds"))
275 genres = models.ManyToManyField(Genre, blank=True, verbose_name=_("genres"))
276 collections = models.ManyToManyField(Collection, blank=True, verbose_name=_("collections"))
279 ordering = ('priority',)
280 verbose_name = _('work rate')
281 verbose_name_plural = _('work rates')
283 def matches(self, book):
284 for category in 'epochs', 'kinds', 'genres', 'collections':
285 oneof = getattr(self, category).all()
287 if not set(oneof).intersection(
288 getattr(book, category).all()):
292 def calculate(self, book):
294 if book.estimated_verses:
295 return book.estimated_verses * self.per_verse
296 elif self.per_normpage:
297 if book.estimated_chars:
298 return (decimal.Decimal(book.estimated_chars) / 1800 * self.per_normpage).quantize(decimal.Decimal('1.00'), rounding=decimal.ROUND_HALF_UP)