X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/fe3838bfb645ccb7e2775ae709ac45044fd14d51..9fc7f1c883b4f5f582dd864ad468ab73f95c62f4:/apps/newtagging/models.py diff --git a/apps/newtagging/models.py b/apps/newtagging/models.py index e988b6c9d..b70ab9a09 100644 --- a/apps/newtagging/models.py +++ b/apps/newtagging/models.py @@ -1,8 +1,12 @@ +# -*- coding: utf-8 -*- """ Models and managers for generic tagging. """ + # Python 2.3 compatibility -if not hasattr(__builtins__, 'set'): +try: + set +except NameError: from sets import Set as set from django.contrib.contenttypes import generic @@ -10,6 +14,7 @@ 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 qn = connection.ops.quote_name @@ -62,6 +67,14 @@ class TagManager(models.Manager): if tag not in current_tags: self.intermediary_table_model._default_manager.create(tag=tag, content_object=obj) + def remove_tag(self, obj, tag): + """ + Remove tag from an object. + """ + content_type = ContentType.objects.get_for_model(obj) + self.intermediary_table_model._default_manager.filter(content_type__pk=content_type.pk, + object_id=obj.pk, tag=tag).delete() + def get_for_object(self, obj): """ Create a queryset matching all tags associated with the given @@ -71,7 +84,7 @@ class TagManager(models.Manager): return self.filter(items__content_type__pk=ctype.pk, items__object_id=obj.pk) - def _get_usage(self, model, counts=False, min_count=None, extra_joins=None, extra_criteria=None, params=None, extra=None): + def _get_usage(self, model, counts=False, min_count=None, extra_joins=None, extra_criteria=None, params=None, extra=None, extra_tables=None): """ Perform the custom SQL query for ``usage_for_model`` and ``usage_for_queryset``. @@ -91,15 +104,15 @@ class TagManager(models.Manager): SELECT DISTINCT %(tag_columns)s%(count_sql)s FROM %(tag)s - INNER JOIN %(tagged_item)s - ON %(tag)s.id = %(tagged_item)s.tag_id + INNER JOIN %(tagged_item)s AS %(tagged_item_alias)s + ON %(tag)s.id = %(tagged_item_alias)s.tag_id INNER JOIN %(model)s - ON %(tagged_item)s.object_id = %(model_pk)s + ON %(tagged_item_alias)s.object_id = %(model_pk)s %%s - WHERE %(tagged_item)s.content_type_id = %(content_type_id)s + WHERE %(tagged_item_alias)s.content_type_id = %(content_type_id)s %%s %(extra_where)s - GROUP BY %(tag)s.id, %(tag)s.name + GROUP BY %(tag_columns)s, %(tag)s.id, %(tag)s.name%(extra_tables)s %%s ORDER BY %(tag)s.%(ordering)s ASC""" % { 'tag': qn(self.model._meta.db_table), @@ -107,9 +120,11 @@ class TagManager(models.Manager): 'tag_columns': tag_columns, 'count_sql': counts and (', COUNT(%s)' % model_pk) or '', 'tagged_item': qn(self.intermediary_table_model._meta.db_table), + 'tagged_item_alias': qn('_newtagging_' + self.intermediary_table_model._meta.db_table), 'model': model_table, 'model_pk': model_pk, 'extra_where': extra_where, + 'extra_tables': ''.join((', %s.id' % qn(table)) for table in extra_tables), 'content_type_id': ContentType.objects.get_for_model(model).pk, } @@ -187,11 +202,12 @@ class TagManager(models.Manager): extra_joins = ' '.join(queryset.query.get_from_clause()[0][1:]) where, params = queryset.query.where.as_sql() + extra_tables = queryset.query.extra_tables if where: extra_criteria = 'AND %s' % where else: extra_criteria = '' - return self._get_usage(queryset.model, counts, min_count, extra_joins, extra_criteria, params, extra) + return self._get_usage(queryset.model, counts, min_count, extra_joins, extra_criteria, params, extra, extra_tables=extra_tables) def related_for_model(self, tags, model, counts=False, min_count=None, extra=None): """ @@ -225,7 +241,7 @@ class TagManager(models.Manager): WHERE %(tagged_item)s.content_type_id = %(content_type_id)s AND %(tagged_item)s.object_id IN ( - SELECT temporary.object_id + SELECT * FROM ( SELECT %(tagged_item)s.object_id FROM %(tagged_item)s, %(tag)s @@ -236,6 +252,7 @@ class TagManager(models.Manager): HAVING COUNT(%(tagged_item)s.object_id) = %(tag_count)s ) AS temporary ) + AND %(tag)s.id NOT IN (%(tag_id_placeholders)s) %(extra_where)s GROUP BY %(tag_columns)s %(min_count_sql)s @@ -465,8 +482,11 @@ def create_intermediary_table_model(model): unique_together = (('tag', 'content_type', 'object_id'),) def obj_unicode(self): - return u'%s [%s]' % (self.content_type.get_object_for_this_type(pk=self.object_id), self.tag) - + 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__,