X-Git-Url: http://git.ithinksw.org/philo.git/blobdiff_plain/c3ff61f0458f01829b271aa88c561de0b88bb364..2f7b3e87ff28392c872a0e828224d0b781297041:/contrib/penfield/models.py?ds=sidebyside diff --git a/contrib/penfield/models.py b/contrib/penfield/models.py index a884663..98dcdd5 100644 --- a/contrib/penfield/models.py +++ b/contrib/penfield/models.py @@ -19,6 +19,7 @@ try: except: mimeparse = None + ATOM = feedgenerator.Atom1Feed.mime_type RSS = feedgenerator.Rss201rev2Feed.mime_type FEEDS = SortedDict([ @@ -52,20 +53,23 @@ class FeedView(MultiView): description = "" - def feed_patterns(self, get_items_attr, page_attr, reverse_name): + def feed_patterns(self, base, get_items_attr, page_attr, reverse_name): """ Given the name to be used to reverse this view and the names of the attributes for the function that fetches the objects, returns patterns suitable for inclusion in urlpatterns. """ - urlpatterns = patterns('', - url(r'^$', self.page_view(get_items_attr, page_attr), name=reverse_name) - ) + urlpatterns = patterns('') if self.feeds_enabled: feed_reverse_name = "%s_feed" % reverse_name + feed_view = self.feed_view(get_items_attr, feed_reverse_name) + feed_pattern = r'%s%s%s$' % (base, (base and base[-1] != "^") and "/" or "", self.feed_suffix) urlpatterns += patterns('', - url(r'^%s$' % self.feed_suffix, self.feed_view(get_items_attr, feed_reverse_name), name=feed_reverse_name), + url(feed_pattern, feed_view, name=feed_reverse_name), ) + urlpatterns += patterns('', + url(r"%s$" % base, self.page_view(get_items_attr, page_attr), name=reverse_name) + ) return urlpatterns def get_object(self, request, **kwargs): @@ -121,6 +125,8 @@ class FeedView(MultiView): def get_feed_type(self, request): feed_type = self.feed_type + if feed_type not in FEEDS: + feed_type = FEEDS.keys()[0] accept = request.META.get('HTTP_ACCEPT') if accept and feed_type not in accept and "*/*" not in accept and "%s/*" % feed_type.split("/")[0] not in accept: # Wups! They aren't accepting the chosen format. Is there another format we can use? @@ -333,6 +339,7 @@ class BlogView(FeedView): index_page = models.ForeignKey(Page, related_name='blog_index_related') entry_page = models.ForeignKey(Page, related_name='blog_entry_related') + # TODO: entry_archive is misleading. Rename to ymd_page or timespan_page. entry_archive_page = models.ForeignKey(Page, related_name='blog_entry_archive_related', null=True, blank=True) tag_page = models.ForeignKey(Page, related_name='blog_tag_related') tag_archive_page = models.ForeignKey(Page, related_name='blog_tag_archive_related', null=True, blank=True) @@ -359,7 +366,7 @@ class BlogView(FeedView): if self.entry_permalink_style == 'D': kwargs.update({'day': str(obj.date.day).zfill(2)}) return self.entry_view, [], kwargs - elif isinstance(obj, Tag) or (isinstance(obj, models.QuerySet) and obj.model == Tag and obj): + elif isinstance(obj, Tag) or (isinstance(obj, models.query.QuerySet) and obj.model == Tag and obj): if isinstance(obj, Tag): obj = [obj] slugs = [tag.slug for tag in obj if tag in self.get_tag_queryset()] @@ -376,16 +383,9 @@ class BlogView(FeedView): @property def urlpatterns(self): - urlpatterns = patterns('', - url(r'^', include(self.feed_patterns('get_all_entries', 'index_page', 'index'))), - ) - if self.feeds_enabled: - urlpatterns += patterns('', - url(r'^%s/(?P[-\w]+[-+/\w]*)/%s$' % (self.tag_permalink_base, self.feed_suffix), self.feed_view('get_entries_by_tag', 'entries_by_tag_feed'), name='entries_by_tag_feed'), - ) - urlpatterns += patterns('', - url(r'^%s/(?P[-\w]+[-+/\w]*)$' % self.tag_permalink_base, self.page_view('get_entries_by_tag', 'tag_page'), name='entries_by_tag') - ) + urlpatterns = self.feed_patterns(r'^', 'get_all_entries', 'index_page', 'index') +\ + self.feed_patterns(r'^%s/(?P[-\w]+[-+/\w]*)$' % self.tag_permalink_base, 'get_entries_by_tag', 'tag_page', 'entries_by_tag') + if self.tag_archive_page: urlpatterns += patterns('', url((r'^%s$' % self.tag_permalink_base), self.tag_archive_view, name='tag_archive') @@ -393,17 +393,11 @@ class BlogView(FeedView): if self.entry_archive_page: if self.entry_permalink_style in 'DMY': - urlpatterns += patterns('', - url(r'^(?P\d{4})', include(self.feed_patterns('get_entries_by_ymd', 'entry_archive_page', 'entries_by_year'))) - ) + urlpatterns += self.feed_patterns(r'^(?P\d{4})', 'get_entries_by_ymd', 'entry_archive_page', 'entries_by_year') if self.entry_permalink_style in 'DM': - urlpatterns += patterns('', - url(r'^(?P\d{4})/(?P\d{2})$', include(self.feed_patterns('get_entries_by_ymd', 'entry_archive_page', 'entries_by_month'))), - ) + urlpatterns += self.feed_patterns(r'^(?P\d{4})/(?P\d{2})', 'get_entries_by_ymd', 'entry_archive_page', 'entries_by_month') if self.entry_permalink_style == 'D': - urlpatterns += patterns('', - url(r'^(?P\d{4})/(?P\d{2})/(?P\d{2})$', include(self.feed_patterns('get_entries_by_ymd', 'entry_archive_page', 'entries_by_day'))) - ) + urlpatterns += self.feed_patterns(r'^(?P\d{4})/(?P\d{2})/(?P\d{2})', 'get_entries_by_ymd', 'entry_archive_page', 'entries_by_day') if self.entry_permalink_style == 'D': urlpatterns += patterns('', @@ -514,7 +508,7 @@ class BlogView(FeedView): if 'tags' in extra_context: tags = extra_context['tags'] - feed.feed['link'] = request.node.construct_url(self.reverse(tags), with_domain=True, request=request, secure=request.is_secure()) + feed.feed['link'] = request.node.construct_url(self.reverse(obj=tags), with_domain=True, request=request, secure=request.is_secure()) else: tags = obj.entry_tags @@ -533,7 +527,7 @@ class BlogView(FeedView): item_context = { 'paginator': paginator, 'paginated_page': paginated_page, - self.item_context_var: objects + self.item_context_var: items } else: item_context = { @@ -653,9 +647,8 @@ class NewsletterView(FeedView): @property def urlpatterns(self): - urlpatterns = patterns('', - url(r'^', include(self.feed_patterns('get_all_articles', 'index_page', 'index'))), - url(r'^%s/(?P.+)' % self.issue_permalink_base, include(self.feed_patterns('get_articles_by_issue', 'issue_page', 'issue'))) + urlpatterns = self.feed_patterns(r'^', 'get_all_articles', 'index_page', 'index') + patterns('', + url(r'^%s/(?P.+)$' % self.issue_permalink_base, self.page_view('get_articles_by_issue', 'issue_page'), name='issue') ) if self.issue_archive_page: urlpatterns += patterns('', @@ -666,17 +659,11 @@ class NewsletterView(FeedView): url(r'^%s' % self.article_permalink_base, include(self.feed_patterns('get_all_articles', 'article_archive_page', 'articles'))) ) if self.article_permalink_style in 'DMY': - urlpatterns += patterns('', - url(r'^%s/(?P\d{4})' % self.article_permalink_base, include(self.feed_patterns('get_articles_by_ymd', 'article_archive_page', 'articles_by_year'))) - ) + urlpatterns += self.feed_patterns(r'^%s/(?P\d{4})' % self.article_permalink_base, 'get_articles_by_ymd', 'article_archive_page', 'articles_by_year') if self.article_permalink_style in 'DM': - urlpatterns += patterns('', - url(r'^%s/(?P\d{4})/(?P\d{2})' % self.article_permalink_base, include(self.feed_patterns('get_articles_by_ymd', 'article_archive_page', 'articles_by_month'))) - ) + urlpatterns += self.feed_patterns(r'^%s/(?P\d{4})/(?P\d{2})' % self.article_permalink_base, 'get_articles_by_ymd', 'article_archive_page', 'articles_by_month') if self.article_permalink_style == 'D': - urlpatterns += patterns('', - url(r'^%s/(?P\d{4})/(?P\d{2})/(?P\d{2})' % self.article_permalink_base, include(self.feed_patterns('get_articles_by_ymd', 'article_archive_page', 'articles_by_day'))) - ) + urlpatterns += self.feed_patterns(r'^%s/(?P\d{4})/(?P\d{2})/(?P\d{2})' % self.article_permalink_base, 'get_articles_by_ymd', 'article_archive_page', 'articles_by_day') if self.article_permalink_style == 'Y': urlpatterns += patterns('',