From: Jan Szejko Date: Thu, 29 Jun 2017 10:30:01 +0000 (+0200) Subject: simplify TagRelation X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/2c7b7a116ebce28ca62052456e8eaae5176e1786 simplify TagRelation maybe it will fix unresolved keyword items --- diff --git a/src/catalogue/models/tag.py b/src/catalogue/models/tag.py index 00bdcb55b..828a8b191 100644 --- a/src/catalogue/models/tag.py +++ b/src/catalogue/models/tag.py @@ -3,8 +3,11 @@ # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # from django.conf import settings +from django.contrib.contenttypes.fields import GenericForeignKey +from django.contrib.contenttypes.models import ContentType from django.core.cache import caches from django.contrib.auth.models import User +from django.core.exceptions import ObjectDoesNotExist from django.db import models from django.db.models import permalink from django.db.models.query import Prefetch @@ -27,6 +30,24 @@ TAG_CATEGORIES = ( ) +class TagRelation(models.Model): + + tag = models.ForeignKey('Tag', verbose_name=_('tag'), related_name='items') + content_type = models.ForeignKey(ContentType, verbose_name=_('content type')) + object_id = models.PositiveIntegerField(_('object id'), db_index=True) + content_object = GenericForeignKey('content_type', 'object_id') + + class Meta: + db_table = 'catalogue_tag_relation' + unique_together = (('tag', 'content_type', 'object_id'),) + + def __unicode__(self): + try: + return u'%s [%s]' % (self.content_type.get_object_for_this_type(pk=self.object_id), self.tag) + except ObjectDoesNotExist: + return u' [%s]' % self.tag + + class Tag(TagBase): """A tag attachable to books and fragments (and possibly anything). @@ -49,6 +70,8 @@ class Tag(TagBase): after_change = Signal(providing_args=['instance', 'languages']) + intermediary_table_model = TagRelation + class UrlDeprecationWarning(DeprecationWarning): def __init__(self, tags=None): super(Tag.UrlDeprecationWarning, self).__init__() @@ -245,10 +268,6 @@ class Tag(TagBase): return meta_tags -# Pickle complains about not having this. -TagRelation = Tag.intermediary_table_model - - def prefetch_relations(objects, category, only_name=True): queryset = TagRelation.objects.filter(tag__category=category).select_related('tag') if only_name: diff --git a/src/newtagging/models.py b/src/newtagging/models.py index 1d1b22185..83aabe4fc 100644 --- a/src/newtagging/models.py +++ b/src/newtagging/models.py @@ -3,12 +3,9 @@ Models and managers for generic tagging. """ -from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.models import ContentType from django.db import connection, models -from django.utils.translation import ugettext_lazy as _ from django.db.models.base import ModelBase -from django.core.exceptions import ObjectDoesNotExist from django.dispatch import Signal qn = connection.ops.quote_name @@ -197,42 +194,13 @@ class TaggedItemManager(models.Manager): ########## # Models # ########## -def create_intermediary_table_model(model): - """Create an intermediary table model for the specific tag model""" - name = model.__name__ + 'Relation' - - class Meta: - db_table = '%s_relation' % model._meta.db_table - unique_together = (('tag', 'content_type', 'object_id'),) - app_label = model._meta.app_label - - def obj_unicode(self): - try: - return u'%s [%s]' % (self.content_type.get_object_for_this_type(pk=self.object_id), self.tag) - except ObjectDoesNotExist: - return u' [%s]' % self.tag - - # Set up a dictionary to simulate declarations within a class - attrs = { - '__module__': model.__module__, - 'Meta': Meta, - 'tag': models.ForeignKey(model, verbose_name=_('tag'), related_name='items'), - 'content_type': models.ForeignKey(ContentType, verbose_name=_('content type')), - 'object_id': models.PositiveIntegerField(_('object id'), db_index=True), - 'content_object': GenericForeignKey('content_type', 'object_id'), - '__unicode__': obj_unicode, - } - - return type(name, (models.Model,), attrs) - class TagMeta(ModelBase): """Metaclass for tag models (models inheriting from TagBase).""" def __new__(mcs, name, bases, attrs): model = super(TagMeta, mcs).__new__(mcs, name, bases, attrs) if not model._meta.abstract: - # Create an intermediary table and register custom managers for concrete models - model.intermediary_table_model = create_intermediary_table_model(model) + # Register custom managers for concrete models TagManager(model.intermediary_table_model).contribute_to_class(model, 'objects') TaggedItemManager(model).contribute_to_class(model.intermediary_table_model, 'objects') return model