]> git.parisson.com Git - django-postman.git/commitdiff
Added the setting POSTMAN_DISABLE_USER_EMAILING ; No need for an immediate rejection...
authorPatrick Samson <pk.samson@gmail.com>
Sun, 15 Jan 2012 18:16:50 +0000 (19:16 +0100)
committerPatrick Samson <pk.samson@gmail.com>
Sun, 15 Jan 2012 18:16:50 +0000 (19:16 +0100)
docs/conf.py
docs/notification.rst
docs/quickstart.rst
postman/__init__.py
postman/admin.py
postman/models.py
postman/tests.py
postman/utils.py

index 4a9c0ebcadaf71b4d9d83c5563f43f5cdd169171..11ce12910c198421ec55aff4276d1137dd69e563 100644 (file)
@@ -45,9 +45,9 @@ copyright = u'2010, Patrick Samson'
 # built documents.\r
 #\r
 # The short X.Y version.\r
-version = '1.0'\r
+version = '1.1'\r
 # The full version, including alpha/beta/rc tags.\r
-release = '1.0'\r
+release = '1.1'\r
 \r
 # The language for content autogenerated by Sphinx. Refer to documentation\r
 # for a list of supported languages.\r
index ec0de66e964eb23e1e7ea7b372ca345cd3b3db92..5be29800adc2ad59c1ba0244b2880ff724ca9032 100644 (file)
@@ -26,6 +26,9 @@ and design yours.
 \r
 For users\r
 ---------\r
+Special case: In case of a rejection by the auto moderation feature, the user is immediately aware of it,\r
+so there is no need for a notification in addition.\r
+\r
 If a notifier application is configured (see :ref:`optional_settings`), the following labels are used:\r
 \r
 * ``postman_rejection`` to notify the sender of the rejection\r
index 9653a93d7866ffde0ca21c9dd91349044b4abdb5..2d9ba3491bfb53a1fa359a3bed0dc9f521177421 100644 (file)
@@ -71,6 +71,13 @@ You may specify some additional configuration options in your :file:`settings.py
     Set it to True if you do not allow additional recipients when replying.\r
 \r
     *Defaults to*: False.\r
+\r
+``POSTMAN_DISABLE_USER_EMAILING``\r
+    Set it to True if you do not want basic email notification to users.\r
+    This setting does not apply to visitors (refer to ``POSTMAN_DISALLOW_ANONYMOUS``),\r
+    nor to a notifier application (refer to ``POSTMAN_NOTIFIER_APP``)\r
+\r
+    *Defaults to*: False.\r
     \r
 ``POSTMAN_AUTO_MODERATE_AS``\r
     The default moderation status when no auto-moderation functions, if any, were decisive.\r
@@ -190,6 +197,7 @@ Examples
     # POSTMAN_DISALLOW_ANONYMOUS = True # default is False\r
     # POSTMAN_DISALLOW_MULTIRECIPIENTS = True # default is False\r
     # POSTMAN_DISALLOW_COPIES_ON_REPLY = True # default is False\r
+    # POSTMAN_DISABLE_USER_EMAILING = True # default is False\r
     # POSTMAN_AUTO_MODERATE_AS = True # default is None\r
     # POSTMAN_NOTIFIER_APP = None # default is 'notification'\r
     # POSTMAN_MAILER_APP = None # default is 'mailer'\r
index 1b706ded33b16ac3f441f0c9c2fa47962babc2a6..06d9f561e446507a571afc65a37bfd850ae234e7 100644 (file)
@@ -1,7 +1,7 @@
 """A messaging application for Django"""
 
 # following PEP 386: N.N[.N]+[{a|b|c|rc}N[.N]+][.postN][.devN]
-VERSION = (1, 0, 1)
+VERSION = (1, 1, 0)
 PREREL = ()
 POST = 0
 DEV = 0
index d1286bee95aa21fe39b1a6e3ded4c978f1c3fba4..a904769a05673ccd89c3e30ac5d52775d4b90aaa 100644 (file)
@@ -130,7 +130,7 @@ class MessageAdmin(admin.ModelAdmin):
         obj.clean_for_visitor()
         super(MessageAdmin, self).save_model(request, obj, form, change)
         obj.update_parent(form.initial_status)
-        obj.notify_users(form.initial_status)
+        obj.notify_users(form.initial_status, is_auto_moderated=False)
 
 class PendingMessageAdminForm(forms.ModelForm):
     class Meta:
index d3d5af64a38e5efe46611fa68eb0ed292e84239b..717017881e45ef808674675494353e45e0aa5770 100644 (file)
@@ -389,11 +389,14 @@ class Message(models.Model):
                         parent.replied_at = None
                     parent.save()
 
-    def notify_users(self, initial_status):
+    def notify_users(self, initial_status, is_auto_moderated=True):
         """Notify the rejection (to sender) or the acceptance (to recipient) of the message."""
         if initial_status == STATUS_PENDING:
             if self.is_rejected():
-                (notify_user if self.sender_id else email_visitor)(self, 'rejection')
+                # 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')
             elif self.is_accepted():
                 (notify_user if self.recipient_id else email_visitor)(self, 'acceptance')
 
index 683af087f2d6de755dfd9e7cca755987300db175..40c4d4d713657a8e81735009bcf5bbd1bc38a59e 100644 (file)
@@ -52,7 +52,8 @@ from postman.fields import CommaSeparatedUserField
 from postman.models import ORDER_BY_KEY, ORDER_BY_MAPPER, Message, PendingMessage,\
     STATUS_PENDING, STATUS_ACCEPTED, STATUS_REJECTED
 from postman.urls import OPTION_MESSAGES
-from postman.utils import format_body, format_subject, notification
+# because of reload()'s, do "from postman.utils import notification" just before needs
+from postman.utils import format_body, format_subject
 
 if not 'pagination' in settings.INSTALLED_APPS:
     try:
@@ -69,7 +70,7 @@ class GenericTest(TestCase):
     Usual generic tests.
     """
     def test_version(self):
-        self.assertEqual(sys.modules['postman'].__version__, "1.0.1")
+        self.assertEqual(sys.modules['postman'].__version__, "1.1.0")
 
 class BaseTest(TestCase):
     """
@@ -83,11 +84,11 @@ class BaseTest(TestCase):
             'POSTMAN_DISALLOW_ANONYMOUS',
             'POSTMAN_DISALLOW_MULTIRECIPIENTS',
             'POSTMAN_DISALLOW_COPIES_ON_REPLY',
+            'POSTMAN_DISABLE_USER_EMAILING',
             'POSTMAN_AUTO_MODERATE_AS',
         ):
             if hasattr(settings, a):
                 delattr(settings, a)
-        settings.POSTMAN_NOTIFIER_APP = None
         settings.POSTMAN_MAILER_APP = None
         settings.POSTMAN_AUTOCOMPLETER_APP = {
             'arg_default': 'postman_single', # no default, mandatory to enable the feature
@@ -171,6 +172,7 @@ class BaseTest(TestCase):
         "Reload some modules after a change in settings."
         clear_url_caches()
         try:
+            reload(sys.modules['postman.utils'])
             reload(sys.modules['postman.forms'])
             reload(sys.modules['postman.views'])
             reload(sys.modules['postman.urls'])
@@ -449,7 +451,6 @@ class ViewTest(BaseTest):
         url = reverse('postman_reply_auto_complete', args=[pk])
         self.assert_(self.client.login(username='foo', password='pass'))
         response = self.client.get(url)
-        # print response.context
         f = response.context['form'].fields['recipients']
         if hasattr(f, 'channel'):
             self.assertEqual(f.channel, 'postman_multiple')
@@ -1269,50 +1270,72 @@ class MessageTest(BaseTest):
         # note: accepted -> pending, with no other suitable reply
         # is covered in the accepted -> rejected case
 
-    def check_notification(self, m, mail_number, email=None, notice_label=None):
+    def check_notification(self, m, mail_number, email=None, is_auto_moderated=True, notice_label=None):
         "Check number of mails, recipient, and notice creation."
-        m.notify_users(STATUS_PENDING)
+        m.notify_users(STATUS_PENDING, is_auto_moderated)
         self.assertEqual(len(mail.outbox), mail_number)
         if mail_number:
             self.assertEqual(mail.outbox[0].to, [email])
+        from utils import notification
         if notification and notice_label:
             notice = notification.Notice.objects.get()
             self.assertEqual(notice.notice_type.label, notice_label)
 
     def test_notification_rejection_visitor(self):
-        "Test notify_users() for rejection, from a visitor."
+        "Test notify_users() for rejection, sender is a visitor."
         m = Message.objects.create(subject='s', moderation_status=STATUS_REJECTED, email=self.email, recipient=self.user2)
         self.check_notification(m, 1, self.email)
 
     def test_notification_rejection_user(self):
-        "Test notify_users() for rejection, from a User."
+        "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)
+        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)
-        self.check_notification(m, 1, self.user1.email, notice_label='postman_rejection')
+        self.check_notification(m, 0, is_auto_moderated=True)
 
     def test_notification_rejection_user_inactive(self):
-        "Test notify_users() for rejection, from a User, but must be active."
+        "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)
         self.user1.is_active = False
-        self.check_notification(m, 0, notice_label='postman_rejection')
+        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)
+        settings.POSTMAN_DISABLE_USER_EMAILING = True
+        settings.POSTMAN_NOTIFIER_APP = None
+        self.reload_modules()
+        self.check_notification(m, 0, is_auto_moderated=False)
 
     def test_notification_acceptance_visitor(self):
-        "Test notify_users() for acceptance, to a visitor."
+        "Test notify_users() for acceptance, recipient is a visitor."
         m = Message.objects.create(subject='s', moderation_status=STATUS_ACCEPTED, sender=self.user1, email=self.email)
         self.check_notification(m, 1, self.email)
 
     def test_notification_acceptance_user(self):
-        "Test notify_users() for acceptance, to a User."
+        "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)
         self.check_notification(m, 1, self.user2.email, notice_label='postman_message')
 
     def test_notification_acceptance_user_inactive(self):
-        "Test notify_users() for acceptance, to a User, but must be active."
+        "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)
         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)
+        settings.POSTMAN_DISABLE_USER_EMAILING = True
+        settings.POSTMAN_NOTIFIER_APP = None
+        self.reload_modules()
+        self.check_notification(m, 0, notice_label='postman_message')
+
     def test_notification_acceptance_reply(self):
-        "Test notify_users() for acceptance, for a reply, to a User."
+        "Test notify_users() for acceptance, for a reply, recipient is a User."
         p = Message.objects.create(subject='s', moderation_status=STATUS_ACCEPTED, sender=self.user2, recipient=self.user1)
         m = Message.objects.create(subject='s', moderation_status=STATUS_ACCEPTED, sender=self.user1, recipient=self.user2,
             parent=p, thread=p)
index d148e616c26bd76558ee2bfa3372a75ef80f3248..b12cbe528c55807795114f37b8c9034dffa395c0 100644 (file)
@@ -26,6 +26,9 @@ if name and name in settings.INSTALLED_APPS:
 else:
     from django.core.mail import send_mail
 
+# to disable email notification to users
+DISABLE_USER_EMAILING = getattr(settings, 'POSTMAN_DISABLE_USER_EMAILING', False)
+
 # default wrap width; referenced in forms.py
 WRAP_WIDTH = 55
 
@@ -66,11 +69,8 @@ def email(subject_template, message_template, recipient_list, object, action=Non
     # Email subject *must not* contain newlines
     subject = ''.join(subject.splitlines())
     message = render_to_string(message_template, ctx_dict)
-    if settings.DEBUG and getattr(settings, 'DEV_DEBUG', False):
-        print "email from:", settings.DEFAULT_FROM_EMAIL, " - to:", recipient_list, " - subject:", subject
-        print message
-    else:
-        send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, recipient_list, fail_silently=True)
+    # during the development phase, consider using the setting: EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
+    send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, recipient_list, fail_silently=True)
 
 def email_visitor(object, action):
     """Email a visitor."""
@@ -90,5 +90,5 @@ def notify_user(object, action):
     if notification:
         notification.send(users=[user], label=label, extra_context={'message': object, 'action': action})
     else:
-        if user.email and user.is_active:
+        if not DISABLE_USER_EMAILING and user.email and user.is_active:
             email('postman/email_user_subject.txt', 'postman/email_user.txt', [user.email], object, action)