From 081fea4d2b4368527c5e29441346339df85f2d81 Mon Sep 17 00:00:00 2001 From: Stephen Burrows Date: Thu, 17 Feb 2011 16:57:54 -0500 Subject: [PATCH] Switched feed_patterns to return urlpatterns suitable for addition to urlpatterns instead of inclusion in urlpatterns. Adjusted penfield urlpatterns accordingly. Added default behavior for FeedView.get_feed in cases where a known feed_type is not supplied for whatever reason. Added RegistrationMultiView.registration_form attribute. --- contrib/penfield/models.py | 60 +++++++++++++++----------------------- contrib/waldo/forms.py | 8 ++--- contrib/waldo/models.py | 11 +++---- 3 files changed, 33 insertions(+), 46 deletions(-) diff --git a/contrib/penfield/models.py b/contrib/penfield/models.py index a616faa..065d033 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,22 +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('', - # include an optional slash at the beginning in case someone uses an include - # with a base that doesn't include a slash. - 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): @@ -123,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? @@ -379,16 +383,11 @@ class BlogView(FeedView): @property def urlpatterns(self): - urlpatterns = patterns('', - url(r'^', include(self.feed_patterns('get_all_entries', 'index_page', 'index'))), - ) + urlpatterns = self.feed_patterns(r'^', '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'^%s/(?P[-\w]+[-+/\w]*)$' % self.tag_permalink_base, 'get_entries_by_tag', 'tag_page', 'get_entries_by_tag') + if self.tag_archive_page: urlpatterns += patterns('', url((r'^%s$' % self.tag_permalink_base), self.tag_archive_view, name='tag_archive') @@ -396,17 +395,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('', @@ -656,9 +649,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('', @@ -669,17 +661,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('', diff --git a/contrib/waldo/forms.py b/contrib/waldo/forms.py index de3349a..6f2b59b 100644 --- a/contrib/waldo/forms.py +++ b/contrib/waldo/forms.py @@ -96,7 +96,7 @@ class WaldoAuthenticationForm(AuthenticationForm): def check_for_test_cookie(self): # This method duplicates the Django 1.3 AuthenticationForm method. - if self.request and not self.request.session.test_cookie_worked(): - raise forms.ValidationError( - _("Your Web browser doesn't appear to have cookies enabled. " - "Cookies are required for logging in.")) \ No newline at end of file + if self.request and not self.request.session.test_cookie_worked(): + raise forms.ValidationError( + _("Your Web browser doesn't appear to have cookies enabled. " + "Cookies are required for logging in.")) \ No newline at end of file diff --git a/contrib/waldo/models.py b/contrib/waldo/models.py index f47dbd6..4ff609b 100644 --- a/contrib/waldo/models.py +++ b/contrib/waldo/models.py @@ -23,9 +23,7 @@ import urlparse class LoginMultiView(MultiView): """ - Handles login, registration, and forgotten passwords. In other words, this - multiview provides exclusively view and methods related to usernames and - passwords. + Handles exclusively methods and views related to logging users in and out. """ login_page = models.ForeignKey(Page, related_name='%(app_label)s_%(class)s_login_related') login_form = WaldoAuthenticationForm @@ -129,6 +127,7 @@ class LoginMultiView(MultiView): class PasswordMultiView(LoginMultiView): + "Adds on views for password-related functions." password_reset_page = models.ForeignKey(Page, related_name='%(app_label)s_%(class)s_password_reset_related', blank=True, null=True) password_reset_confirmation_email = models.ForeignKey(Page, related_name='%(app_label)s_%(class)s_password_reset_confirmation_email_related', blank=True, null=True) password_set_page = models.ForeignKey(Page, related_name='%(app_label)s_%(class)s_password_set_related', blank=True, null=True) @@ -251,8 +250,10 @@ class PasswordMultiView(LoginMultiView): class RegistrationMultiView(PasswordMultiView): + """Adds on the pages necessary for letting new users register.""" register_page = models.ForeignKey(Page, related_name='%(app_label)s_%(class)s_register_related', blank=True, null=True) register_confirmation_email = models.ForeignKey(Page, related_name='%(app_label)s_%(class)s_register_confirmation_email_related', blank=True, null=True) + registration_form = RegistrationForm @property def urlpatterns(self): @@ -269,7 +270,7 @@ class RegistrationMultiView(PasswordMultiView): return HttpResponseRedirect(request.node.get_absolute_url()) if request.method == 'POST': - form = RegistrationForm(request.POST) + form = self.registration_form(request.POST) if form.is_valid(): user = form.save() context = { @@ -280,7 +281,7 @@ class RegistrationMultiView(PasswordMultiView): messages.add_message(request, messages.SUCCESS, 'An email has been sent to %s with details on activating your account.' % user.email, fail_silently=True) return HttpResponseRedirect(request.node.get_absolute_url()) else: - form = RegistrationForm() + form = self.registration_form() context = self.get_context() context.update(extra_context or {}) -- 2.20.1