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_death = models.SmallIntegerField(_("year of death"), null=True, blank=True)
23 status = models.PositiveSmallIntegerField(
34 notes = models.TextField(_("notes"), blank=True)
35 gazeta_link = models.CharField(_("gazeta link"), max_length=255, blank=True)
36 culturepl_link = models.CharField(_("culture.pl link"), max_length=255, blank=True)
38 description = models.TextField(_("description"), blank=True)
39 description_de = models.TextField(_("description (de)"), blank=True)
40 description_lt = models.TextField(_("description (lt)"), blank=True)
42 priority = models.PositiveSmallIntegerField(
44 default=0, choices=[(0, _("Low")), (1, _("Medium")), (2, _("High"))]
46 collections = models.ManyToManyField("Collection", blank=True, verbose_name=_("collections"))
49 verbose_name = _('author')
50 verbose_name_plural = _('authors')
51 ordering = ("last_name", "first_name", "year_of_death")
54 first_name = WIKIDATA.GIVEN_NAME
55 last_name = WIKIDATA.LAST_NAME
56 year_of_death = WIKIDATA.DATE_OF_DEATH
57 gender = WIKIDATA.GENDER
61 return f"{self.first_name} {self.last_name}"
63 def get_absolute_url(self):
64 return reverse("catalogue_author", args=[self.slug])
68 if self.year_of_death:
69 return self.year_of_death + 71
70 elif self.year_of_death == 0:
76 class Category(WikidataMixin, models.Model):
77 name = models.CharField(_("name"), max_length=255)
78 slug = models.SlugField(max_length=255, unique=True)
86 class Epoch(Category):
88 verbose_name = _('epoch')
89 verbose_name_plural = _('epochs')
92 class Genre(Category):
94 verbose_name = _('genre')
95 verbose_name_plural = _('genres')
100 verbose_name = _('kind')
101 verbose_name_plural = _('kinds')
104 class Book(WikidataMixin, models.Model):
105 slug = models.SlugField(max_length=255, blank=True, null=True, unique=True)
106 authors = models.ManyToManyField(Author, blank=True, verbose_name=_("authors"))
107 translators = models.ManyToManyField(
109 related_name="translated_book_set",
110 related_query_name="translated_book",
112 verbose_name=_("translators")
114 epochs = models.ManyToManyField(Epoch, blank=True, verbose_name=_("epochs"))
115 kinds = models.ManyToManyField(Kind, blank=True, verbose_name=_("kinds"))
116 genres = models.ManyToManyField(Genre, blank=True, verbose_name=_("genres"))
117 title = models.CharField(_("title"), max_length=255, blank=True)
118 language = models.CharField(_("language"), max_length=255, blank=True)
119 based_on = models.ForeignKey(
120 "self", models.PROTECT, related_name="translation", null=True, blank=True,
121 verbose_name=_("based on")
123 scans_source = models.CharField(_("scans source"), max_length=255, blank=True)
124 text_source = models.CharField(_("text source"), max_length=255, blank=True)
125 notes = models.TextField(_("notes"), blank=True)
126 priority = models.PositiveSmallIntegerField(
128 default=0, choices=[(0, _("Low")), (1, _("Medium")), (2, _("High"))]
130 pd_year = models.IntegerField(_('year of entry into PD'), null=True, blank=True)
131 gazeta_link = models.CharField(_("gazeta link"), max_length=255, blank=True)
132 collections = models.ManyToManyField("Collection", blank=True, verbose_name=_("collections"))
134 estimated_chars = models.IntegerField(_("estimated number of characters"), null=True, blank=True)
135 estimated_verses = models.IntegerField(_("estimated number of verses"), null=True, blank=True)
136 estimate_source = models.CharField(_("source of estimates"), max_length=2048, blank=True)
139 ordering = ("title",)
140 verbose_name = _('book')
141 verbose_name_plural = _('books')
144 authors = WIKIDATA.AUTHOR
145 translators = WIKIDATA.TRANSLATOR
146 title = WIKIDATA.TITLE
147 language = WIKIDATA.LANGUAGE
148 based_on = WIKIDATA.BASED_ON
149 notes = "description"
153 astr = self.authors_str()
155 txt = f"{astr} – {txt}"
156 tstr = self.translators_str()
158 txt = f"{txt} (tłum. {tstr})"
161 def get_absolute_url(self):
162 return reverse("catalogue_book", args=[self.slug])
164 def authors_str(self):
165 return ", ".join(str(author) for author in self.authors.all())
166 authors_str.admin_order_field = 'authors__last_name'
167 authors_str.short_description = _('Author')
169 def translators_str(self):
170 return ", ".join(str(author) for author in self.translators.all())
171 translators_str.admin_order_field = 'translators__last_name'
172 translators_str.short_description = _('Translator')
174 def get_estimated_costs(self):
176 work_type: work_type.calculate(self)
177 for work_type in WorkType.objects.all()
181 class CollectionCategory(models.Model):
182 name = models.CharField(_("name"), max_length=255)
183 parent = models.ForeignKey('self', models.SET_NULL, related_name='children', null=True, blank=True, verbose_name=_("parent"))
184 notes = models.TextField(_("notes"), blank=True)
187 ordering = ('parent__name', 'name')
188 verbose_name = _('collection category')
189 verbose_name_plural = _('collection categories')
193 return f"{self.parent} / {self.name}"
198 class Collection(models.Model):
199 name = models.CharField(_("name"), max_length=255)
200 slug = models.SlugField(max_length=255, unique=True)
201 category = models.ForeignKey(CollectionCategory, models.SET_NULL, null=True, blank=True, verbose_name=_("category"))
202 notes = models.TextField(_("notes"), blank=True)
205 ordering = ('category', 'name')
206 verbose_name = _('collection')
207 verbose_name_plural = _('collections')
211 return f"{self.category} / {self.name}"
215 def get_estimated_costs(self):
217 for book in self.book_set.all():
218 for k, v in book.get_estimated_costs().items():
221 for author in self.author_set.all():
222 for book in author.book_set.all():
223 for k, v in book.get_estimated_costs().items():
225 for book in author.translated_book_set.all():
226 for k, v in book.get_estimated_costs().items():
231 class WorkType(models.Model):
232 name = models.CharField(_("name"), max_length=255)
236 verbose_name = _('work type')
237 verbose_name_plural = _('work types')
239 def get_rate_for(self, book):
240 for workrate in self.workrate_set.all():
241 if workrate.matches(book):
244 def calculate(self, book):
245 workrate = self.get_rate_for(book)
246 if workrate is not None:
247 return workrate.calculate(book)
251 class WorkRate(models.Model):
252 priority = models.IntegerField(_("priority"), default=1)
253 per_normpage = models.DecimalField(_("per normalized page"), decimal_places=2, max_digits=6, null=True, blank=True)
254 per_verse = models.DecimalField(_("per verse"), decimal_places=2, max_digits=6, null=True, blank=True)
255 work_type = models.ForeignKey(WorkType, models.CASCADE, verbose_name=_("work type"))
256 epochs = models.ManyToManyField(Epoch, blank=True, verbose_name=_("epochs"))
257 kinds = models.ManyToManyField(Kind, blank=True, verbose_name=_("kinds"))
258 genres = models.ManyToManyField(Genre, blank=True, verbose_name=_("genres"))
259 collections = models.ManyToManyField(Collection, blank=True, verbose_name=_("collections"))
262 ordering = ('priority',)
263 verbose_name = _('work rate')
264 verbose_name_plural = _('work rates')
266 def matches(self, book):
267 for category in 'epochs', 'kinds', 'genres', 'collections':
268 oneof = getattr(self, category).all()
270 if not set(oneof).intersection(
271 getattr(book, category).all()):
275 def calculate(self, book):
277 if book.estimated_verses:
278 return book.estimated_verses * self.per_verse
279 elif self.per_normpage:
280 if book.estimated_chars:
281 return (decimal.Decimal(book.estimated_chars) / 1800 * self.per_normpage).quantize(decimal.Decimal('1.00'), rounding=decimal.ROUND_HALF_UP)