From: Radek Czajka Date: Mon, 20 May 2024 13:19:37 +0000 (+0200) Subject: Translators as authors. X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/f3ee4c1bfbf4e3856268fa79c63465f591f3499e?ds=sidebyside Translators as authors. --- diff --git a/src/catalogue/migrations/0047_book_translators.py b/src/catalogue/migrations/0047_book_translators.py new file mode 100644 index 000000000..6146f6495 --- /dev/null +++ b/src/catalogue/migrations/0047_book_translators.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.8 on 2024-05-20 12:31 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('catalogue', '0046_alter_book_options_alter_bookmedia_options_and_more'), + ] + + operations = [ + migrations.AddField( + model_name='book', + name='translators', + field=models.ManyToManyField(to='catalogue.tag'), + ), + ] diff --git a/src/catalogue/models/book.py b/src/catalogue/models/book.py index 10e9d22e3..9e0ec50ff 100644 --- a/src/catalogue/models/book.py +++ b/src/catalogue/models/book.py @@ -91,6 +91,7 @@ class Book(models.Model): tagged = managers.ModelTaggedItemManager(Tag) tags = managers.TagDescriptor(Tag) tag_relations = GenericRelation(Tag.intermediary_table_model) + translators = models.ManyToManyField(Tag) html_built = django.dispatch.Signal() published = django.dispatch.Signal() @@ -154,12 +155,6 @@ class Book(models.Model): def genre_unicode(self): return self.tag_unicode('genre') - def translators(self): - translators = self.get_extra_info_json().get('translators') or [] - return [ - '\xa0'.join(reversed(translator.split(', ', 1))) for translator in translators - ] - def translator(self): translators = self.get_extra_info_json().get('translators') if not translators: @@ -658,14 +653,17 @@ class Book(models.Model): meta_tags = Tag.tags_from_info(book_info) - for tag in meta_tags: + for tag, relationship in meta_tags: if not tag.for_books: tag.for_books = True tag.save() - book.tags = set(meta_tags + book_shelves) + just_tags = [t for (t, rel) in meta_tags if not rel] + book.tags = set(just_tags + book_shelves) book.save() # update sort_key_author + book.translators.set([t for (t, rel) in meta_tags if rel == 'translator']) + cover_changed = old_cover != book.cover_info() obsolete_children = set(b for b in book.children.all() if b not in children) diff --git a/src/catalogue/models/tag.py b/src/catalogue/models/tag.py index cdc1dc8a2..a5c96d542 100644 --- a/src/catalogue/models/tag.py +++ b/src/catalogue/models/tag.py @@ -213,16 +213,20 @@ class Tag(models.Model): from slugify import slugify from sortify import sortify meta_tags = [] - categories = (('kinds', 'kind'), ('genres', 'genre'), ('authors', 'author'), ('epochs', 'epoch')) - for field_name, category in categories: + categories = ( + # BookInfo field names, Tag category, relationship + ('kinds', 'kind', None), + ('genres', 'genre', None), + ('epochs', 'epoch', None), + ('authors', 'author', None), + ('translators', 'author', 'translator'), + ) + for field_name, category, relationship in categories: try: tag_names = getattr(info, field_name) except (AttributeError, KeyError): # TODO: shouldn't be KeyError here at all. - try: - tag_names = [getattr(info, category)] - except KeyError: - # For instance, Pictures do not have 'genre' field. - continue + # For instance, Pictures do not have 'genre' field. + continue for tag_name in tag_names: lang = getattr(tag_name, 'lang', None) or settings.LANGUAGE_CODE tag_sort_key = tag_name @@ -243,9 +247,9 @@ class Tag(models.Model): tag.sort_key = sortify(tag_sort_key.lower()) tag.save() - meta_tags.append(tag) + meta_tags.append((tag, relationship)) else: - meta_tags.append(tag) + meta_tags.append((tag, relationship)) return meta_tags diff --git a/src/catalogue/templates/catalogue/book_detail.html b/src/catalogue/templates/catalogue/book_detail.html index 6586970dd..eb1ab8a7d 100644 --- a/src/catalogue/templates/catalogue/book_detail.html +++ b/src/catalogue/templates/catalogue/book_detail.html @@ -90,18 +90,17 @@

{% for author in book.authors %}{{ author.name }}{% if not forloop.last %}, {% endif %}{% endfor %}

{{ book.title }}

- {% with translators=book.translators %} - {% if translators %} + {% if book.translators.exists %}

- {% if translators.0 != 'tłumacz nieznany' %} - {% trans "tłum." %} - {% endif %} - {% for translator in translators %} - {{ translator }}{% if not forloop.last %}, {% endif %} + {% for translator in book.translators.all %} + {% if forloop.first and translator.name != 'tłumacz nieznany' %} + {% trans "tłum." %} + {% endif %} + + {{ translator }}{% if not forloop.last %}, {% endif %} {% endfor %}

{% endif %} - {% endwith %} diff --git a/src/catalogue/templates/catalogue/book_list.html b/src/catalogue/templates/catalogue/book_list.html index dabb0ca48..e8debd975 100644 --- a/src/catalogue/templates/catalogue/book_list.html +++ b/src/catalogue/templates/catalogue/book_list.html @@ -117,6 +117,17 @@ {% paginate %} + {% if translation_list %} +
+

Tłumaczenia

+
+ {% for book in translation_list %} + {% include "catalogue/book_box.html" %} + {% endfor %} +
+
+ {% endif %} + {% if main_tag %}
diff --git a/src/catalogue/templates/catalogue/book_text.html b/src/catalogue/templates/catalogue/book_text.html index 421d439fe..52f281b05 100644 --- a/src/catalogue/templates/catalogue/book_text.html +++ b/src/catalogue/templates/catalogue/book_text.html @@ -151,18 +151,17 @@

{% for author in book.authors %}{{ author.name }}{% if not forloop.last %}, {% endif %}{% endfor %}

{{ book.title }}

- {% with translators=book.translators %} - {% if translators %} -

- {% if translators.0 != 'tłumacz nieznany' %} + {% if book.translators.exists %} +

+ {% for translator in book.translators.all %} + {% if forloop.first and translator.name != 'tłumacz nieznany' %} {% trans "tłum." %} {% endif %} - {% for translator in translators %} - {{ translator }}{% if not forloop.last %}, {% endif %} - {% endfor %} -

- {% endif %} - {% endwith %} + + {{ translator }}{% if not forloop.last %}, {% endif %} + {% endfor %} +

+ {% endif %}
{% content_warning book %}
diff --git a/src/catalogue/views.py b/src/catalogue/views.py index af4d1e3b5..588cdb073 100644 --- a/src/catalogue/views.py +++ b/src/catalogue/views.py @@ -253,6 +253,8 @@ class TaggedObjectList(BookList): t for t in self.ctx['tags'] if t is not self.ctx['main_tag'] ] + if len(self.ctx['tags']) == 1 and self.ctx['main_tag'].category == 'author': + self.ctx['translation_list'] = self.ctx['main_tag'].book_set.all() def get_queryset(self): qs = Book.tagged.with_all(self.ctx['work_tags']).filter(findable=True) diff --git a/src/picture/models.py b/src/picture/models.py index dec9960cb..98fd382f3 100644 --- a/src/picture/models.py +++ b/src/picture/models.py @@ -202,7 +202,7 @@ class Picture(models.Model): picture.title = str(picture_xml.picture_info.title) picture.extra_info = json.dumps(picture_xml.picture_info.to_dict()) - picture_tags = set(catalogue.models.Tag.tags_from_info(picture_xml.picture_info)) + picture_tags = set([t for (t, rel) in catalogue.models.Tag.tags_from_info(picture_xml.picture_info)]) for tag in picture_tags: if not tag.for_pictures: tag.for_pictures = True