Commits

Federico Hlawaczek  committed 86def40

Alternativas para receptores de los emails de contacto

  • Participants
  • Parent commits 9d9dcc3

Comments (0)

Files changed (1)

File simple_contact_form/models.py

 
 LOG_ACTION_VIEW = 4
 
+if hasattr(settings, 'SIMPLE_CONTACT_FORM'):
+    SIMPLE_CONTACT_FORM_SETTINGS = settings.SIMPLE_CONTACT_FORM
+else:
+    SIMPLE_CONTACT_FORM_SETTINGS = {}
+
 class SimpleContact(models.Model):
-    # 
-    # Model data
-    # 
+    # Model data #######################
     ts = models.DateTimeField(_("Time stamp"), auto_now=True, editable=False)
     from_name = models.CharField(_("Nombre"), max_length=64)
     from_email = models.EmailField(_("Email"))
         ordering = ("-ts", )
         verbose_name = _(u"Contácto")
         verbose_name_plural = _(u"Contáctos")
-    # 
-    # Model instance methods
-    # 
+    # Model instance methods ###########
     def set_viewed(self, user):
         LogEntry.objects.log_action(
             user_id         = user.pk,
                         )
                 .exists()
                 )
-    def notify_users(self, email_template=None):
+    def notify_users(self, email_template=None, recipients=None):
         """
         Sends an email to all users who have permission to view the model in admin.
         First line of the template is the subject
         """
+        # Render emails
         email_template = loader.get_template(email_template or "simple_contact_form/notification_email.txt")
         context = Context({ 'obj': self })
         body = email_template.render(context)
         subject = body.splitlines()[0]
-        for u in User.objects.filter(is_staff=True, is_active=True).exclude(email=""):
-            if u.has_perm("simple_contact_form.change_simplecontact"):
-                send_mail(subject, 
-                          body, 
-                          settings.DEFAULT_FROM_EMAIL, 
-                          (u.email, ), 
-                          fail_silently=True, 
-                          )
+        # Get list of receivers
+        if recipients:
+            recipient_list = recipients
+        elif 'recipient_list' in SIMPLE_CONTACT_FORM_SETTINGS:
+            recipient_list = SIMPLE_CONTACT_FORM_SETTINGS['recipient_list']
+        else:
+            recipient_list = []
+            for u in User.objects.filter(is_staff=True, is_active=True).exclude(email=""):
+                if u.has_perm("simple_contact_form.change_simplecontact"):
+                    recipient_list.append(u.email)
+        # Send email
+        send_mail(subject, 
+                  body, 
+                  settings.DEFAULT_FROM_EMAIL, 
+                  recipient_list, 
+                  fail_silently=True, 
+                  )
     
-    # 
-    # Method overrides
-    # 
+    # Method overrides #################
     def __unicode__(self):
         return "{0} ({1})".format(self.ts, self.from_email)