1 from collections import Counter
2 from datetime import date, timedelta
6 from urllib.request import urlopen
7 from django.apps import apps
8 from django.conf import settings
9 from django.db import models
10 from django.template.loader import render_to_string
11 from django.urls import reverse
12 from django.utils.translation import gettext_lazy as _
13 from admin_ordering.models import OrderableModel
14 from wikidata.client import Client
15 from librarian import DCNS
16 from librarian.cover import make_cover
17 from librarian.dcparser import BookInfo, Person
18 from .constants import WIKIDATA
19 from .wikidata import WikidataModel
20 from .wikimedia import WikiMedia
23 class Author(WikidataModel):
24 slug = models.SlugField(max_length=255, null=True, blank=True, unique=True)
25 first_name = models.CharField(_("first name"), max_length=255, blank=True)
26 last_name = models.CharField(_("last name"), max_length=255, blank=True)
27 genitive = models.CharField(
28 'dopełniacz', max_length=255, blank=True,
29 help_text='utwory … (czyje?)'
32 name_de = models.CharField(_("name (de)"), max_length=255, blank=True)
33 name_lt = models.CharField(_("name (lt)"), max_length=255, blank=True)
35 gender = models.CharField(_("gender"), max_length=255, blank=True)
36 nationality = models.CharField(_("nationality"), max_length=255, blank=True)
38 year_of_birth = models.SmallIntegerField(_("year of birth"), null=True, blank=True)
39 year_of_birth_inexact = models.BooleanField(_("inexact"), default=False)
40 year_of_birth_range = models.SmallIntegerField(_("year of birth, range end"), null=True, blank=True)
41 date_of_birth = models.DateField(_("date_of_birth"), null=True, blank=True)
42 century_of_birth = models.SmallIntegerField(
43 _("century of birth"), null=True, blank=True,
44 help_text=_('Set if year unknown. Negative for BC.')
46 place_of_birth = models.ForeignKey(
47 'Place', models.PROTECT, null=True, blank=True,
48 verbose_name=_('place of birth'),
49 related_name='authors_born'
51 year_of_death = models.SmallIntegerField(_("year of death"), null=True, blank=True)
52 year_of_death_inexact = models.BooleanField(_("inexact"), default=False)
53 year_of_death_range = models.SmallIntegerField(_("year of death, range end"), null=True, blank=True)
54 date_of_death = models.DateField(_("date_of_death"), null=True, blank=True)
55 century_of_death = models.SmallIntegerField(
56 _("century of death"), null=True, blank=True,
57 help_text=_('Set if year unknown. Negative for BC.')
59 place_of_death = models.ForeignKey(
60 'Place', models.PROTECT, null=True, blank=True,
61 verbose_name=_('place of death'),
62 related_name='authors_died'
64 status = models.PositiveSmallIntegerField(
75 notes = models.TextField(_("notes"), blank=True, help_text=_('private'))
77 gazeta_link = models.CharField(_("gazeta link"), max_length=255, blank=True)
78 culturepl_link = models.CharField(_("culture.pl link"), max_length=255, blank=True)
79 plwiki = models.CharField(blank=True, max_length=255)
80 photo = models.ImageField(blank=True, null=True, upload_to='catalogue/author/')
81 photo_source = models.CharField(blank=True, max_length=255)
82 photo_attribution = models.CharField(max_length=255, blank=True)
84 description = models.TextField(_("description"), blank=True, help_text=_('for publication'))
86 priority = models.PositiveSmallIntegerField(
88 default=0, choices=[(0, _("Low")), (1, _("Medium")), (2, _("High"))]
90 collections = models.ManyToManyField("Collection", blank=True, verbose_name=_("collections"))
92 woblink = models.IntegerField(null=True, blank=True)
95 verbose_name = _('author')
96 verbose_name_plural = _('authors')
97 ordering = ("last_name", "first_name", "year_of_death")
100 first_name = WIKIDATA.GIVEN_NAME
101 last_name = WIKIDATA.LAST_NAME
102 date_of_birth = WIKIDATA.DATE_OF_BIRTH
103 year_of_birth = WIKIDATA.DATE_OF_BIRTH
104 place_of_birth = WIKIDATA.PLACE_OF_BIRTH
105 date_of_death = WIKIDATA.DATE_OF_DEATH
106 year_of_death = WIKIDATA.DATE_OF_DEATH
107 place_of_death = WIKIDATA.PLACE_OF_DEATH
108 gender = WIKIDATA.GENDER
109 notes = WikiMedia.append("description")
111 photo = WikiMedia.download(WIKIDATA.IMAGE)
112 photo_source = WikiMedia.descriptionurl(WIKIDATA.IMAGE)
113 photo_attribution = WikiMedia.attribution(WIKIDATA.IMAGE)
115 def _supplement(obj):
116 if not obj.first_name and not obj.last_name:
117 yield 'first_name', 'label'
120 name = f"{self.first_name} {self.last_name}"
121 if self.year_of_death is not None:
122 name += f' (zm. {self.year_of_death})'
125 def get_absolute_url(self):
126 return reverse("catalogue_author", args=[self.slug])
129 def get_by_literal(cls, literal):
130 names = literal.split(',', 1)
131 names = [n.strip() for n in names]
133 return cls.objects.filter(last_name=names[0], first_name=names[1]).first()
135 return cls.objects.filter(last_name_pl=names[0], first_name_pl='').first() or \
136 cls.objects.filter(first_name_pl=names[0], last_name_pl='').first() or \
137 cls.objects.filter(first_name_pl=literal, last_name_pl='').first() or \
138 cls.objects.filter(first_name_pl=literal, last_name_pl=None).first()
142 return f"{self.last_name}, {self.first_name}"
146 if self.year_of_death:
147 return self.year_of_death + 71
148 elif self.year_of_death == 0:
153 def generate_description(self):
154 t = render_to_string(
155 'catalogue/author_description.html',
160 def century_description(self, number):
181 def birth_century_description(self):
182 return self.century_description(self.century_of_birth)
184 def death_century_description(self):
185 return self.century_description(self.century_of_death)
187 def year_description(self, number):
195 def year_of_birth_description(self):
196 return self.year_description(self.year_of_birth)
197 def year_of_death_description(self):
198 return self.year_description(self.year_of_death)
201 class NotableBook(OrderableModel):
202 author = models.ForeignKey(Author, models.CASCADE)
203 book = models.ForeignKey('Book', models.CASCADE)
206 return self.book.title
209 class Category(WikidataModel):
210 name = models.CharField(_("name"), max_length=255)
211 slug = models.SlugField(max_length=255, unique=True)
212 description = models.TextField(_("description"), blank=True, help_text=_('for publication'))
221 class Epoch(Category):
222 adjective_feminine_singular = models.CharField(
223 'przymiotnik pojedynczy żeński', max_length=255, blank=True,
224 help_text='twórczość … Adama Mickiewicza'
226 adjective_nonmasculine_plural = models.CharField(
227 'przymiotnik mnogi niemęskoosobowy', max_length=255, blank=True,
228 help_text='utwory … Adama Mickiewicza'
232 verbose_name = _('epoch')
233 verbose_name_plural = _('epochs')
236 class Genre(Category):
237 thema = models.CharField(
238 max_length=32, blank=True,
239 help_text='Odpowiadający kwalifikator Thema.'
241 plural = models.CharField(
242 'liczba mnoga', max_length=255, blank=True,
244 is_epoch_specific = models.BooleanField(
246 help_text='Po wskazaniu tego gatunku, dodanie epoki byłoby nadmiarowe, np. „dramat romantyczny”'
250 verbose_name = _('genre')
251 verbose_name_plural = _('genres')
254 class Kind(Category):
255 collective_noun = models.CharField(
256 'określenie zbiorowe', max_length=255, blank=True,
257 help_text='np. „Liryka” albo „Twórczość dramatyczna”'
261 verbose_name = _('kind')
262 verbose_name_plural = _('kinds')
265 class Book(WikidataModel):
266 slug = models.SlugField(max_length=255, blank=True, null=True, unique=True)
267 parent = models.ForeignKey('self', models.SET_NULL, null=True, blank=True)
268 parent_number = models.IntegerField(null=True, blank=True)
269 authors = models.ManyToManyField(Author, blank=True, verbose_name=_("authors"))
270 translators = models.ManyToManyField(
272 related_name="translated_book_set",
273 related_query_name="translated_book",
275 verbose_name=_("translators")
277 epochs = models.ManyToManyField(Epoch, blank=True, verbose_name=_("epochs"))
278 kinds = models.ManyToManyField(Kind, blank=True, verbose_name=_("kinds"))
279 genres = models.ManyToManyField(Genre, blank=True, verbose_name=_("genres"))
280 title = models.CharField(_("title"), max_length=255, blank=True)
281 language = models.CharField(_("language"), max_length=255, blank=True)
282 based_on = models.ForeignKey(
283 "self", models.PROTECT, related_name="translation", null=True, blank=True,
284 verbose_name=_("based on")
286 scans_source = models.CharField(_("scans source"), max_length=255, blank=True)
287 text_source = models.CharField(_("text source"), max_length=255, blank=True)
288 notes = models.TextField(_("notes"), blank=True, help_text=_('private'))
289 priority = models.PositiveSmallIntegerField(
291 default=0, choices=[(0, _("Low")), (1, _("Medium")), (2, _("High"))]
293 original_year = models.IntegerField(_('original publication year'), null=True, blank=True)
294 pd_year = models.IntegerField(_('year of entry into PD'), null=True, blank=True)
295 plwiki = models.CharField(blank=True, max_length=255)
296 gazeta_link = models.CharField(_("gazeta link"), max_length=255, blank=True)
297 collections = models.ManyToManyField("Collection", blank=True, verbose_name=_("collections"))
299 estimated_chars = models.IntegerField(_("estimated number of characters"), null=True, blank=True)
300 estimated_verses = models.IntegerField(_("estimated number of verses"), null=True, blank=True)
301 estimate_source = models.CharField(_("source of estimates"), max_length=2048, blank=True)
303 free_license = models.BooleanField(_('free license'), default=False)
304 polona_missing = models.BooleanField(_('missing on Polona'), default=False)
306 cover = models.FileField(blank=True, upload_to='catalogue/cover')
308 monthly_views_reader = models.IntegerField(default=0)
309 monthly_views_page = models.IntegerField(default=0)
312 ordering = ("title",)
313 verbose_name = _('book')
314 verbose_name_plural = _('books')
318 authors = WIKIDATA.AUTHOR
319 translators = WIKIDATA.TRANSLATOR
320 title = WIKIDATA.TITLE
321 language = WIKIDATA.LANGUAGE
322 based_on = WIKIDATA.BASED_ON
323 original_year = WIKIDATA.PUBLICATION_DATE
324 notes = WikiMedia.append("description")
328 if self.original_year:
329 txt = f"{txt} ({self.original_year})"
330 astr = self.authors_str()
332 txt = f"{txt}, {astr}"
333 tstr = self.translators_str()
335 txt = f"{txt}, tłum. {tstr}"
338 def build_cover(self):
339 width, height = 212, 300
340 # TODO: BookInfo shouldn't be required to build a cover.
341 info = BookInfo(rdf_attrs={}, dc_fields={
342 DCNS('creator'): [Person('Mickiewicz', 'Adam')],
343 DCNS('title'): ['Ziutek'],
344 DCNS('date'): ['1900-01-01'],
345 DCNS('publisher'): ['F'],
346 DCNS('language'): ['pol'],
347 DCNS('identifier.url'): ['test'],
348 DCNS('rights'): ['-'],
350 cover = make_cover(info, width=width, height=height)
354 self.cover.save(f'{self.slug}.{ext}', out, save=False)
355 type(self).objects.filter(pk=self.pk).update(cover=self.cover)
357 def get_absolute_url(self):
358 return reverse("catalogue_book", args=[self.slug])
360 def is_text_public(self):
361 return self.free_license or (self.pd_year is not None and self.pd_year <= date.today().year)
363 def audio_status(self):
368 return f'https://wolnelektury.pl/katalog/lektura/{self.slug}/'
370 def authors_str(self):
373 return ", ".join(str(author) for author in self.authors.all())
374 authors_str.admin_order_field = 'authors__last_name'
375 authors_str.short_description = _('Author')
377 def translators_str(self):
380 return ", ".join(str(author) for author in self.translators.all())
381 translators_str.admin_order_field = 'translators__last_name'
382 translators_str.short_description = _('Translator')
384 def authors_first_names(self):
385 return ', '.join(a.first_name for a in self.authors.all())
387 def authors_last_names(self):
388 return ', '.join(a.last_name for a in self.authors.all())
390 def translators_first_names(self):
391 return ', '.join(a.first_name for a in self.translators.all())
393 def translators_last_names(self):
394 return ', '.join(a.last_name for a in self.translators.all())
396 def document_book__project(self):
397 b = self.document_books.first()
398 if b is None: return ''
399 if b.project is None: return ''
400 return b.project.name
404 return self.document_books.first().wldocument().book_info.audience or ''
408 def get_estimated_costs(self):
410 work_type: work_type.calculate(self)
411 for work_type in WorkType.objects.all()
414 def scans_galleries(self):
415 return [bs.pk for bs in self.booksource_set.all()]
417 def is_published(self):
418 return any(b.is_published() for b in self.document_books.all())
420 def update_monthly_stats(self):
421 # Find publication date.
422 # By default, get previous 12 months.
423 this_month = date.today().replace(day=1)
424 cutoff = this_month.replace(year=this_month.year - 1)
427 # If the book was published later,
428 # find out the denominator.
429 pbr = apps.get_model('documents', 'BookPublishRecord').objects.filter(
430 book__catalogue_book=self).order_by('timestamp').first()
431 if pbr is not None and pbr.timestamp.date() > cutoff:
432 months = (this_month - pbr.timestamp.date()).days / 365 * 12
437 stats = self.bookmonthlystats_set.filter(date__gte=cutoff).aggregate(
438 views_page=models.Sum('views_page'),
439 views_reader=models.Sum('views_reader')
441 self.monthly_views_page = stats['views_page'] / months
442 self.monthly_views_reader = stats['views_reader'] / months
443 self.save(update_fields=['monthly_views_page', 'monthly_views_reader'])
446 def content_stats(self):
447 if hasattr(self, '_content_stats'):
448 return self._content_stats
450 stats = self.document_books.first().wldocument(librarian2=True).get_statistics()['total']
451 except Exception as e:
453 self._content_stats = stats
457 def are_sources_ready(self):
458 if not self.booksource_set.exists():
460 for bs in self.booksource_set.all():
461 if not bs.source.has_view_files() or not bs.source.has_ocr_files() or bs.source.modified_at > bs.source.processed_at:
465 chars = lambda self: self.content_stats.get('chars', '')
466 chars_with_fn = lambda self: self.content_stats.get('chars_with_fn', '')
467 words = lambda self: self.content_stats.get('words', '')
468 words_with_fn = lambda self: self.content_stats.get('words_with_fn', '')
469 verses = lambda self: self.content_stats.get('verses', '')
470 verses_with_fn = lambda self: self.content_stats.get('verses_with_fn', '')
471 chars_out_verse = lambda self: self.content_stats.get('chars_out_verse', '')
472 chars_out_verse_with_fn = lambda self: self.content_stats.get('chars_out_verse_with_fn', '')
475 class EditorNote(models.Model):
476 book = models.ForeignKey(Book, models.CASCADE)
477 user = models.ForeignKey(settings.AUTH_USER_MODEL, models.CASCADE)
478 created_at = models.DateTimeField(auto_now_add=True)
479 changed_at = models.DateTimeField(auto_now=True)
480 note = models.TextField(blank=True)
481 rate = models.IntegerField(default=3, choices=[
482 (n, n) for n in range(1, 6)
486 class CollectionCategory(models.Model):
487 name = models.CharField(_("name"), max_length=255)
488 parent = models.ForeignKey('self', models.SET_NULL, related_name='children', null=True, blank=True, verbose_name=_("parent"))
489 notes = models.TextField(_("notes"), blank=True, help_text=_('private'))
492 ordering = ('parent__name', 'name')
493 verbose_name = _('collection category')
494 verbose_name_plural = _('collection categories')
498 return f"{self.parent} / {self.name}"
503 class Collection(models.Model):
504 name = models.CharField(_("name"), max_length=255)
505 slug = models.SlugField(max_length=255, unique=True)
506 category = models.ForeignKey(CollectionCategory, models.SET_NULL, null=True, blank=True, verbose_name=_("category"))
507 notes = models.TextField(_("notes"), blank=True, help_text=_('private'))
508 description = models.TextField(_("description"), blank=True)
511 ordering = ('category', 'name')
512 verbose_name = _('collection')
513 verbose_name_plural = _('collections')
517 return f"{self.category} / {self.name}"
521 def get_estimated_costs(self):
523 for book in self.book_set.all():
524 for k, v in book.get_estimated_costs().items():
527 for author in self.author_set.all():
528 for book in author.book_set.all():
529 for k, v in book.get_estimated_costs().items():
531 for book in author.translated_book_set.all():
532 for k, v in book.get_estimated_costs().items():
537 class WorkType(models.Model):
538 name = models.CharField(_("name"), max_length=255)
542 verbose_name = _('work type')
543 verbose_name_plural = _('work types')
545 def get_rate_for(self, book):
546 for workrate in self.workrate_set.all():
547 if workrate.matches(book):
550 def calculate(self, book):
551 workrate = self.get_rate_for(book)
552 if workrate is not None:
553 return workrate.calculate(book)
557 class WorkRate(models.Model):
558 priority = models.IntegerField(_("priority"), default=1)
559 per_normpage = models.DecimalField(_("per normalized page"), decimal_places=2, max_digits=6, null=True, blank=True)
560 per_verse = models.DecimalField(_("per verse"), decimal_places=2, max_digits=6, null=True, blank=True)
561 work_type = models.ForeignKey(WorkType, models.CASCADE, verbose_name=_("work type"))
562 epochs = models.ManyToManyField(Epoch, blank=True, verbose_name=_("epochs"))
563 kinds = models.ManyToManyField(Kind, blank=True, verbose_name=_("kinds"))
564 genres = models.ManyToManyField(Genre, blank=True, verbose_name=_("genres"))
565 collections = models.ManyToManyField(Collection, blank=True, verbose_name=_("collections"))
568 ordering = ('priority',)
569 verbose_name = _('work rate')
570 verbose_name_plural = _('work rates')
572 def matches(self, book):
573 for category in 'epochs', 'kinds', 'genres', 'collections':
574 oneof = getattr(self, category).all()
576 if not set(oneof).intersection(
577 getattr(book, category).all()):
581 def calculate(self, book):
583 if book.estimated_verses:
584 return book.estimated_verses * self.per_verse
585 elif self.per_normpage:
586 if book.estimated_chars:
587 return (decimal.Decimal(book.estimated_chars) / 1800 * self.per_normpage).quantize(decimal.Decimal('1.00'), rounding=decimal.ROUND_HALF_UP)
590 class Place(WikidataModel):
591 name = models.CharField(_('name'), max_length=255, blank=True)
592 locative = models.CharField(_('locative'), max_length=255, blank=True, help_text=_('in…'))
595 verbose_name = _('place')
596 verbose_name_plural = _('places')
605 class BookMonthlyStats(models.Model):
606 book = models.ForeignKey('catalogue.Book', models.CASCADE)
607 date = models.DateField()
608 views_reader = models.IntegerField(default=0)
609 views_page = models.IntegerField(default=0)
612 def build_for_month(cls, date):
613 date = date.replace(day=1)
616 date = date.isoformat()
617 url = f'{settings.PIWIK_URL}?date={date}&filter_limit=-1&format=CSV&idSite={settings.PIWIK_WL_SITE_ID}&language=pl&method=Actions.getPageUrls&module=API&period={period}&segment=&token_auth={settings.PIWIK_TOKEN}&flat=1'
618 data = urlopen(url).read().decode('utf-16')
619 lines = data.split('\n')[1:]
621 m = re.match('^/katalog/lektura/([^,./]+)\.html,', line)
623 which = 'views_reader'
625 m = re.match('^/katalog/lektura/([^,./]+)/,', line)
630 _url, _uviews, views, _rest = line.split(',', 3)
633 book = Book.objects.get(slug=slug)
634 except Book.DoesNotExist:
637 cls.objects.update_or_create(
638 book=book, date=date,
639 defaults={which: views}
641 book.update_monthly_stats()
644 class Thema(models.Model):
645 code = models.CharField(
646 max_length=128, unique=True,
647 help_text='Używamy rozszerzenia <code>.WL-</code> do oznaczania własnych kodów.<br> '
648 'Przykładowo, w przypadku potrzeby stworzenia nowej kategorii „insurekcja kościuszkowska”, '
649 'można by ją utworzyć jako 3MLQ‑PL‑A.WL-A, czyli w ramach już istniejącej wyższej kategorii 3MLQ‑PL‑A „rozbiory Polski”.',
651 name = models.CharField(max_length=1024)
652 slug = models.SlugField(
653 max_length=255, null=True, blank=True, unique=True,
654 help_text='Element adresu na WL, w postaci: /tag/slug/. Można zmieniać.'
656 plural = models.CharField(
657 'liczba mnoga', max_length=255, blank=True,
659 description = models.TextField(blank=True)
660 public_description = models.TextField(blank=True)
661 usable = models.BooleanField()
662 usable_as_main = models.BooleanField(default=False)
663 hidden = models.BooleanField(default=False)
664 woblink_category = models.IntegerField(null=True, blank=True)
668 verbose_name_plural = 'Thema'
671 class Audience(models.Model):
672 code = models.CharField(
673 max_length=128, unique=True,
674 help_text='Techniczny identifyikator. W miarę możliwości nie należy zmieniać.'
676 name = models.CharField(
678 help_text='W formie: „dla … (kogo?)”'
680 slug = models.SlugField(
681 max_length=255, null=True, blank=True, unique=True,
682 help_text='Element adresu na WL, w postaci: /dla/slug/. Można zmieniać.'
684 description = models.TextField(blank=True)
685 thema = models.CharField(
686 max_length=32, blank=True,
687 help_text='Odpowiadający kwalifikator Thema.'
689 woblink = models.IntegerField(null=True, blank=True)