From 09f311cf21633def6e6eda0549633cd07b363cf2 Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Thu, 21 Dec 2017 12:05:47 +0100 Subject: [PATCH] book/picture-only tags --- src/catalogue/fields.py | 5 +++ .../migrations/0019_auto_20171221_1107.py | 35 +++++++++++++++++++ src/catalogue/models/book.py | 5 +++ src/catalogue/models/tag.py | 3 ++ src/picture/models.py | 14 +++++++- 5 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/catalogue/migrations/0019_auto_20171221_1107.py diff --git a/src/catalogue/fields.py b/src/catalogue/fields.py index 21d2bcf72..372cf2735 100644 --- a/src/catalogue/fields.py +++ b/src/catalogue/fields.py @@ -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.for_books = True 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) + 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 diff --git a/src/catalogue/migrations/0019_auto_20171221_1107.py b/src/catalogue/migrations/0019_auto_20171221_1107.py new file mode 100644 index 000000000..751e707d8 --- /dev/null +++ b/src/catalogue/migrations/0019_auto_20171221_1107.py @@ -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) + ] diff --git a/src/catalogue/models/book.py b/src/catalogue/models/book.py index 12fdb8d3d..44ee02ecc 100644 --- a/src/catalogue/models/book.py +++ b/src/catalogue/models/book.py @@ -391,6 +391,11 @@ class Book(models.Model): 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() diff --git a/src/catalogue/models/tag.py b/src/catalogue/models/tag.py index 828a8b191..830f29f69 100644 --- a/src/catalogue/models/tag.py +++ b/src/catalogue/models/tag.py @@ -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) + 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) diff --git a/src/picture/models.py b/src/picture/models.py index 8f913c356..7732e2042 100644 --- a/src/picture/models.py +++ b/src/picture/models.py @@ -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)) + for tag in picture_tags: + if not tag.for_pictures: + tag.for_pictures = True + tag.save() 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.for_pictures = True tag.save() - # thing_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() + # WTF thing area does not inherit tags from picture and theme area does, is it intentional? 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) + tag.for_pictures = True 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'] -- 2.20.1