Add work rates.
[redakcja.git] / src / catalogue / models.py
1 import decimal
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
10
11
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)
16
17     name_de = models.CharField(max_length=255, blank=True)
18     name_lt = models.CharField(max_length=255, blank=True)
19
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(
24         null=True,
25         blank=True,
26         choices=[
27             (1, _("Alive")),
28             (2, _("Dead")),
29             (3, _("Long dead")),
30             (4, _("Unknown")),
31         ],
32     )
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)
36
37     description = models.TextField(blank=True)
38     description_de = models.TextField(blank=True)
39     description_lt = models.TextField(blank=True)
40
41     priority = models.PositiveSmallIntegerField(
42         default=0, choices=[(0, _("Low")), (1, _("Medium")), (2, _("High"))]
43     )
44     collections = models.ManyToManyField("Collection", blank=True)
45
46     class Meta:
47         verbose_name = _('author')
48         verbose_name_plural = _('authors')
49         ordering = ("last_name", "first_name", "year_of_death")
50
51     class Wikidata:
52         first_name = WIKIDATA.GIVEN_NAME
53         last_name = WIKIDATA.LAST_NAME
54         year_of_death = WIKIDATA.DATE_OF_DEATH
55         gender = WIKIDATA.GENDER
56         notes = "description"
57
58     def __str__(self):
59         return f"{self.first_name} {self.last_name}"
60
61     def get_absolute_url(self):
62         return reverse("catalogue_author", args=[self.slug])
63
64     @property
65     def pd_year(self):
66         if self.year_of_death:
67             return self.year_of_death + 71
68         elif self.year_of_death == 0:
69             return 0
70         else:
71             return None
72
73
74 class Category(WikidataMixin, models.Model):
75     name = models.CharField(max_length=255)
76     slug = models.SlugField(max_length=255, unique=True)
77
78     class Meta:
79         abstract = True
80
81     def __str__(self):
82         return self.name
83
84 class Epoch(Category):
85     class Meta:
86         verbose_name = _('epoch')
87         verbose_name_plural = _('epochs')
88
89
90 class Genre(Category):
91     class Meta:
92         verbose_name = _('genre')
93         verbose_name_plural = _('genres')
94
95
96 class Kind(Category):
97     class Meta:
98         verbose_name = _('kind')
99         verbose_name_plural = _('kinds')
100
101
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(
106         Author,
107         related_name="translated_book_set",
108         related_query_name="translated_book",
109         blank=True,
110     )
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
118     )
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"))]
124     )
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)
128
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)
132
133     objects = UnrelatedManager()
134
135     class Meta:
136         ordering = ("title",)
137         verbose_name = _('book')
138         verbose_name_plural = _('books')
139
140     class Wikidata:
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"
147
148     def __str__(self):
149         txt = self.title
150         astr = self.authors_str()
151         if astr:
152             txt = f"{astr} – {txt}"
153         tstr = self.translators_str()
154         if tstr:
155             txt = f"{txt} (tłum. {tstr})"
156         return txt
157
158     def get_absolute_url(self):
159         return reverse("catalogue_book", args=[self.slug])
160     
161     def authors_str(self):
162         return ", ".join(str(author) for author in self.authors.all())
163
164     def translators_str(self):
165         return ", ".join(str(author) for author in self.translators.all())
166
167     def get_document_books(self):
168         DBook = apps.get_model("documents", "Book")
169         return DBook.objects.filter(dc_slug=self.slug)
170
171     def estimated_costs(self):
172         return "\n".join(
173             "{}: {} zł".format(
174                 work_type.name,
175                 work_type.calculate(self) or '—'
176             )
177             for work_type in WorkType.objects.all()
178         )
179
180
181 class Collection(models.Model):
182     name = models.CharField(max_length=255)
183     slug = models.SlugField(max_length=255, unique=True)
184
185     class Meta:
186         verbose_name = _('collection')
187         verbose_name_plural = _('collections')
188
189     def __str__(self):
190         return self.name
191
192
193 class WorkType(models.Model):
194     name = models.CharField(max_length=255)
195
196     def get_rate_for(self, book):
197         for workrate in self.workrate_set.all():
198             if workrate.matches(book):
199                 return workrate
200
201     def calculate(self, book):
202         workrate = self.get_rate_for(book)
203         if workrate is not None:
204             return workrate.calculate(book)
205         
206
207
208 class WorkRate(models.Model):
209     priority = models.IntegerField(default=1)
210     per_normpage = models.DecimalField(decimal_places=2, max_digits=6, null=True, blank=True)
211     per_verse = models.DecimalField(decimal_places=2, max_digits=6, null=True, blank=True)
212     work_type = models.ForeignKey(WorkType, models.CASCADE)
213     epochs = models.ManyToManyField(Epoch, blank=True)
214     kinds = models.ManyToManyField(Kind, blank=True)
215     genres = models.ManyToManyField(Genre, blank=True)
216     collections = models.ManyToManyField(Collection, blank=True)
217
218     class Meta:
219         ordering = ('priority',)
220
221     def matches(self, book):
222         for category in 'epochs', 'kinds', 'genres', 'collections':
223             oneof = getattr(self, category).all()
224             if oneof:
225                 if not set(oneof).intersection(
226                         getattr(book, category).all()):
227                     return False
228         return True
229
230     def calculate(self, book):
231         if self.per_verse:
232             if book.estimated_verses:
233                 return book.estimated_verses * self.per_verse
234         elif self.per_normpage:
235             if book.estimated_chars:
236                 return (decimal.Decimal(book.estimated_chars) / 1800 * self.per_normpage).quantize(decimal.Decimal('1.00'), rounding=decimal.ROUND_HALF_UP)
237