Very simple implementation of email notifications.
authorAlexandre Segura <mex.zktk@gmail.com>
Tue, 21 Mar 2017 13:42:17 +0100
changeset 458 4d7a01e357f1
parent 457 312239eca2e1
child 459 99327a255e04
Very simple implementation of email notifications.
src/iconolab/signals/handlers.py
--- a/src/iconolab/signals/handlers.py	Tue Mar 21 11:58:09 2017 +0100
+++ b/src/iconolab/signals/handlers.py	Tue Mar 21 13:42:17 2017 +0100
@@ -1,4 +1,7 @@
 from django.apps import apps
+from django.conf import settings
+from django.core.mail import send_mail
+from django.core.urlresolvers import reverse
 from django.db.models.signals import post_save
 from django.dispatch import Signal, receiver
 from notifications.signals import notify
@@ -11,6 +14,45 @@
 revision_rejected = Signal(providing_args=['instance'])
 revision_created = Signal(providing_args=['instance'])
 
+VERB_NEW_COMMENT = 'a écrit un commentaire sur votre annotation'
+VERB_NEW_REPLY = 'a répondu à votre commentaire'
+VERB_NEW_ANNOTATION = 'a proposé une révision sur votre annotation'
+VERB_ACCEPTED_REVISION = 'a étudié votre révision'
+VERB_REQUEST_FOR_CONTRIBUTION = 'a fait un appel à contribution'
+VERB_REQUEST_FOR_EXPERTISE = 'a fait un appel à expertise'
+
+class EmailManager():
+
+    def __message_content(self):
+        return 'Connectez-vous pour voir les notifications \n\n' + settings.BASE_URL + reverse('user_notifications')
+
+    def __send_mail(self, recipient, verb):
+        send_mail(
+            'Un utilisateur ' + verb,
+            self.__message_content(),
+            settings.CONTACT_EMAIL,
+            [recipient.email]
+        )
+
+    def new_comment(self, recipient):
+        self.__send_mail(recipient, VERB_NEW_COMMENT)
+
+    def new_reply(self, recipient):
+        self.__send_mail(recipient, VERB_NEW_REPLY)
+
+    def new_revision(self, recipient):
+        self.__send_mail(recipient, VERB_NEW_ANNOTATION)
+
+    def accepted_revision(self, recipient):
+        self.__send_mail(recipient, VERB_ACCEPTED_REVISION)
+
+    def request_for_contribution(self, recipient):
+        self.__send_mail(recipient, VERB_REQUEST_FOR_CONTRIBUTION)
+
+    def request_for_expertise(self, recipient):
+        self.__send_mail(recipient, VERB_REQUEST_FOR_EXPERTISE)
+
+emailManager = EmailManager()
 
 def increment_stats_on_new_revision(sender, instance, **kwargs):
     """
@@ -34,8 +76,8 @@
             image = instance.annotation.image
             image.stats.submitted_revisions_count += 1
             image.stats.set_tags_stats()
-            image.stats.save()  
-    
+            image.stats.save()
+
 def increment_stats_on_new_comment(sender, instance, created, **kwargs):
     """
         Signal to increment stats on annotation when a comment is posted
@@ -103,9 +145,9 @@
 def update_annotation_validation_state(sender, instance, created, **kwargs):
     """
         Example signal to check if a metacategory added on an annotation is enough to make the annotation "validated"
-        
-        We listen to saves on the m2m for the metacategory count in annotation stats, 
-        check if the corresponding metacategory can affect the validation state, 
+
+        We listen to saves on the m2m for the metacategory count in annotation stats,
+        check if the corresponding metacategory can affect the validation state,
         check if the state wasn't overriden, and compute the state again if checks were passed
     """
     from iconolab.models import MetaCategory, MetaCategoryInfo, MetaCategoriesCountInfo
@@ -130,7 +172,7 @@
             if non_neutral_mtcgs_count > 3 and (validating_mtcgs_count / non_neutral_mtcgs_count < 0.4):
                 annotation.validation_state = False
                 annotation.save()
-        
+
 def notify_users_on_new_comment(sender, instance, **kwargs):
     """
         Signal to notify users when a comment is created. Notified users are: annotation author, parent comment author
@@ -144,11 +186,13 @@
             if instance.level > 0: # We check parent_id as django comment xtd saves comments in two steps and only set the information we need in the second step
                 parent_comment = IconolabComment.objects.get(id=instance.parent_id)
                 if parent_comment.user != instance.user:
-                    notify.send(instance.user, recipient=parent_comment.user, verb='a répondu à votre commentaire', action_object=instance, target=comment_annotation)
+                    emailManager.new_reply(parent_comment.user)
+                    notify.send(instance.user, recipient=parent_comment.user, verb=VERB_NEW_REPLY, action_object=instance, target=comment_annotation, emailed=True)
                     if parent_comment.user == comment_annotation.author:
                         notified_author = True
             if instance.user != comment_annotation.author and not notified_author:
-                notify.send(instance.user, recipient=comment_annotation.author, verb='a écrit un commentaire sur votre annotation', action_object=instance, target=comment_annotation)         
+                emailManager.new_comment(comment_annotation.author)
+                notify.send(instance.user, recipient=comment_annotation.author, verb=VERB_NEW_COMMENT, action_object=instance, target=comment_annotation, emailed=True)
 
 def notify_users_on_metacategory(sender, instance, created, **kwargs):
     """
@@ -162,25 +206,30 @@
             comment_annotation = Annotation.objects.prefetch_related("image__item__collection").get(id=related_comment.object_pk)
             if related_metacategory.triggers_notifications == MetaCategory.COMMENTERS:
                 for commenter in comment_annotation.stats.commenters.exclude(id=related_comment.user.id).all():
-                    notify.send(related_comment.user, recipient=commenter, verb='a fait un appel à contribution', action_object=related_comment, target=comment_annotation)
+                    emailManager.request_for_contribution(commenter)
+                    notify.send(related_comment.user, recipient=commenter, verb=VERB_REQUEST_FOR_CONTRIBUTION, action_object=related_comment, target=comment_annotation, emailed=True)
             elif related_metacategory.triggers_notifications == MetaCategory.CONTRIBUTORS:
                 for contributor in comment_annotation.stats.contributors.exclude(id=related_comment.user.id).all():
-                    notify.send(related_comment.user, recipient=contributor, verb='a fait un appel à contribution', action_object=related_comment, target=comment_annotation)
+                    emailManager.request_for_contribution(contributor)
+                    notify.send(related_comment.user, recipient=contributor, verb=VERB_REQUEST_FOR_CONTRIBUTION, action_object=related_comment, target=comment_annotation, emailed=True)
             if related_metacategory.triggers_notifications == MetaCategory.COLLECTION_ADMINS:
                 for collection_admin in comment_annotation.image.item.collection.admins.all():
-                    notify.send(related_comment.user, recipient=collection_admin.user, verb='a fait un appel à expertise', action_object=related_comment, target=comment_annotation)
-        
+                    emailManager.request_for_expertise(collection_admin.user)
+                    notify.send(related_comment.user, recipient=collection_admin.user, verb=VERB_REQUEST_FOR_EXPERTISE, action_object=related_comment, target=comment_annotation, emailed=True)
+
 def notify_users_on_new_revision(sender, instance, **kwargs):
     from iconolab.models import AnnotationRevision
     if sender == AnnotationRevision:
         if instance.author != instance.annotation.author:
-            notify.send(instance.author, recipient=instance.annotation.author, verb='a proposé une révision sur votre annotation', action_object=instance, target=instance.annotation)       
-        
+            emailManager.new_revision(instance.annotation.author)
+            notify.send(instance.author, recipient=instance.annotation.author, verb=VERB_NEW_ANNOTATION, action_object=instance, target=instance.annotation, emailed=True)
+
 def notify_users_on_accepted_revision(sender, instance, **kwargs):
     from iconolab.models import AnnotationRevision
     if sender == AnnotationRevision:
         if instance.author != instance.annotation.author and instance.state in [AnnotationRevision.ACCEPTED, AnnotationRevision.STUDIED]:
-            notify.send(instance.annotation.author, recipient=instance.author, verb='a étudié votre révision', action_object=instance, target=instance.annotation)       
+            emailManager.accepted_revision(instance.author)
+            notify.send(instance.annotation.author, recipient=instance.author, verb=VERB_ACCEPTED_REVISION, action_object=instance, target=instance.annotation, emailed=True)
 
 def create_user_profile(sender, instance, created, **kwargs):
     from iconolab.models import UserProfile
@@ -204,4 +253,4 @@
 post_save.connect(notify_users_on_metacategory)
 revision_created.connect(notify_users_on_new_revision)
 revision_accepted.connect(notify_users_on_accepted_revision)
-        
\ No newline at end of file
+