Translators as authors.
authorRadek Czajka <rczajka@rczajka.pl>
Mon, 20 May 2024 13:19:37 +0000 (15:19 +0200)
committerRadek Czajka <rczajka@rczajka.pl>
Mon, 20 May 2024 13:19:37 +0000 (15:19 +0200)
src/catalogue/migrations/0047_book_translators.py [new file with mode: 0644]
src/catalogue/models/book.py
src/catalogue/models/tag.py
src/catalogue/templates/catalogue/book_detail.html
src/catalogue/templates/catalogue/book_list.html
src/catalogue/templates/catalogue/book_text.html
src/catalogue/views.py
src/picture/models.py

diff --git a/src/catalogue/migrations/0047_book_translators.py b/src/catalogue/migrations/0047_book_translators.py
new file mode 100644 (file)
index 0000000..6146f64
--- /dev/null
@@ -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'),
+        ),
+    ]
index 10e9d22..9e0ec50 100644 (file)
@@ -91,6 +91,7 @@ class Book(models.Model):
     tagged = managers.ModelTaggedItemManager(Tag)
     tags = managers.TagDescriptor(Tag)
     tag_relations = GenericRelation(Tag.intermediary_table_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()
 
     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 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:
     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)
 
 
         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()
 
             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.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)
         cover_changed = old_cover != book.cover_info()
         obsolete_children = set(b for b in book.children.all()
                                 if b not in children)
index cdc1dc8..a5c96d5 100644 (file)
@@ -213,16 +213,20 @@ class Tag(models.Model):
         from slugify import slugify
         from sortify import sortify
         meta_tags = []
         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, 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
             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()
 
                             tag.sort_key = sortify(tag_sort_key.lower())
                             tag.save()
 
-                        meta_tags.append(tag)
+                        meta_tags.append((tag, relationship))
                 else:
                 else:
-                    meta_tags.append(tag)
+                    meta_tags.append((tag, relationship))
         return meta_tags
 
 
         return meta_tags
 
 
index 6586970..eb1ab8a 100644 (file)
                 <p>{% for author in book.authors %}<a href="{{ author.get_absolute_url }}">{{ author.name }}</a>{% if not forloop.last %}, {% endif %}{% endfor %}
                 </p>
                 <h1><a href="{{ book.get_absolute_url }}">{{ book.title }}</a></h1>
                 <p>{% for author in book.authors %}<a href="{{ author.get_absolute_url }}">{{ author.name }}</a>{% if not forloop.last %}, {% endif %}{% endfor %}
                 </p>
                 <h1><a href="{{ book.get_absolute_url }}">{{ book.title }}</a></h1>
-                {% with translators=book.translators %}
-                {% if translators %}
+                {% if book.translators.exists %}
                   <p class="l-header__translators">
                   <p class="l-header__translators">
-                    {% 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 %}
+                      <a href="{{ translator.get_absolute_url }}">
+                        {{ translator }}</a>{% if not forloop.last %}, {% endif %}
                     {% endfor %}
                   </p>
                 {% endif %}
                     {% endfor %}
                   </p>
                 {% endif %}
-                {% endwith %}
               </div>
 
 
               </div>
 
 
index dabb0ca..e8debd9 100644 (file)
     {% paginate %}
   </div>
 
     {% paginate %}
   </div>
 
+  {% if translation_list %}
+    <div class="l-section l-section--col">
+      <h2 class="header">Tłumaczenia</h2>
+      <div class="l-books__grid" id="book-list">
+        {% for book in translation_list %}
+          {% include "catalogue/book_box.html" %}
+        {% endfor %}
+      </div>
+    </div>
+  {% endif %}
+
   {% if main_tag %}
     <section class="l-section">
       <div class="l-author">
   {% if main_tag %}
     <section class="l-section">
       <div class="l-author">
index 421d439..52f281b 100644 (file)
       <p class="l-text_header_author">{% for author in book.authors %}<a href="{{ author.get_absolute_url }}">{{ author.name }}</a>{% if not forloop.last %}, {% endif %}{% endfor %}
       </p>
       <h1 class="l-text_header_title"><a href="{{ book.get_absolute_url }}">{{ book.title }}</a></h1>
       <p class="l-text_header_author">{% for author in book.authors %}<a href="{{ author.get_absolute_url }}">{{ author.name }}</a>{% if not forloop.last %}, {% endif %}{% endfor %}
       </p>
       <h1 class="l-text_header_title"><a href="{{ book.get_absolute_url }}">{{ book.title }}</a></h1>
-      {% with translators=book.translators %}
-        {% if translators %}
-          <p class="l-header__translators">
-            {% if translators.0 != 'tłumacz nieznany' %}
+      {% if book.translators.exists %}
+        <p class="l-header__translators">
+          {% for translator in book.translators.all %}
+            {% if forloop.first and translator.name != 'tłumacz nieznany' %}
               {% trans "tłum." %}
             {% endif %}
               {% trans "tłum." %}
             {% endif %}
-            {% for translator in translators %}
-              {{ translator }}{% if not forloop.last %}, {% endif %}
-            {% endfor %}
-          </p>
-        {% endif %}
-      {% endwith %}
+            <a href="{{ translator.get_absolute_url }}">
+              {{ translator }}</a>{% if not forloop.last %}, {% endif %}
+          {% endfor %}
+        </p>
+      {% endif %}
       <div style="margin-top:1em;">
         {% content_warning book %}
       </div>
       <div style="margin-top:1em;">
         {% content_warning book %}
       </div>
index af4d1e3..588cdb0 100644 (file)
@@ -253,6 +253,8 @@ class TaggedObjectList(BookList):
             t for t in self.ctx['tags']
             if t is not self.ctx['main_tag']
         ]
             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)
 
     def get_queryset(self):
         qs = Book.tagged.with_all(self.ctx['work_tags']).filter(findable=True)
index dec9960..98fd382 100644 (file)
@@ -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.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
             for tag in picture_tags:
                 if not tag.for_pictures:
                     tag.for_pictures = True