Corrections to Blog.entry_tags to use taggit APIs. Tweaks to penfield migration 0005...
authorStephen Burrows <stephen.r.burrows@gmail.com>
Tue, 19 Jul 2011 03:45:43 +0000 (23:45 -0400)
committerStephen Burrows <stephen.r.burrows@gmail.com>
Tue, 19 Jul 2011 04:27:04 +0000 (00:27 -0400)
philo/admin/base.py
philo/contrib/penfield/migrations/0005_to_taggit.py
philo/contrib/penfield/migrations/0006_delete_tag_rels.py
philo/contrib/penfield/models.py

index 2d52e55..d966c39 100644 (file)
@@ -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.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
 
 
 from philo.forms.entities import EntityForm, proxy_fields_for_entity_model
 
 
index d6e41b4..52090c6 100644 (file)
@@ -39,21 +39,23 @@ class Migration(DataMigration):
 
        def backwards(self, orm):
                "Write your backwards methods here."
 
        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']
                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():
                
                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():
                        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()
 
                        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
index e65662f..d3bba00 100644 (file)
@@ -215,4 +215,4 @@ class Migration(SchemaMigration):
                }
        }
 
                }
        }
 
-       complete_apps = ['penfield']
+       complete_apps = ['penfield', 'taggit']
index adda629..eef8974 100644 (file)
@@ -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 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
 
 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."""
        @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):
        
        @property
        def entry_dates(self):