From: Stephen Burrows Date: Mon, 23 Aug 2010 16:23:30 +0000 (-0400) Subject: Implemented feeds for NewsletterView. Corrected BlogView/NewsletterView urlpatterns. X-Git-Tag: philo-0.9~34^2~1^2~1 X-Git-Url: http://git.ithinksw.org/philo.git/commitdiff_plain/0f1b0f543695cc02b2587d22a7da223eeb705022?ds=inline Implemented feeds for NewsletterView. Corrected BlogView/NewsletterView urlpatterns. --- diff --git a/contrib/penfield/models.py b/contrib/penfield/models.py index 50b82d4..f6c827a 100644 --- a/contrib/penfield/models.py +++ b/contrib/penfield/models.py @@ -110,40 +110,50 @@ class BlogView(MultiView, FeedMultiViewMixin): @property def urlpatterns(self): - base_patterns = patterns('', + urlpatterns = patterns('', url(r'^', include(self.feed_patterns(self.get_all_entries, self.index_page, 'index'))), - url((r'^(?:%s)/?$' % self.tag_permalink_base), self.tag_archive_view), url((r'^(?:%s)/(?P[-\w]+[-+/\w]*)/' % self.tag_permalink_base), include(self.feed_patterns(self.get_entries_by_tag, self.tag_page, 'entries_by_tag'))) ) + if self.tag_archive_page: + urlpatterns += patterns('', + url((r'^(?:%s)/?$' % self.tag_permalink_base), self.tag_archive_view) + ) + + if self.entry_archive_page: + if self.entry_permalink_style in 'DMY': + urlpatterns += patterns('', + url(r'^(?P\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\d{4})/(?P\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\d{4})/(?P\d{2})/(?P\d{2})/?$', include(self.feed_patterns(self.get_entries_by_ymd, self.entry_archive_page, 'entries_by_day'))) + ) + if self.entry_permalink_style == 'D': - entry_patterns = patterns('', - url(r'^(?P\d{4})/', include(self.feed_patterns(self.get_entries_by_ymd, self.entry_archive_page, 'entries_by_year'))), - url(r'^(?P\d{4})/(?P\d{2})/?$', include(self.feed_patterns(self.get_entries_by_ymd, self.entry_archive_page, 'entries_by_month'))), - url(r'^(?P\d{4})/(?P\d{2})/(?P\d{2})/?$', include(self.feed_patterns(self.get_entries_by_ymd, self.entry_archive_page, 'entries_by_day'))), + urlpatterns += patterns('', url(r'^(?P\d{4})/(?P\d{2})/(?P\d{2})/(?P[-\w]+)/?$', self.entry_view) ) elif self.entry_permalink_style == 'M': - entry_patterns = patterns('', - url(r'^(?P\d{4})/', include(self.feed_patterns(self.get_entries_by_ymd, self.entry_archive_page, 'entries_by_year'))), - url(r'^(?P\d{4})/(?P\d{2})/?$', include(self.feed_patterns(self.get_entries_by_ymd, self.entry_archive_page, 'entries_by_month'))), + urlpatterns += patterns('', url(r'^(?P\d{4})/(?P\d{2})/(?P[-\w]+)/?$', self.entry_view) ) elif self.entry_permalink_style == 'Y': - entry_patterns = patterns('', - url(r'^(?P\d{4})/', include(self.feed_patterns(self.get_entries_by_ymd, self.entry_archive_page, 'entries_by_year'))), + urlpatterns += patterns('', url(r'^(?P\d{4})/(?P[-\w]+)/?$', self.entry_view) ) elif self.entry_permalink_style == 'B': - entry_patterns = patterns('', - url((r'^(?:%s)/?$' % self.entry_permalink_base), - url(r'^(?P\d{4})/', include(self.feed_patterns(self.get_all_entries, self.entry_archive_page, 'entries_by_year'))),), + urlpatterns += patterns('', url((r'^(?:%s)/(?P[-\w]+)/?$' % self.entry_permalink_base), self.entry_view) ) else: - entry_patterns = patterns('', + urlpatterns = patterns('', url(r'^(?P[-\w]+)/?$', self.entry_view) ) - return base_patterns + entry_patterns + return urlpatterns def get_all_entries(self, request, node=None, extra_context=None): return self.blog.entries.all(), extra_context @@ -183,6 +193,9 @@ class BlogView(MultiView, FeedMultiViewMixin): return entries, extra_context + def get_obj_description(self, obj): + return obj.excerpt + def entry_view(self, request, slug, year=None, month=None, day=None, node=None, extra_context=None): entries = self.blog.entries.all() if year: @@ -245,7 +258,7 @@ class NewsletterIssue(Entity, Titled): register_value_model(NewsletterIssue) -class NewsletterView(MultiView): +class NewsletterView(MultiView, FeedMultiViewMixin): ARTICLE_PERMALINK_STYLE_CHOICES = ( ('D', 'Year, month, and day'), ('M', 'Year and month'), @@ -265,6 +278,13 @@ class NewsletterView(MultiView): article_permalink_base = models.CharField(max_length=255, blank=False, default='articles') issue_permalink_base = models.CharField(max_length=255, blank=False, default='issues') + feed_suffix = models.CharField(max_length=255, blank=False, default=FeedMultiViewMixin.feed_suffix) + feeds_enabled = models.BooleanField() + + @property + def feed_title(self): + return self.newsletter.title + def get_subpath(self, obj): if isinstance(obj, NewsletterArticle): if obj.newsletter == self.newsletter: @@ -283,46 +303,72 @@ class NewsletterView(MultiView): @property def urlpatterns(self): - base_patterns = patterns('', - url(r'^$', self.index_view), - url((r'^(?:%s)/?$' % self.issue_permalink_base), self.issue_archive_view), - url((r'^(?:%s)/(?P\d+)/?$' % self.issue_permalink_base), self.issue_view) - ) - article_patterns = patterns('', - url((r'^(?:%s)/?$' % self.article_permalink_base), self.article_archive_view) + urlpatterns = patterns('', + url(r'^', include(self.feed_patterns(self.get_all_articles, self.index_page, 'index'))), + url(r'^(?:%s)/(?P\d+)/' % self.issue_permalink_base, include(self.feed_patterns(self.get_articles_by_issue, self.issue_page, 'articles_by_issue'))) ) - if self.article_permalink_style in 'DMY': - article_patterns += patterns('', - url((r'^(?:%s)/(?P\d{4})/?$' % self.article_permalink_base), self.article_archive_view) + if self.issue_archive_page: + urlpatterns += patterns('', + url(r'^(?:%s)/$' % self.issue_permalink_base, self.issue_archive_view) ) - if self.article_permalink_style in 'DM': - article_patterns += patterns('', - url((r'^(?:%s)/(?P\d{4})/(?P\d{2})/?$' % self.article_permalink_base), self.article_archive_view) + 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'))) + ) + if self.article_permalink_style in 'DMY': + urlpatterns += patterns('', + url(r'^(?:%s)/(?P\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 == 'D': - article_patterns += patterns('', - url((r'^(?:%s)/(?P\d{4})/(?P\d{2})/(?P\d{2})/?$' % self.article_permalink_base), self.article_archive_view), - url((r'^(?:%s)/(?P\d{4})/(?P\d{2})/(?P\d{2})/(?P[-\w]+)/?$' % self.article_permalink_base), self.article_view) - ) - else: - article_patterns += patterns('', - url((r'^(?:%s)/(?P\d{4})/(?P\d{2})/(?P[-\w]+)/?$' % self.article_permalink_base), self.article_view) + 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(self.get_articles_by_ymd, self.article_archive_page, 'articles_by_month'))) ) - else: - article_patterns += patterns('', - url((r'^(?:%s)/(?P\d{4})/(?P[-\w]+)/?$' % self.article_permalink_base), self.article_view) - ) - else: - article_patterns += patterns('', - url((r'^(?:%s)/(?P[-\w]+)/?$' % self.article_permalink_base), self.article_view) + 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(self.get_articles_by_ymd, self.article_archive_page, 'articles_by_day'))) + ) + + if self.article_permalink_style == 'Y': + urlpatterns += patterns('', + url(r'^(?:%s)/(?P\d{4})/(?P[\w-]+)/$' % self.article_permalink_base, self.article_view) + ) + elif self.article_permalink_style == 'M': + urlpatterns += patterns('', + url(r'^(?:%s)/(?P\d{4})/(?P\d{2})/(?P[\w-]+)/$' % self.article_permalink_base, self.article_view) ) - return base_patterns + article_patterns + elif self.article_permalink_style == 'D': + urlpatterns += patterns('', + url(r'^(?:%s)/(?P\d{4})/(?P\d{2})/(?P\d{2})/(?P[\w-]+)/$' % self.article_permalink_base, self.article_view) + ) + else: + urlpatterns += patterns('', + url(r'^(?:%s)/(?P[-\w]+)/?$' % self.article_permalink_base, self.article_view) + ) + + return urlpatterns - def index_view(self, request, node=None, extra_context=None): - context = {} - context.update(extra_context or {}) - context.update({'newsletter': self.newsletter}) - return self.index_page.render_to_response(node, request, extra_context=context) + def get_context(self): + return {'newsletter': self.newsletter} + + def get_all_articles(self, request, node, extra_context=None): + return self.newsletter.articles.all(), extra_context + + def get_articles_by_ymd(self, request, year, month=None, day=None, node=None, extra_context=None): + articles = self.newsletter.articles.filter(dat__year=year) + if month: + articles = articles.filter(date__month=month) + if day: + articles = articles.filter(date__day=day) + return articles + + def get_articles_by_issue(self, request, number, node=None, extra_context=None): + try: + issue = self.newsletter.issues.get(number=number) + except: + raise Http404 + context = extra_context or {} + context.update({'issue': issue}) + return issue.articles.all(), context def article_view(self, request, slug, year=None, month=None, day=None, node=None, extra_context=None): articles = self.newsletter.articles.all() @@ -356,20 +402,13 @@ class NewsletterView(MultiView): context.update({'newsletter': self.newsletter, 'year': year, 'month': month, 'day': day, 'articles': articles}) return self.article_archive_page.render_to_response(node, request, extra_context=context) - def issue_view(self, request, number, node=None, extra_context=None): - try: - issue = self.newsletter.issues.get(number=number) - except: - raise Http404 - context = {} - context.update(extra_context or {}) - context.update({'newsletter': self.newsletter, 'issue': issue}) - return self.issue_page.render_to_response(node, request, extra_context=context) - def issue_archive_view(self, request, node=None, extra_context=None): if not self.issue_archive_page: raise Http404 context = {} context.update(extra_context or {}) context.update({'newsletter': self.newsletter}) - return self.issue_archive_page.render_to_response(node, request, extra_context=context) \ No newline at end of file + return self.issue_archive_page.render_to_response(node, request, extra_context=context) + + def get_obj_description(self, obj): + return obj.lede or obj.full_text \ No newline at end of file diff --git a/contrib/penfield/utils.py b/contrib/penfield/utils.py index 6f2d41d..7b5e946 100644 --- a/contrib/penfield/utils.py +++ b/contrib/penfield/utils.py @@ -21,7 +21,7 @@ class FeedMultiViewMixin(object): Wraps an object-fetching function and renders the results as a page. """ def inner(request, node=None, extra_context=None, **kwargs): - objects, extra_context = func(request, node, extra_context, **kwargs) + objects, extra_context = func(request=request, node=node, extra_context=extra_context, **kwargs) context = self.get_context() context.update(extra_context or {}) @@ -42,7 +42,7 @@ class FeedMultiViewMixin(object): Wraps an object-fetching function and renders the results as a rss or atom feed. """ def inner(request, node=None, extra_context=None, **kwargs): - objects, extra_context = func(request, node, extra_context, **kwargs) + objects, extra_context = func(request=request, node=node, extra_context=extra_context, **kwargs) if 'HTTP_ACCEPT' in request.META and 'rss' in request.META['HTTP_ACCEPT'] and 'atom' not in request.META['HTTP_ACCEPT']: feed_type = 'rss' @@ -52,7 +52,7 @@ class FeedMultiViewMixin(object): feed = self.get_feed(feed_type, request, node, kwargs, reverse_name) for obj in objects: - feed.add_item(obj.title, '/%s/%s/' % (node.get_absolute_url().strip('/'), self.get_subpath(obj).strip('/')), description=obj.excerpt) + feed.add_item(obj.title, '/%s/%s/' % (node.get_absolute_url().strip('/'), self.get_subpath(obj).strip('/')), description=self.get_obj_description(obj)) response = HttpResponse(mimetype=feed.mime_type) feed.write(response, 'utf-8') @@ -75,4 +75,7 @@ class FeedMultiViewMixin(object): url(r'^%s/$' % self.feed_suffix, self.feed_view(object_fetcher, feed_name), name=feed_name), url(r'^$', self.page_view(object_fetcher, page), name=base_name) ) - return urlpatterns \ No newline at end of file + return urlpatterns + + def get_obj_description(self, obj): + raise NotImplementedError \ No newline at end of file