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 .utils import UnrelatedManager
10 from .wikidata import WikidataMixin
13 class Author(WikidataMixin, models.Model):
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_death = models.SmallIntegerField(_("year of death"), null=True, blank=True)
24 status = models.PositiveSmallIntegerField(
35 notes = models.TextField(_("notes"), blank=True)
36 gazeta_link = models.CharField(_("gazeta link"), max_length=255, blank=True)
37 culturepl_link = models.CharField(_("culture.pl link"), max_length=255, blank=True)
39 description = models.TextField(_("description"), blank=True)
40 description_de = models.TextField(_("description (de)"), blank=True)
41 description_lt = models.TextField(_("description (lt)"), blank=True)
43 priority = models.PositiveSmallIntegerField(
45 default=0, choices=[(0, _("Low")), (1, _("Medium")), (2, _("High"))]
47 collections = models.ManyToManyField("Collection", blank=True, verbose_name=_("collections"))
50 verbose_name = _('author')
51 verbose_name_plural = _('authors')
52 ordering = ("last_name", "first_name", "year_of_death")
55 first_name = WIKIDATA.GIVEN_NAME
56 last_name = WIKIDATA.LAST_NAME
57 year_of_death = WIKIDATA.DATE_OF_DEATH
58 gender = WIKIDATA.GENDER
62 return f"{self.first_name} {self.last_name}"
64 def get_absolute_url(self):
65 return reverse("catalogue_author", args=[self.slug])
69 if self.year_of_death:
70 return self.year_of_death + 71
71 elif self.year_of_death == 0:
77 class Category(WikidataMixin, models.Model):
78 name = models.CharField(_("name"), max_length=255)
79 slug = models.SlugField(max_length=255, unique=True)
87 class Epoch(Category):
89 verbose_name = _('epoch')
90 verbose_name_plural = _('epochs')
93 class Genre(Category):
95 verbose_name = _('genre')
96 verbose_name_plural = _('genres')
101 verbose_name = _('kind')
102 verbose_name_plural = _('kinds')
105 class Book(WikidataMixin, models.Model):
106 slug = models.SlugField(max_length=255, blank=True, null=True, unique=True)
107 authors = models.ManyToManyField(Author, blank=True, verbose_name=_("authors"))
108 translators = models.ManyToManyField(
110 related_name="translated_book_set",
111 related_query_name="translated_book",
113 verbose_name=_("translators")
115 epochs = models.ManyToManyField(Epoch, blank=True, verbose_name=_("epochs"))
116 kinds = models.ManyToManyField(Kind, blank=True, verbose_name=_("kinds"))
117 genres = models.ManyToManyField(Genre, blank=True, verbose_name=_("genres"))
118 title = models.CharField(_("title"), max_length=255, blank=True)
119 language = models.CharField(_("language"), max_length=255, blank=True)
120 based_on = models.ForeignKey(
121 "self", models.PROTECT, related_name="translation", null=True, blank=True,
122 verbose_name=_("based on")
124 scans_source = models.CharField(_("scans source"), max_length=255, blank=True)
125 text_source = models.CharField(_("text source"), max_length=255, blank=True)
126 notes = models.TextField(_("notes"), blank=True)
127 priority = models.PositiveSmallIntegerField(
129 default=0, choices=[(0, _("Low")), (1, _("Medium")), (2, _("High"))]
131 pd_year = models.IntegerField(_('year of entry into PD'), null=True, blank=True)
132 gazeta_link = models.CharField(_("gazeta link"), max_length=255, blank=True)
133 collections = models.ManyToManyField("Collection", blank=True, verbose_name=_("collections"))
135 estimated_chars = models.IntegerField(_("estimated number of characters"), null=True, blank=True)
136 estimated_verses = models.IntegerField(_("estimated number of verses"), null=True, blank=True)
137 estimate_source = models.CharField(_("source of estimates"), max_length=2048, blank=True)
139 objects = UnrelatedManager()
142 ordering = ("title",)
143 verbose_name = _('book')
144 verbose_name_plural = _('books')
147 authors = WIKIDATA.AUTHOR
148 translators = WIKIDATA.TRANSLATOR
149 title = WIKIDATA.TITLE
150 language = WIKIDATA.LANGUAGE
151 based_on = WIKIDATA.BASED_ON
152 notes = "description"
156 astr = self.authors_str()
158 txt = f"{astr} – {txt}"
159 tstr = self.translators_str()
161 txt = f"{txt} (tłum. {tstr})"
164 def get_absolute_url(self):
165 return reverse("catalogue_book", args=[self.slug])
167 def authors_str(self):
168 return ", ".join(str(author) for author in self.authors.all())
170 def translators_str(self):
171 return ", ".join(str(author) for author in self.translators.all())
173 def get_document_books(self):
174 DBook = apps.get_model("documents", "Book")
175 return DBook.objects.filter(dc_slug=self.slug)
177 def get_estimated_costs(self):
179 work_type: work_type.calculate(self)
180 for work_type in WorkType.objects.all()
184 class CollectionCategory(models.Model):
185 name = models.CharField(_("name"), max_length=255)
186 parent = models.ForeignKey('self', models.SET_NULL, related_name='children', null=True, blank=True, verbose_name=_("parent"))
189 ordering = ('parent__name', 'name')
190 verbose_name = _('collection category')
191 verbose_name_plural = _('collection categories')
195 return f"{self.parent} / {self.name}"
200 class Collection(models.Model):
201 name = models.CharField(_("name"), max_length=255)
202 slug = models.SlugField(max_length=255, unique=True)
203 category = models.ForeignKey(CollectionCategory, models.SET_NULL, null=True, blank=True, verbose_name=_("category"))
206 ordering = ('category', 'name')
207 verbose_name = _('collection')
208 verbose_name_plural = _('collections')
212 return f"{self.category} / {self.name}"
216 def get_estimated_costs(self):
218 for book in self.book_set.all():
219 for k, v in book.get_estimated_costs().items():
222 for author in self.author_set.all():
223 for book in author.book_set.all():
224 for k, v in book.get_estimated_costs().items():
226 for book in author.translated_book_set.all():
227 for k, v in book.get_estimated_costs().items():
232 class WorkType(models.Model):
233 name = models.CharField(_("name"), max_length=255)
237 verbose_name = _('work type')
238 verbose_name_plural = _('work types')
240 def get_rate_for(self, book):
241 for workrate in self.workrate_set.all():
242 if workrate.matches(book):
245 def calculate(self, book):
246 workrate = self.get_rate_for(book)
247 if workrate is not None:
248 return workrate.calculate(book)
252 class WorkRate(models.Model):
253 priority = models.IntegerField(_("priority"), default=1)
254 per_normpage = models.DecimalField(_("per normalized page"), decimal_places=2, max_digits=6, null=True, blank=True)
255 per_verse = models.DecimalField(_("per verse"), decimal_places=2, max_digits=6, null=True, blank=True)
256 work_type = models.ForeignKey(WorkType, models.CASCADE, verbose_name=_("work type"))
257 epochs = models.ManyToManyField(Epoch, blank=True, verbose_name=_("epochs"))
258 kinds = models.ManyToManyField(Kind, blank=True, verbose_name=_("kinds"))
259 genres = models.ManyToManyField(Genre, blank=True, verbose_name=_("genres"))
260 collections = models.ManyToManyField(Collection, blank=True, verbose_name=_("collections"))
263 ordering = ('priority',)
264 verbose_name = _('work rate')
265 verbose_name_plural = _('work rates')
267 def matches(self, book):
268 for category in 'epochs', 'kinds', 'genres', 'collections':
269 oneof = getattr(self, category).all()
271 if not set(oneof).intersection(
272 getattr(book, category).all()):
276 def calculate(self, book):
278 if book.estimated_verses:
279 return book.estimated_verses * self.per_verse
280 elif self.per_normpage:
281 if book.estimated_chars:
282 return (decimal.Decimal(book.estimated_chars) / 1800 * self.per_normpage).quantize(decimal.Decimal('1.00'), rounding=decimal.ROUND_HALF_UP)