2 from django.apps import apps
3 from django.db import models
4 from django.urls import reverse
5 from django.utils.translation import gettext_lazy as _
6 from wikidata.client import Client
7 from .constants import WIKIDATA
8 from .utils import UnrelatedManager
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(max_length=255, blank=True)
15 last_name = models.CharField(max_length=255, blank=True)
17 name_de = models.CharField(max_length=255, blank=True)
18 name_lt = models.CharField(max_length=255, blank=True)
20 gender = models.CharField(max_length=255, blank=True)
21 nationality = models.CharField(max_length=255, blank=True)
22 year_of_death = models.SmallIntegerField(null=True, blank=True)
23 status = models.PositiveSmallIntegerField(
33 notes = models.TextField(blank=True)
34 gazeta_link = models.CharField(max_length=255, blank=True)
35 culturepl_link = models.CharField(max_length=255, blank=True)
37 description = models.TextField(blank=True)
38 description_de = models.TextField(blank=True)
39 description_lt = models.TextField(blank=True)
41 priority = models.PositiveSmallIntegerField(
42 default=0, choices=[(0, _("Low")), (1, _("Medium")), (2, _("High"))]
44 collections = models.ManyToManyField("Collection", blank=True)
47 verbose_name = _('author')
48 verbose_name_plural = _('authors')
49 ordering = ("last_name", "first_name", "year_of_death")
52 first_name = WIKIDATA.GIVEN_NAME
53 last_name = WIKIDATA.LAST_NAME
54 year_of_death = WIKIDATA.DATE_OF_DEATH
55 gender = WIKIDATA.GENDER
59 return f"{self.first_name} {self.last_name}"
61 def get_absolute_url(self):
62 return reverse("catalogue_author", args=[self.slug])
66 if self.year_of_death:
67 return self.year_of_death + 71
68 elif self.year_of_death == 0:
74 class Category(WikidataMixin, models.Model):
75 name = models.CharField(max_length=255)
76 slug = models.SlugField(max_length=255, unique=True)
84 class Epoch(Category):
86 verbose_name = _('epoch')
87 verbose_name_plural = _('epochs')
90 class Genre(Category):
92 verbose_name = _('genre')
93 verbose_name_plural = _('genres')
98 verbose_name = _('kind')
99 verbose_name_plural = _('kinds')
102 class Book(WikidataMixin, models.Model):
103 slug = models.SlugField(max_length=255, blank=True, null=True, unique=True)
104 authors = models.ManyToManyField(Author, blank=True)
105 translators = models.ManyToManyField(
107 related_name="translated_book_set",
108 related_query_name="translated_book",
111 epochs = models.ManyToManyField(Epoch, blank=True)
112 kinds = models.ManyToManyField(Kind, blank=True)
113 genres = models.ManyToManyField(Genre, blank=True)
114 title = models.CharField(max_length=255, blank=True)
115 language = models.CharField(max_length=255, blank=True)
116 based_on = models.ForeignKey(
117 "self", models.PROTECT, related_name="translation", null=True, blank=True
119 scans_source = models.CharField(max_length=255, blank=True)
120 text_source = models.CharField(max_length=255, blank=True)
121 notes = models.TextField(blank=True)
122 priority = models.PositiveSmallIntegerField(
123 default=0, choices=[(0, _("Low")), (1, _("Medium")), (2, _("High"))]
125 pd_year = models.IntegerField(null=True, blank=True)
126 gazeta_link = models.CharField(max_length=255, blank=True)
127 collections = models.ManyToManyField("Collection", blank=True)
129 estimated_chars = models.IntegerField(null=True, blank=True)
130 estimated_verses = models.IntegerField(null=True, blank=True)
131 estimate_source = models.CharField(max_length=2048, blank=True)
133 objects = UnrelatedManager()
136 ordering = ("title",)
137 verbose_name = _('book')
138 verbose_name_plural = _('books')
141 authors = WIKIDATA.AUTHOR
142 translators = WIKIDATA.TRANSLATOR
143 title = WIKIDATA.TITLE
144 language = WIKIDATA.LANGUAGE
145 based_on = WIKIDATA.BASED_ON
146 notes = "description"
150 astr = self.authors_str()
152 txt = f"{astr} – {txt}"
153 tstr = self.translators_str()
155 txt = f"{txt} (tłum. {tstr})"
158 def get_absolute_url(self):
159 return reverse("catalogue_book", args=[self.slug])
161 def authors_str(self):
162 return ", ".join(str(author) for author in self.authors.all())
164 def translators_str(self):
165 return ", ".join(str(author) for author in self.translators.all())
167 def get_document_books(self):
168 DBook = apps.get_model("documents", "Book")
169 return DBook.objects.filter(dc_slug=self.slug)
171 def get_estimated_costs(self):
173 work_type: work_type.calculate(self)
174 for work_type in WorkType.objects.all()
178 class Collection(models.Model):
179 name = models.CharField(max_length=255)
180 slug = models.SlugField(max_length=255, unique=True)
183 verbose_name = _('collection')
184 verbose_name_plural = _('collections')
190 class WorkType(models.Model):
191 name = models.CharField(max_length=255)
193 def get_rate_for(self, book):
194 for workrate in self.workrate_set.all():
195 if workrate.matches(book):
198 def calculate(self, book):
199 workrate = self.get_rate_for(book)
200 if workrate is not None:
201 return workrate.calculate(book)
205 class WorkRate(models.Model):
206 priority = models.IntegerField(default=1)
207 per_normpage = models.DecimalField(decimal_places=2, max_digits=6, null=True, blank=True)
208 per_verse = models.DecimalField(decimal_places=2, max_digits=6, null=True, blank=True)
209 work_type = models.ForeignKey(WorkType, models.CASCADE)
210 epochs = models.ManyToManyField(Epoch, blank=True)
211 kinds = models.ManyToManyField(Kind, blank=True)
212 genres = models.ManyToManyField(Genre, blank=True)
213 collections = models.ManyToManyField(Collection, blank=True)
216 ordering = ('priority',)
218 def matches(self, book):
219 for category in 'epochs', 'kinds', 'genres', 'collections':
220 oneof = getattr(self, category).all()
222 if not set(oneof).intersection(
223 getattr(book, category).all()):
227 def calculate(self, book):
229 if book.estimated_verses:
230 return book.estimated_verses * self.per_verse
231 elif self.per_normpage:
232 if book.estimated_chars:
233 return (decimal.Decimal(book.estimated_chars) / 1800 * self.per_normpage).quantize(decimal.Decimal('1.00'), rounding=decimal.ROUND_HALF_UP)