Corrections to Blog.entry_tags to use taggit APIs. Tweaks to penfield migration 0005...
[philo.git] / philo / contrib / penfield / models.py
index 9337589..eef8974 100644 (file)
@@ -4,10 +4,12 @@ from django.conf import settings
 from django.conf.urls.defaults import url, patterns, include
 from django.db import models
 from django.http import Http404, HttpResponse
 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
 
 from philo.contrib.winer.models import FeedView
 from philo.exceptions import ViewCanNotProvideSubpath
-from philo.models import Tag, Entity, Page, register_value_model
+from philo.models import Entity, Page, register_value_model
 from philo.models.fields import TemplateField
 from philo.utils import paginate
 
 from philo.models.fields import TemplateField
 from philo.utils import paginate
 
@@ -26,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):
@@ -61,8 +67,8 @@ class BlogEntry(Entity):
        #: An optional brief excerpt from the :class:`BlogEntry`.
        excerpt = models.TextField(blank=True, null=True)
        
        #: An optional brief excerpt from the :class:`BlogEntry`.
        excerpt = models.TextField(blank=True, null=True)
        
-       #: :class:`.Tag`\ s for this :class:`BlogEntry`.
-       tags = models.ManyToManyField(Tag, related_name='blogentries', blank=True, null=True)
+       #: A ``django-taggit`` :class:`TaggableManager`.
+       tags = TaggableManager()
        
        def save(self, *args, **kwargs):
                if self.date is None:
        
        def save(self, *args, **kwargs):
                if self.date is None:
@@ -132,7 +138,7 @@ class BlogView(FeedView):
        
        def get_reverse_params(self, obj):
                if isinstance(obj, BlogEntry):
        
        def get_reverse_params(self, obj):
                if isinstance(obj, BlogEntry):
-                       if obj.blog == self.blog:
+                       if obj.blog_id == self.blog_id:
                                kwargs = {'slug': obj.slug}
                                if self.entry_permalink_style in 'DMY':
                                        kwargs.update({'year': str(obj.date.year).zfill(4)})
                                kwargs = {'slug': obj.slug}
                                if self.entry_permalink_style in 'DMY':
                                        kwargs.update({'year': str(obj.date.year).zfill(4)})
@@ -161,12 +167,12 @@ class BlogView(FeedView):
                urlpatterns = self.feed_patterns(r'^', 'get_all_entries', 'index_page', 'index') +\
                        self.feed_patterns(r'^%s/(?P<tag_slugs>[-\w]+[-+/\w]*)' % self.tag_permalink_base, 'get_entries_by_tag', 'tag_page', 'entries_by_tag')
                
                urlpatterns = self.feed_patterns(r'^', 'get_all_entries', 'index_page', 'index') +\
                        self.feed_patterns(r'^%s/(?P<tag_slugs>[-\w]+[-+/\w]*)' % self.tag_permalink_base, 'get_entries_by_tag', 'tag_page', 'entries_by_tag')
                
-               if self.tag_archive_page:
+               if self.tag_archive_page_id:
                        urlpatterns += patterns('',
                                url((r'^%s$' % self.tag_permalink_base), self.tag_archive_view, name='tag_archive')
                        )
                
                        urlpatterns += patterns('',
                                url((r'^%s$' % self.tag_permalink_base), self.tag_archive_view, name='tag_archive')
                        )
                
-               if self.entry_archive_page:
+               if self.entry_archive_page_id:
                        if self.entry_permalink_style in 'DMY':
                                urlpatterns += self.feed_patterns(r'^(?P<year>\d{4})', 'get_entries_by_ymd', 'entry_archive_page', 'entries_by_year')
                                if self.entry_permalink_style in 'DM':
                        if self.entry_permalink_style in 'DMY':
                                urlpatterns += self.feed_patterns(r'^(?P<year>\d{4})', 'get_entries_by_ymd', 'entry_archive_page', 'entries_by_year')
                                if self.entry_permalink_style in 'DM':
@@ -368,8 +374,8 @@ class NewsletterArticle(Entity):
        lede = TemplateField(null=True, blank=True, verbose_name='Summary')
        #: A :class:`.TemplateField` containing the full text of the article.
        full_text = TemplateField(db_index=True)
        lede = TemplateField(null=True, blank=True, verbose_name='Summary')
        #: A :class:`.TemplateField` containing the full text of the article.
        full_text = TemplateField(db_index=True)
-       #: A :class:`ManyToManyField` to :class:`.Tag`\ s for the :class:`NewsletterArticle`.
-       tags = models.ManyToManyField(Tag, related_name='newsletterarticles', blank=True, null=True)
+       #: A ``django-taggit`` :class:`TaggableManager`.
+       tags = TaggableManager()
        
        def save(self, *args, **kwargs):
                if self.date is None:
        
        def save(self, *args, **kwargs):
                if self.date is None:
@@ -455,7 +461,7 @@ class NewsletterView(FeedView):
        
        def get_reverse_params(self, obj):
                if isinstance(obj, NewsletterArticle):
        
        def get_reverse_params(self, obj):
                if isinstance(obj, NewsletterArticle):
-                       if obj.newsletter == self.newsletter:
+                       if obj.newsletter_id == self.newsletter_id:
                                kwargs = {'slug': obj.slug}
                                if self.article_permalink_style in 'DMY':
                                        kwargs.update({'year': str(obj.date.year).zfill(4)})
                                kwargs = {'slug': obj.slug}
                                if self.article_permalink_style in 'DMY':
                                        kwargs.update({'year': str(obj.date.year).zfill(4)})
@@ -465,7 +471,7 @@ class NewsletterView(FeedView):
                                                        kwargs.update({'day': str(obj.date.day).zfill(2)})
                                return self.article_view, [], kwargs
                elif isinstance(obj, NewsletterIssue):
                                                        kwargs.update({'day': str(obj.date.day).zfill(2)})
                                return self.article_view, [], kwargs
                elif isinstance(obj, NewsletterIssue):
-                       if obj.newsletter == self.newsletter:
+                       if obj.newsletter_id == self.newsletter_id:
                                return 'issue', [], {'numbering': obj.numbering}
                elif isinstance(obj, (date, datetime)):
                        kwargs = {
                                return 'issue', [], {'numbering': obj.numbering}
                elif isinstance(obj, (date, datetime)):
                        kwargs = {
@@ -481,11 +487,11 @@ class NewsletterView(FeedView):
                urlpatterns = self.feed_patterns(r'^', 'get_all_articles', 'index_page', 'index') + patterns('',
                        url(r'^%s/(?P<numbering>.+)$' % self.issue_permalink_base, self.page_view('get_articles_by_issue', 'issue_page'), name='issue')
                )
                urlpatterns = self.feed_patterns(r'^', 'get_all_articles', 'index_page', 'index') + patterns('',
                        url(r'^%s/(?P<numbering>.+)$' % self.issue_permalink_base, self.page_view('get_articles_by_issue', 'issue_page'), name='issue')
                )
-               if self.issue_archive_page:
+               if self.issue_archive_page_id:
                        urlpatterns += patterns('',
                                url(r'^%s$' % self.issue_permalink_base, self.issue_archive_view, 'issue_archive')
                        )
                        urlpatterns += patterns('',
                                url(r'^%s$' % self.issue_permalink_base, self.issue_archive_view, 'issue_archive')
                        )
-               if self.article_archive_page:
+               if self.article_archive_page_id:
                        urlpatterns += self.feed_patterns(r'^%s' % self.article_permalink_base, 'get_all_articles', 'article_archive_page', 'articles')
                        if self.article_permalink_style in 'DMY':
                                urlpatterns += self.feed_patterns(r'^%s/(?P<year>\d{4})' % self.article_permalink_base, 'get_articles_by_ymd', 'article_archive_page', 'articles_by_year')
                        urlpatterns += self.feed_patterns(r'^%s' % self.article_permalink_base, 'get_all_articles', 'article_archive_page', 'articles')
                        if self.article_permalink_style in 'DMY':
                                urlpatterns += self.feed_patterns(r'^%s/(?P<year>\d{4})' % self.article_permalink_base, 'get_articles_by_ymd', 'article_archive_page', 'articles_by_year')