Author description generator
[redakcja.git] / src / catalogue / models.py
index d671425..92275ec 100644 (file)
@@ -2,15 +2,16 @@ from collections import Counter
 import decimal
 from django.apps import apps
 from django.db import models
+from django.template.loader import render_to_string
 from django.urls import reverse
 from django.utils.translation import gettext_lazy as _
 from admin_ordering.models import OrderableModel
 from wikidata.client import Client
 from .constants import WIKIDATA
-from .wikidata import WikidataMixin
+from .wikidata import WikidataModel
 
 
-class Author(WikidataMixin, models.Model):
+class Author(WikidataModel):
     slug = models.SlugField(max_length=255, null=True, blank=True, unique=True)
     first_name = models.CharField(_("first name"), max_length=255, blank=True)
     last_name = models.CharField(_("last name"), max_length=255, blank=True)
@@ -104,13 +105,19 @@ class Author(WikidataMixin, models.Model):
         else:
             return None
 
+    def generate_description(self):
+        t = render_to_string(
+            'catalogue/author_description.html',
+            {'obj': self}
+        )
+        return t
 
 class NotableBook(OrderableModel):
     author = models.ForeignKey(Author, models.CASCADE)
     book = models.ForeignKey('Book', models.CASCADE)
 
 
-class Category(WikidataMixin, models.Model):
+class Category(WikidataModel):
     name = models.CharField(_("name"), max_length=255)
     slug = models.SlugField(max_length=255, unique=True)
 
@@ -138,7 +145,7 @@ class Kind(Category):
         verbose_name_plural = _('kinds')
 
 
-class Book(WikidataMixin, models.Model):
+class Book(WikidataModel):
     slug = models.SlugField(max_length=255, blank=True, null=True, unique=True)
     authors = models.ManyToManyField(Author, blank=True, verbose_name=_("authors"))
     translators = models.ManyToManyField(
@@ -164,6 +171,7 @@ class Book(WikidataMixin, models.Model):
         _("priority"),
         default=0, choices=[(0, _("Low")), (1, _("Medium")), (2, _("High"))]
     )
+    original_year = models.IntegerField(_('original publication year'), null=True, blank=True)
     pd_year = models.IntegerField(_('year of entry into PD'), null=True, blank=True)
     gazeta_link = models.CharField(_("gazeta link"), max_length=255, blank=True)
     collections = models.ManyToManyField("Collection", blank=True, verbose_name=_("collections"))
@@ -186,10 +194,13 @@ class Book(WikidataMixin, models.Model):
         title = WIKIDATA.TITLE
         language = WIKIDATA.LANGUAGE
         based_on = WIKIDATA.BASED_ON
+        original_year = WIKIDATA.PUBLICATION_DATE
         notes = "description"
 
     def __str__(self):
         txt = self.title
+        if self.original_year:
+            txt = f"{txt} ({self.original_year})"
         astr = self.authors_str()
         if astr:
             txt = f"{txt}, {astr}"
@@ -325,7 +336,7 @@ class WorkRate(models.Model):
                 return (decimal.Decimal(book.estimated_chars) / 1800 * self.per_normpage).quantize(decimal.Decimal('1.00'), rounding=decimal.ROUND_HALF_UP)
 
 
-class Place(WikidataMixin, models.Model):
+class Place(WikidataModel):
     name = models.CharField(_('name'), max_length=255, blank=True)
     locative = models.CharField(_('locative'), max_length=255, blank=True, help_text=_('in…'))