book/picture-only tags
authorJan Szejko <janek37@gmail.com>
Thu, 21 Dec 2017 11:05:47 +0000 (12:05 +0100)
committerJan Szejko <janek37@gmail.com>
Thu, 21 Dec 2017 11:05:47 +0000 (12:05 +0100)
src/catalogue/fields.py
src/catalogue/migrations/0019_auto_20171221_1107.py [new file with mode: 0644]
src/catalogue/models/book.py
src/catalogue/models/tag.py
src/picture/models.py

index 21d2bcf..372cf27 100644 (file)
@@ -193,6 +193,7 @@ class BuildHtml(BuildEbook):
                             tag.name = theme_name
                             setattr(tag, "name_%s" % lang, theme_name)
                             tag.sort_key = sortify(theme_name.lower())
                             tag.name = theme_name
                             setattr(tag, "name_%s" % lang, theme_name)
                             tag.sort_key = sortify(theme_name.lower())
+                            tag.for_books = True
                             tag.save()
                         themes.append(tag)
                     elif lang is not None:
                             tag.save()
                         themes.append(tag)
                     elif lang is not None:
@@ -214,6 +215,10 @@ class BuildHtml(BuildEbook):
 
                 new_fragment.save()
                 new_fragment.tags = set(meta_tags + themes)
 
                 new_fragment.save()
                 new_fragment.tags = set(meta_tags + themes)
+                for theme in themes:
+                    if not theme.for_books:
+                        theme.for_books = True
+                        theme.save()
             book.html_built.send(sender=type(self), instance=book)
             return True
         return False
             book.html_built.send(sender=type(self), instance=book)
             return True
         return False
diff --git a/src/catalogue/migrations/0019_auto_20171221_1107.py b/src/catalogue/migrations/0019_auto_20171221_1107.py
new file mode 100644 (file)
index 0000000..751e707
--- /dev/null
@@ -0,0 +1,35 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+def init_tag_for_books_pictures(apps, schema_editor):
+    Tag = apps.get_model('catalogue', 'Tag')
+    db_alias = schema_editor.connection.alias
+    tag_objects = Tag.objects.using(db_alias).exclude(category='set')
+    tags_for_books = tag_objects.filter(items__content_type__model__in=('book', 'fragment')).distinct()
+    tags_for_books.update(for_books=True)
+    tags_for_pictures = tag_objects.filter(items__content_type__model__in=('picture', 'picturearea')).distinct()
+    tags_for_pictures.update(for_pictures=True)
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('catalogue', '0018_auto_20171221_1106'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='tag',
+            name='for_books',
+            field=models.BooleanField(default=False),
+        ),
+        migrations.AddField(
+            model_name='tag',
+            name='for_pictures',
+            field=models.BooleanField(default=False),
+        ),
+        migrations.RunPython(init_tag_for_books_pictures, migrations.RunPython.noop)
+    ]
index 12fdb8d..44ee02e 100644 (file)
@@ -391,6 +391,11 @@ 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:
+            if not tag.for_books:
+                tag.for_books = True
+                tag.save()
+
         book.tags = set(meta_tags + book_shelves)
 
         cover_changed = old_cover != book.cover_info()
         book.tags = set(meta_tags + book_shelves)
 
         cover_changed = old_cover != book.cover_info()
index 828a8b1..830f29f 100644 (file)
@@ -60,6 +60,9 @@ class Tag(TagBase):
         _('category'), max_length=50, blank=False, null=False, db_index=True, choices=TAG_CATEGORIES)
     description = models.TextField(_('description'), blank=True)
 
         _('category'), max_length=50, blank=False, null=False, db_index=True, choices=TAG_CATEGORIES)
     description = models.TextField(_('description'), blank=True)
 
+    for_books = models.BooleanField(default=False)
+    for_pictures = models.BooleanField(default=False)
+
     user = models.ForeignKey(User, blank=True, null=True)
     gazeta_link = models.CharField(blank=True, max_length=240)
     culturepl_link = models.CharField(blank=True, max_length=240)
     user = models.ForeignKey(User, blank=True, null=True)
     gazeta_link = models.CharField(blank=True, max_length=240)
     culturepl_link = models.CharField(blank=True, max_length=240)
index 8f913c3..7732e20 100644 (file)
@@ -198,6 +198,10 @@ class Picture(models.Model):
             picture.extra_info = picture_xml.picture_info.to_dict()
 
             picture_tags = set(catalogue.models.Tag.tags_from_info(picture_xml.picture_info))
             picture.extra_info = picture_xml.picture_info.to_dict()
 
             picture_tags = set(catalogue.models.Tag.tags_from_info(picture_xml.picture_info))
+            for tag in picture_tags:
+                if not tag.for_pictures:
+                    tag.for_pictures = True
+                    tag.save()
 
             area_data = {'themes': {}, 'things': {}}
 
 
             area_data = {'themes': {}, 'things': {}}
 
@@ -221,16 +225,20 @@ class Picture(models.Model):
                             tag.name = objname
                             setattr(tag, 'name_%s' % lang, tag.name)
                             tag.sort_key = sortify(tag.name)
                             tag.name = objname
                             setattr(tag, 'name_%s' % lang, tag.name)
                             tag.sort_key = sortify(tag.name)
+                            tag.for_pictures = True
                             tag.save()
                             tag.save()
-                        # thing_tags.add(tag)
                         area_data['things'][tag.slug] = {
                             'object': objname,
                             'coords': part['coords'],
                             }
 
                         _tags.add(tag)
                         area_data['things'][tag.slug] = {
                             'object': objname,
                             'coords': part['coords'],
                             }
 
                         _tags.add(tag)
+                        if not tag.for_pictures:
+                            tag.for_pictures = True
+                            tag.save()
                     area = PictureArea.rectangle(picture, 'thing', part['coords'])
                     area.save()
                     area = PictureArea.rectangle(picture, 'thing', part['coords'])
                     area.save()
+                    # WTF thing area does not inherit tags from picture and theme area does, is it intentional?
                     area.tags = _tags
                 else:
                     _tags = set()
                     area.tags = _tags
                 else:
                     _tags = set()
@@ -241,9 +249,13 @@ class Picture(models.Model):
                             if created:
                                 tag.name = motif
                                 tag.sort_key = sortify(tag.name)
                             if created:
                                 tag.name = motif
                                 tag.sort_key = sortify(tag.name)
+                                tag.for_pictures = True
                                 tag.save()
                             # motif_tags.add(tag)
                             _tags.add(tag)
                                 tag.save()
                             # motif_tags.add(tag)
                             _tags.add(tag)
+                            if not tag.for_pictures:
+                                tag.for_pictures = True
+                                tag.save()
                             area_data['themes'][tag.slug] = {
                                 'theme': motif,
                                 'coords': part['coords']
                             area_data['themes'][tag.slug] = {
                                 'theme': motif,
                                 'coords': part['coords']