From 37f95f6fd3e1da107a0eaf28c516e71206100bf4 Mon Sep 17 00:00:00 2001 From: Stephen Burrows Date: Mon, 18 Jul 2011 23:45:43 -0400 Subject: [PATCH] Corrections to Blog.entry_tags to use taggit APIs. Tweaks to penfield migration 0005 because of South issue 428 (and some incorrect filters obscured by that issue.) --- philo/admin/base.py | 1 - philo/contrib/penfield/migrations/0005_to_taggit.py | 13 ++++++++----- .../penfield/migrations/0006_delete_tag_rels.py | 2 +- philo/contrib/penfield/models.py | 7 ++++++- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/philo/admin/base.py b/philo/admin/base.py index 2d52e55..d966c39 100644 --- a/philo/admin/base.py +++ b/philo/admin/base.py @@ -9,7 +9,6 @@ from mptt.admin import MPTTModelAdmin from philo.models import Attribute from philo.models.fields.entities import ForeignKeyAttribute, ManyToManyAttribute from philo.admin.forms.attributes import AttributeForm, AttributeInlineFormSet -from philo.admin.widgets import TagFilteredSelectMultiple from philo.forms.entities import EntityForm, proxy_fields_for_entity_model diff --git a/philo/contrib/penfield/migrations/0005_to_taggit.py b/philo/contrib/penfield/migrations/0005_to_taggit.py index d6e41b4..52090c6 100644 --- a/philo/contrib/penfield/migrations/0005_to_taggit.py +++ b/philo/contrib/penfield/migrations/0005_to_taggit.py @@ -39,21 +39,23 @@ class Migration(DataMigration): def backwards(self, orm): "Write your backwards methods here." - import pdb - pdb.set_trace() BlogEntry = orm['penfield.BlogEntry'] NewsletterArticle = orm['penfield.NewsletterArticle'] Tag = orm['philo.Tag'] TaggitTag = orm['taggit.Tag'] TaggedItem = orm['taggit.TaggedItem'] + ContentType = orm['contenttypes.contenttype'] + + entry_ct = ContentType.objects.get(app_label="penfield", model="blogentry") + article_ct = ContentType.objects.get(app_label="penfield", model="newsletterarticle") for entry in BlogEntry.objects.all(): - tag_slugs = list(TaggitTag.objects.filter(taggit_taggeditem_items__content_object=entry).values_list('slug', flat=True)).distinct() + tag_slugs = list(TaggitTag.objects.filter(taggit_taggeditem_items__content_type=entry_ct, taggit_taggeditem_items__object_id=entry.pk).values_list('slug', flat=True).distinct()) entry.tags = Tag.objects.filter(slug__in=tag_slugs) entry.save() for article in NewsletterArticle.objects.all(): - tag_slugs = list(TaggitTag.objects.filter(taggit_taggeditem_items__content_object=article).values_list('slug', flat=True)).distinct() + tag_slugs = list(TaggitTag.objects.filter(taggit_taggeditem_items__content_type=article_ct, taggit_taggeditem_items__object_id=article.pk).values_list('slug', flat=True).distinct()) article.tags = Tag.objects.filter(slug__in=tag_slugs) article.save() @@ -242,4 +244,5 @@ class Migration(DataMigration): } } - complete_apps = ['penfield'] + complete_apps = ['penfield', 'taggit'] + symmetrical = True diff --git a/philo/contrib/penfield/migrations/0006_delete_tag_rels.py b/philo/contrib/penfield/migrations/0006_delete_tag_rels.py index e65662f..d3bba00 100644 --- a/philo/contrib/penfield/migrations/0006_delete_tag_rels.py +++ b/philo/contrib/penfield/migrations/0006_delete_tag_rels.py @@ -215,4 +215,4 @@ class Migration(SchemaMigration): } } - complete_apps = ['penfield'] + complete_apps = ['penfield', 'taggit'] diff --git a/philo/contrib/penfield/models.py b/philo/contrib/penfield/models.py index adda629..eef8974 100644 --- a/philo/contrib/penfield/models.py +++ b/philo/contrib/penfield/models.py @@ -5,6 +5,7 @@ from django.conf.urls.defaults import url, patterns, include from django.db import models from django.http import Http404, HttpResponse from taggit.managers import TaggableManager +from taggit.models import Tag, TaggedItem from philo.contrib.winer.models import FeedView from philo.exceptions import ViewCanNotProvideSubpath @@ -27,7 +28,11 @@ class Blog(Entity): @property def entry_tags(self): """Returns a :class:`QuerySet` of :class:`.Tag`\ s that are used on any entries in this blog.""" - return Tag.objects.filter(blogentries__blog=self).distinct() + entry_pks = list(self.entries.values_list('pk', flat=True)) + kwargs = { + '%s__object_id__in' % TaggedItem.tag_relname(): entry_pks + } + return TaggedItem.tags_for(BlogEntry).filter(**kwargs) @property def entry_dates(self): -- 2.20.1