1 from django.apps import apps
2 from django.db import models
3 from django.urls import reverse
4 from django.utils.translation import gettext_lazy as _
5 from wikidata.client import Client
6 from .constants import WIKIDATA
7 from .utils import UnrelatedManager
8 from .wikidata import WikidataMixin
11 class Author(WikidataMixin, models.Model):
12 slug = models.SlugField(max_length=255, null=True, blank=True, unique=True)
13 first_name = models.CharField(max_length=255, blank=True)
14 last_name = models.CharField(max_length=255, blank=True)
16 name_de = models.CharField(max_length=255, blank=True)
17 name_lt = models.CharField(max_length=255, blank=True)
19 gender = models.CharField(max_length=255, blank=True)
20 nationality = models.CharField(max_length=255, blank=True)
21 year_of_death = models.SmallIntegerField(null=True, blank=True)
22 status = models.PositiveSmallIntegerField(
32 notes = models.TextField(blank=True)
33 gazeta_link = models.CharField(max_length=255, blank=True)
34 culturepl_link = models.CharField(max_length=255, blank=True)
36 description = models.TextField(blank=True)
37 description_de = models.TextField(blank=True)
38 description_lt = models.TextField(blank=True)
40 priority = models.PositiveSmallIntegerField(
41 default=0, choices=[(0, _("Low")), (1, _("Medium")), (2, _("High"))]
43 collections = models.ManyToManyField("Collection", blank=True)
46 verbose_name = _('author')
47 verbose_name_plural = _('authors')
48 ordering = ("last_name", "first_name", "year_of_death")
51 first_name = WIKIDATA.GIVEN_NAME
52 last_name = WIKIDATA.LAST_NAME
53 year_of_death = WIKIDATA.DATE_OF_DEATH
54 gender = WIKIDATA.GENDER
58 return f"{self.first_name} {self.last_name}"
60 def get_absolute_url(self):
61 return reverse("catalogue_author", args=[self.slug])
65 if self.year_of_death:
66 return self.year_of_death + 71
67 elif self.year_of_death == 0:
73 class Category(WikidataMixin, models.Model):
74 name = models.CharField(max_length=255)
75 slug = models.SlugField(max_length=255, unique=True)
81 class Epoch(Category):
83 verbose_name = _('epoch')
84 verbose_name_plural = _('epochs')
87 class Genre(Category):
89 verbose_name = _('genre')
90 verbose_name_plural = _('genres')
95 verbose_name = _('kind')
96 verbose_name_plural = _('kinds')
99 class Book(WikidataMixin, models.Model):
100 slug = models.SlugField(max_length=255, blank=True, null=True, unique=True)
101 authors = models.ManyToManyField(Author, blank=True)
102 translators = models.ManyToManyField(
104 related_name="translated_book_set",
105 related_query_name="translated_book",
108 epochs = models.ManyToManyField(Epoch, blank=True)
109 kinds = models.ManyToManyField(Kind, blank=True)
110 genres = models.ManyToManyField(Genre, blank=True)
111 title = models.CharField(max_length=255, blank=True)
112 language = models.CharField(max_length=255, blank=True)
113 based_on = models.ForeignKey(
114 "self", models.PROTECT, related_name="translation", null=True, blank=True
116 scans_source = models.CharField(max_length=255, blank=True)
117 text_source = models.CharField(max_length=255, blank=True)
118 notes = models.TextField(blank=True)
119 priority = models.PositiveSmallIntegerField(
120 default=0, choices=[(0, _("Low")), (1, _("Medium")), (2, _("High"))]
122 pd_year = models.IntegerField(null=True, blank=True)
123 gazeta_link = models.CharField(max_length=255, blank=True)
124 collections = models.ManyToManyField("Collection", blank=True)
126 objects = UnrelatedManager()
129 ordering = ("title",)
130 verbose_name = _('book')
131 verbose_name_plural = _('books')
134 authors = WIKIDATA.AUTHOR
135 translators = WIKIDATA.TRANSLATOR
136 title = WIKIDATA.TITLE
137 language = WIKIDATA.LANGUAGE
138 based_on = WIKIDATA.BASED_ON
139 notes = "description"
143 astr = self.authors_str()
145 txt = f"{astr} – {txt}"
146 tstr = self.translators_str()
148 txt = f"{txt} (tłum. {tstr})"
151 def get_absolute_url(self):
152 return reverse("catalogue_book", args=[self.slug])
154 def authors_str(self):
155 return ", ".join(str(author) for author in self.authors.all())
157 def translators_str(self):
158 return ", ".join(str(author) for author in self.translators.all())
160 def get_document_books(self):
161 DBook = apps.get_model("documents", "Book")
162 return DBook.objects.filter(dc_slug=self.slug)
165 class Collection(models.Model):
166 name = models.CharField(max_length=255)
167 slug = models.SlugField(max_length=255, unique=True)
170 verbose_name = _('collection')
171 verbose_name_plural = _('collections')