From faad10487eecd1cf0b0cf8c57442a2a863655ba7 Mon Sep 17 00:00:00 2001 From: Patrick Samson Date: Fri, 17 Jan 2014 21:23:54 +0100 Subject: [PATCH] Allowed 0 for User key; Relaxed the allowed charset for naming the recipients --- docs/features.rst | 10 +++++----- docs/moderation.rst | 2 +- docs/views.rst | 2 +- postman/models.py | 12 ++++++------ postman/tests.py | 24 +++++++++++++++--------- postman/urls.py | 2 +- postman/urls_for_tests.py | 28 ++++++++++++++-------------- 7 files changed, 43 insertions(+), 37 deletions(-) diff --git a/docs/features.rst b/docs/features.rst index 0167ab8..416622a 100644 --- a/docs/features.rst +++ b/docs/features.rst @@ -45,7 +45,7 @@ Example:: urlpatterns = patterns('postman.views', # ... - url(r'^write/(?:(?P[\w.@+-:]+)/)?$', + url(r'^write/(?:(?P[^/#]+)/)?$', WriteView.as_view(max=3), name='postman_write'), # ... @@ -93,7 +93,7 @@ Example:: urlpatterns = patterns('postman.views', # ... - url(r'^write/(?:(?P[\w.@+-:]+)/)?$', + url(r'^write/(?:(?P[^/#]+)/)?$', WriteView.as_view(user_filter=my_user_filter), name='postman_write'), # ... @@ -152,7 +152,7 @@ An example, with the django-relationships application:: urlpatterns = patterns('postman.views', # ... - url(r'^write/(?:(?P[\w.@+-:]+)/)?$', + url(r'^write/(?:(?P[^/#]+)/)?$', WriteView.as_view(exchange_filter=my_exchange_filter), name='postman_write'), # ... @@ -261,7 +261,7 @@ Example:: urlpatterns = patterns('postman.views', # ... - url(r'^write/(?:(?P[\w.@+-:]+)/)?$', + url(r'^write/(?:(?P[^/#]+)/)?$', WriteView.as_view(autocomplete_channels=(None,'anonymous_ac')), name='postman_write'), url(r'^reply/(?P[\d]+)/$', @@ -274,7 +274,7 @@ Example:: urlpatterns = patterns('postman.views', # ... - url(r'^write/(?:(?P[\w.@+-:]+)/)?$', + url(r'^write/(?:(?P[^/#]+)/)?$', WriteView.as_view(autocomplete_channels='write_ac'), name='postman_write'), # ... diff --git a/docs/moderation.rst b/docs/moderation.rst index e133324..fd04078 100644 --- a/docs/moderation.rst +++ b/docs/moderation.rst @@ -36,7 +36,7 @@ Example:: urlpatterns = patterns('postman.views', # ... - url(r'^write/(?:(?P[\w.@+-:]+)/)?$', + url(r'^write/(?:(?P[^/#]+)/)?$', WriteView.as_view(auto_moderators=(mod1, mod2)), name='postman_write'), url(r'^reply/(?P[\d]+)/$', diff --git a/docs/views.rst b/docs/views.rst index 05772f5..17656fd 100644 --- a/docs/views.rst +++ b/docs/views.rst @@ -25,7 +25,7 @@ Examples:: urlpatterns = patterns('postman.views', # ... - url(r'^write/(?:(?P[\w.@+-:]+)/)?$', + url(r'^write/(?:(?P[^/#]+)/)?$', WriteView.as_view(form_classes=(MyCustomWriteForm, MyCustomAnonymousWriteForm)), name='postman_write'), url(r'^reply/(?P[\d]+)/$', diff --git a/postman/models.py b/postman/models.py index eb58dcb..a819f71 100644 --- a/postman/models.py +++ b/postman/models.py @@ -368,7 +368,7 @@ class Message(models.Model): def clean(self): """Check some validity constraints.""" - if not (self.sender_id or self.email): + if not (self.sender_id is not None or self.email): raise ValidationError(ugettext("Undefined sender.")) def clean_moderation(self, initial_status, user=None): @@ -385,11 +385,11 @@ class Message(models.Model): def clean_for_visitor(self): """Do some auto-read and auto-delete, because there is no one to do it (no account).""" - if not self.sender_id: + if self.sender_id is None: # no need to wait for a final moderation status to mark as deleted if not self.sender_deleted_at: self.sender_deleted_at = now() - elif not self.recipient_id: + elif self.recipient_id is None: if self.is_accepted(): if not self.read_at: self.read_at = now() @@ -431,10 +431,10 @@ class Message(models.Model): if self.is_rejected(): # Bypass: for an online user, no need to notify when rejection is immediate. # Only useful for a visitor as an archive copy of the message, otherwise lost. - if not (self.sender_id and is_auto_moderated): - (notify_user if self.sender_id else email_visitor)(self, 'rejection', site) + if not (self.sender_id is not None and is_auto_moderated): + (notify_user if self.sender_id is not None else email_visitor)(self, 'rejection', site) elif self.is_accepted(): - (notify_user if self.recipient_id else email_visitor)(self, 'acceptance', site) + (notify_user if self.recipient_id is not None else email_visitor)(self, 'acceptance', site) def get_dates(self): """Get some dates to restore later.""" diff --git a/postman/tests.py b/postman/tests.py index 75f3666..a26c09f 100644 --- a/postman/tests.py +++ b/postman/tests.py @@ -1,4 +1,4 @@ -""" +""" Test suite. - Do not put 'mailer' in INSTALLED_APPS, it disturbs the emails counting. @@ -287,6 +287,12 @@ class ViewTest(BaseTest): response = self.client.get(url) self.assertContains(response, 'value="bar, foo"') + # because of Custom User Model, do allow almost any character, not only '^[\w.@+-]+$' of the legacy django.contrib.auth.User model + get_user_model().objects.create_user("Le Créac'h", 'foobar@domain.com', 'pass') # even: space, accentued, qootes + url = reverse('postman_write', args=["Le Créac'h"]) + response = self.client.get(url) + self.assertContains(response, 'value="Le Créac'h"') + def test_write_auto_complete(self): "Test the 'autocomplete_channels' parameter." url = reverse('postman_write_auto_complete') @@ -1001,7 +1007,7 @@ class MessageManagerTest(BaseTest): x X--- """ - m1 = self.c12(moderation_status=STATUS_PENDING); + m1 = self.c12(moderation_status=STATUS_PENDING) m2 = self.c12(moderation_status=STATUS_REJECTED, recipient_deleted_at=now()) m3 = self.c12() m3.read_at, m3.thread = now(), m3 @@ -1377,23 +1383,23 @@ class MessageTest(BaseTest): def test_notification_rejection_user(self): "Test notify_users() for rejection, sender is a User." - m = Message.objects.create(subject='s', moderation_status=STATUS_REJECTED, sender = self.user1, recipient=self.user2) + m = Message.objects.create(subject='s', moderation_status=STATUS_REJECTED, sender=self.user1, recipient=self.user2) self.check_notification(m, 1, self.user1.email, is_auto_moderated=False, notice_label='postman_rejection') def test_notification_rejection_user_auto_moderated(self): "Test notify_users() for rejection, sender is a User, and is alerted online." - m = Message.objects.create(subject='s', moderation_status=STATUS_REJECTED, sender = self.user1, recipient=self.user2) + m = Message.objects.create(subject='s', moderation_status=STATUS_REJECTED, sender=self.user1, recipient=self.user2) self.check_notification(m, 0, is_auto_moderated=True) def test_notification_rejection_user_inactive(self): "Test notify_users() for rejection, sender is a User, but must be active." - m = Message.objects.create(subject='s', moderation_status=STATUS_REJECTED, sender = self.user1, recipient=self.user2) + m = Message.objects.create(subject='s', moderation_status=STATUS_REJECTED, sender=self.user1, recipient=self.user2) self.user1.is_active = False self.check_notification(m, 0, is_auto_moderated=False, notice_label='postman_rejection') def test_notification_rejection_user_disable(self): "Test notify_users() for rejection, sender is a User, but emailing is disabled." - m = Message.objects.create(subject='s', moderation_status=STATUS_REJECTED, sender = self.user1, recipient=self.user2) + m = Message.objects.create(subject='s', moderation_status=STATUS_REJECTED, sender=self.user1, recipient=self.user2) settings.POSTMAN_DISABLE_USER_EMAILING = True settings.POSTMAN_NOTIFIER_APP = None self.reload_modules() @@ -1406,18 +1412,18 @@ class MessageTest(BaseTest): def test_notification_acceptance_user(self): "Test notify_users() for acceptance, recipient is a User." - m = Message.objects.create(subject='s', moderation_status=STATUS_ACCEPTED, sender=self.user1, recipient = self.user2) + m = Message.objects.create(subject='s', moderation_status=STATUS_ACCEPTED, sender=self.user1, recipient=self.user2) self.check_notification(m, 1, self.user2.email, notice_label='postman_message') def test_notification_acceptance_user_inactive(self): "Test notify_users() for acceptance, recipient is a User, but must be active." - m = Message.objects.create(subject='s', moderation_status=STATUS_ACCEPTED, sender=self.user1, recipient = self.user2) + m = Message.objects.create(subject='s', moderation_status=STATUS_ACCEPTED, sender=self.user1, recipient=self.user2) self.user2.is_active = False self.check_notification(m, 0, notice_label='postman_message') def test_notification_acceptance_user_disable(self): "Test notify_users() for acceptance, recipient is a User, but emailing is disabled." - m = Message.objects.create(subject='s', moderation_status=STATUS_ACCEPTED, sender=self.user1, recipient = self.user2) + m = Message.objects.create(subject='s', moderation_status=STATUS_ACCEPTED, sender=self.user1, recipient=self.user2) settings.POSTMAN_DISABLE_USER_EMAILING = True settings.POSTMAN_NOTIFIER_APP = None self.reload_modules() diff --git a/postman/urls.py b/postman/urls.py index 1fcc091..71c29fd 100644 --- a/postman/urls.py +++ b/postman/urls.py @@ -105,7 +105,7 @@ urlpatterns = patterns('', url(r'^sent/(?:(?P