Refactored Node.get_absolute_url and related functions (such as MultiView.reverse...
[philo.git] / contrib / penfield / models.py
index 6b6b1f7..27e5b5d 100644 (file)
@@ -93,7 +93,7 @@ class BlogView(MultiView, FeedMultiViewMixin):
                                                        kwargs.update({'day': str(obj.date.day).zfill(2)})
                                return self.entry_view, [], kwargs
                elif isinstance(obj, Tag):
-                       if obj in self.blog.entry_tags:
+                       if obj in self.get_tag_queryset():
                                return 'entries_by_tag', [], {'tag_slugs': obj.slug}
                elif isinstance(obj, (date, datetime)):
                        kwargs = {
@@ -111,65 +111,68 @@ class BlogView(MultiView, FeedMultiViewMixin):
                )
                if self.feeds_enabled:
                        urlpatterns += patterns('',
-                               url(r'^%s/(?P<tag_slugs>[-\w]+[-+/\w]*)/%s/' % (self.tag_permalink_base, self.feed_suffix), self.feed_view(self.get_entries_by_tag, 'entries_by_tag_feed'), name='entries_by_tag_feed'),
+                               url(r'^%s/(?P<tag_slugs>[-\w]+[-+/\w]*)/%s$' % (self.tag_permalink_base, self.feed_suffix), self.feed_view(self.get_entries_by_tag, 'entries_by_tag_feed'), name='entries_by_tag_feed'),
                        )
                urlpatterns += patterns('',
-                       url(r'^%s/(?P<tag_slugs>[-\w]+[-+/\w]*)/' % self.tag_permalink_base, self.page_view(self.get_entries_by_tag, self.tag_page), name='entries_by_tag')
+                       url(r'^%s/(?P<tag_slugs>[-\w]+[-+/\w]*)$' % self.tag_permalink_base, self.page_view(self.get_entries_by_tag, self.tag_page), name='entries_by_tag')
                )
                if self.tag_archive_page:
                        urlpatterns += patterns('',
-                               url((r'^(?:%s)/?$' % self.tag_permalink_base), self.basic_view('tag_archive_page'))
+                               url((r'^%s$' % self.tag_permalink_base), self.tag_archive_view, name='tag_archive')
                        )
                
                if self.entry_archive_page:
                        if self.entry_permalink_style in 'DMY':
                                urlpatterns += patterns('',
-                                       url(r'^(?P<year>\d{4})/', include(self.feed_patterns(self.get_entries_by_ymd, self.entry_archive_page, 'entries_by_year')))
+                                       url(r'^(?P<year>\d{4})', include(self.feed_patterns(self.get_entries_by_ymd, self.entry_archive_page, 'entries_by_year')))
                                )
                                if self.entry_permalink_style in 'DM':
                                        urlpatterns += patterns('',
-                                               url(r'^(?P<year>\d{4})/(?P<month>\d{2})/?$', include(self.feed_patterns(self.get_entries_by_ymd, self.entry_archive_page, 'entries_by_month'))),
+                                               url(r'^(?P<year>\d{4})/(?P<month>\d{2})$', include(self.feed_patterns(self.get_entries_by_ymd, self.entry_archive_page, 'entries_by_month'))),
                                        )
                                        if self.entry_permalink_style == 'D':
                                                urlpatterns += patterns('',
-                                                       url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/?$', include(self.feed_patterns(self.get_entries_by_ymd, self.entry_archive_page, 'entries_by_day')))
+                                                       url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})$', include(self.feed_patterns(self.get_entries_by_ymd, self.entry_archive_page, 'entries_by_day')))
                                                )
                
                if self.entry_permalink_style == 'D':
                        urlpatterns += patterns('',
-                               url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<slug>[-\w]+)/?$', self.entry_view)
+                               url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<slug>[-\w]+)$', self.entry_view)
                        )
                elif self.entry_permalink_style == 'M':
                        urlpatterns += patterns('',
-                               url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<slug>[-\w]+)/?$', self.entry_view)
+                               url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<slug>[-\w]+)$', self.entry_view)
                        )
                elif self.entry_permalink_style == 'Y':
                        urlpatterns += patterns('',
-                               url(r'^(?P<year>\d{4})/(?P<slug>[-\w]+)/?$', self.entry_view)
+                               url(r'^(?P<year>\d{4})/(?P<slug>[-\w]+)$', self.entry_view)
                        )
                elif self.entry_permalink_style == 'B':
                        urlpatterns += patterns('',
-                               url((r'^(?:%s)/(?P<slug>[-\w]+)/?$' % self.entry_permalink_base), self.entry_view)
+                               url((r'^%s/(?P<slug>[-\w]+)$' % self.entry_permalink_base), self.entry_view)
                        )
                else:
                        urlpatterns = patterns('',
-                               url(r'^(?P<slug>[-\w]+)/?$', self.entry_view)
+                               url(r'^(?P<slug>[-\w]+)$', self.entry_view)
                        )
                return urlpatterns
        
        def get_context(self):
                return {'blog': self.blog}
        
-       def get_item_queryset(self):
+       def get_entry_queryset(self):
                return self.blog.entries.all()
        
+       def get_tag_queryset(self):
+               return self.blog.entry_tags
+       
        def get_all_entries(self, request, extra_context=None):
-               return self.get_item_queryset(), extra_context
+               return self.get_entry_queryset(), extra_context
        
        def get_entries_by_ymd(self, request, year=None, month=None, day=None, extra_context=None):
                if not self.entry_archive_page:
                        raise Http404
-               entries = self.get_item_queryset()
+               entries = self.get_entry_queryset()
                if year:
                        entries = entries.filter(date__year=year)
                if month:
@@ -183,7 +186,7 @@ class BlogView(MultiView, FeedMultiViewMixin):
        
        def get_entries_by_tag(self, request, tag_slugs, extra_context=None):
                tag_slugs = tag_slugs.replace('+', '/').split('/')
-               tags = self.blog.entry_tags.filter(slug__in=tag_slugs)
+               tags = self.get_tag_queryset().filter(slug__in=tag_slugs)
                
                if not tags:
                        raise Http404
@@ -194,7 +197,7 @@ class BlogView(MultiView, FeedMultiViewMixin):
                        if slug and slug not in found_slugs:
                                raise Http404
 
-               entries = self.get_item_queryset()
+               entries = self.get_entry_queryset()
                for tag in tags:
                        entries = entries.filter(tags=tag)
                
@@ -203,6 +206,33 @@ class BlogView(MultiView, FeedMultiViewMixin):
                
                return entries, context
        
+       def entry_view(self, request, slug, year=None, month=None, day=None, extra_context=None):
+               entries = self.get_entry_queryset()
+               if year:
+                       entries = entries.filter(date__year=year)
+               if month:
+                       entries = entries.filter(date__month=month)
+               if day:
+                       entries = entries.filter(date__day=day)
+               try:
+                       entry = entries.get(slug=slug)
+               except:
+                       raise Http404
+               context = self.get_context()
+               context.update(extra_context or {})
+               context.update({'entry': entry})
+               return self.entry_page.render_to_response(request, extra_context=context)
+       
+       def tag_archive_view(self, request, extra_context=None):
+               if not self.tag_archive_page:
+                       raise Http404
+               context = self.get_context()
+               context.update(extra_context or {})
+               context.update({
+                       'tags': self.get_tag_queryset()
+               })
+               return self.tag_archive_page.render_to_response(request, extra_context=context)
+       
        def add_item(self, feed, obj, kwargs=None):
                title = loader.get_template("penfield/feeds/blog_entry/title.html")
                description = loader.get_template("penfield/feeds/blog_entry/description.html")
@@ -227,23 +257,6 @@ class BlogView(MultiView, FeedMultiViewMixin):
                }
                defaults.update(kwargs or {})
                return super(BlogView, self).get_feed(feed_type, extra_context, defaults)
-       
-       def entry_view(self, request, slug, year=None, month=None, day=None, extra_context=None):
-               entries = self.get_item_queryset()
-               if year:
-                       entries = entries.filter(date__year=year)
-               if month:
-                       entries = entries.filter(date__month=month)
-               if day:
-                       entries = entries.filter(date__day=day)
-               try:
-                       entry = entries.get(slug=slug)
-               except:
-                       raise Http404
-               context = self.get_context()
-               context.update(extra_context or {})
-               context.update({'entry': entry})
-               return self.entry_page.render_to_response(request, extra_context=context)
 
 
 class Newsletter(Entity, Titled):
@@ -342,44 +355,44 @@ class NewsletterView(MultiView, FeedMultiViewMixin):
        def urlpatterns(self):
                urlpatterns = patterns('',
                        url(r'^', include(self.feed_patterns(self.get_all_articles, self.index_page, 'index'))),
-                       url(r'^(?:%s)/(?P<numbering>.+)/' % self.issue_permalink_base, include(self.feed_patterns(self.get_articles_by_issue, self.issue_page, 'issue')))
+                       url(r'^%s/(?P<numbering>.+)' % self.issue_permalink_base, include(self.feed_patterns(self.get_articles_by_issue, self.issue_page, 'issue')))
                )
                if self.issue_archive_page:
                        urlpatterns += patterns('',
-                               url(r'^(?:%s)/$' % self.issue_permalink_base, self.basic_view('issue_archive_page'))
+                               url(r'^%s$' % self.issue_permalink_base, self.issue_archive_view, 'issue_archive')
                        )
                if self.article_archive_page:
                        urlpatterns += patterns('',
-                               url(r'^(?:%s)/' % self.article_permalink_base, include(self.feed_patterns(self.get_all_articles, self.article_archive_page, 'articles')))
+                               url(r'^%s' % self.article_permalink_base, include(self.feed_patterns(self.get_all_articles, self.article_archive_page, 'articles')))
                        )
                        if self.article_permalink_style in 'DMY':
                                urlpatterns += patterns('',
-                                       url(r'^(?:%s)/(?P<year>\d{4})/' % self.article_permalink_base, include(self.feed_patterns(self.get_articles_by_ymd, self.article_archive_page, 'articles_by_year')))
+                                       url(r'^%s/(?P<year>\d{4})' % self.article_permalink_base, include(self.feed_patterns(self.get_articles_by_ymd, self.article_archive_page, 'articles_by_year')))
                                )
                                if self.article_permalink_style in 'DM':
                                        urlpatterns += patterns('',
-                                               url(r'^(?:%s)/(?P<year>\d{4})/(?P<month>\d{2})/' % self.article_permalink_base, include(self.feed_patterns(self.get_articles_by_ymd, self.article_archive_page, 'articles_by_month')))
+                                               url(r'^%s/(?P<year>\d{4})/(?P<month>\d{2})' % self.article_permalink_base, include(self.feed_patterns(self.get_articles_by_ymd, self.article_archive_page, 'articles_by_month')))
                                        )
                                        if self.article_permalink_style == 'D':
                                                urlpatterns += patterns('',
-                                                       url(r'^(?:%s)/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/' % self.article_permalink_base, include(self.feed_patterns(self.get_articles_by_ymd, self.article_archive_page, 'articles_by_day')))
+                                                       url(r'^%s/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})' % self.article_permalink_base, include(self.feed_patterns(self.get_articles_by_ymd, self.article_archive_page, 'articles_by_day')))
                                                )
                
                if self.article_permalink_style == 'Y':
                        urlpatterns += patterns('',
-                               url(r'^(?:%s)/(?P<year>\d{4})/(?P<slug>[\w-]+)/$' % self.article_permalink_base, self.article_view)
+                               url(r'^%s/(?P<year>\d{4})/(?P<slug>[\w-]+)$' % self.article_permalink_base, self.article_view)
                        )
                elif self.article_permalink_style == 'M':
                        urlpatterns += patterns('',
-                               url(r'^(?:%s)/(?P<year>\d{4})/(?P<month>\d{2})/(?P<slug>[\w-]+)/$' % self.article_permalink_base, self.article_view)
+                               url(r'^%s/(?P<year>\d{4})/(?P<month>\d{2})/(?P<slug>[\w-]+)$' % self.article_permalink_base, self.article_view)
                        )
                elif self.article_permalink_style == 'D':
                        urlpatterns += patterns('',
-                               url(r'^(?:%s)/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<slug>[\w-]+)/$' % self.article_permalink_base, self.article_view)
+                               url(r'^%s/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<slug>[\w-]+)$' % self.article_permalink_base, self.article_view)
                        )
                else:   
                        urlpatterns += patterns('',
-                               url(r'^(?:%s)/(?P<slug>[-\w]+)/?$' % self.article_permalink_base, self.article_view)
+                               url(r'^%s/(?P<slug>[-\w]+)$' % self.article_permalink_base, self.article_view)
                        )
                
                return urlpatterns
@@ -387,14 +400,17 @@ class NewsletterView(MultiView, FeedMultiViewMixin):
        def get_context(self):
                return {'newsletter': self.newsletter}
        
-       def get_item_queryset(self):
+       def get_article_queryset(self):
                return self.newsletter.articles.all()
        
+       def get_issue_queryset(self):
+               return self.newsletter.issues.all()
+       
        def get_all_articles(self, request, extra_context=None):
-               return self.get_item_queryset(), extra_context
+               return self.get_article_queryset(), extra_context
        
        def get_articles_by_ymd(self, request, year, month=None, day=None, extra_context=None):
-               articles = self.get_item_queryset().filter(date__year=year)
+               articles = self.get_article_queryset().filter(date__year=year)
                if month:
                        articles = articles.filter(date__month=month)
                if day:
@@ -403,15 +419,15 @@ class NewsletterView(MultiView, FeedMultiViewMixin):
        
        def get_articles_by_issue(self, request, numbering, extra_context=None):
                try:
-                       issue = self.newsletter.issues.get(numbering=numbering)
+                       issue = self.get_issue_queryset().get(numbering=numbering)
                except NewsletterIssue.DoesNotExist:
                        raise Http404
                context = extra_context or {}
                context.update({'issue': issue})
-               return self.get_item_queryset().filter(issues=issue), context
+               return self.get_article_queryset().filter(issues=issue), context
        
        def article_view(self, request, slug, year=None, month=None, day=None, extra_context=None):
-               articles = self.get_item_queryset()
+               articles = self.get_article_queryset()
                if year:
                        articles = articles.filter(date__year=year)
                if month:
@@ -427,6 +443,16 @@ class NewsletterView(MultiView, FeedMultiViewMixin):
                context.update({'article': article})
                return self.article_page.render_to_response(request, extra_context=context)
        
+       def issue_archive_view(self, request, extra_context):
+               if not self.issue_archive_page:
+                       raise Http404
+               context = self.get_context()
+               context.update(extra_context or {})
+               context.update({
+                       'issues': self.get_issue_queryset()
+               })
+               return self.issue_archive_page.render_to_response(request, extra_context=context)
+       
        def add_item(self, feed, obj, kwargs=None):
                title = loader.get_template("penfield/feeds/newsletter_article/title.html")
                description = loader.get_template("penfield/feeds/newsletter_article/description.html")