def PreSaveHandler(self): """If send_notification is set to True, send a default formatted notification mail""" if not self.send_notification: return (subj, cont) = self._get_changes_texts() if not cont: # If any of these come back as None, it means that nothing actually changed, # or that we don't care to send out notifications about it. return cont = self._build_url() + "\n\n" + cont # Build the mail text msg = MIMEText(cont, _charset='utf-8') msg['Subject'] = "%s by %s" % (subj, get_current_user()) msg['To'] = settings.NOTIFICATION_EMAIL msg['From'] = settings.NOTIFICATION_FROM if hasattr(settings,'SUPPRESS_NOTIFICATIONS') and settings.SUPPRESS_NOTIFICATIONS: print msg.as_string() else: sendmail(msg)
def save_model(self, request, obj, form, change): if change and self.model.send_notification: # We only do processing if something changed, not when adding # a new object. if request.POST.has_key('new_notification') and request.POST['new_notification']: # Need to send off a new notification. We'll also store # it in the database for future reference, of course. if not obj.org.email: # Should not happen because we remove the form field. Thus # a hard exception is ok. raise Exception("Organization does not have an email, canot send notification!") n = ModerationNotification() n.objecttype = obj.__class__.__name__ n.objectid = obj.id n.text = request.POST['new_notification'] n.author = request.user.username n.save() # Now send an email too msgstr = _get_notification_text(request.POST.has_key('remove_after_notify'), obj, request.POST['new_notification']) msg = MIMEText(msgstr, _charset='utf-8') msg['Subject'] = "postgresql.org moderation notification" msg['To'] = obj.org.email msg['From'] = settings.NOTIFICATION_FROM if hasattr(settings,'SUPPRESS_NOTIFICATIONS') and settings.SUPPRESS_NOTIFICATIONS: print msg.as_string() else: sendmail(msg) # Also generate a mail to the moderators msg = MIMEText(_get_moderator_notification_text(request.POST.has_key('remove_after_notify'), obj, request.POST['new_notification'], request.user.username ), _charset='utf-8') msg['Subject'] = "Moderation comment on %s %s" % (obj.__class__._meta.verbose_name, obj.id) msg['To'] = settings.NOTIFICATION_EMAIL msg['From'] = settings.NOTIFICATION_FROM if hasattr(settings,'SUPPRESS_NOTIFICATIONS') and settings.SUPPRESS_NOTIFICATIONS: print msg.as_string() else: sendmail(msg) if request.POST.has_key('remove_after_notify'): # Object should not be saved, it should be deleted obj.delete() return # Either no notifications, or done with notifications super(PgwebAdmin, self).save_model(request, obj, form, change)
def delete(self): # We can't compare the object, but we should be able to construct something anyway if self.send_notification: subject = "%s id %s has been deleted by %s" % (self._meta.verbose_name, self.id, get_current_user()) msg = MIMEText(self.full_text_representation(), _charset="utf-8") msg["Subject"] = subject msg["To"] = settings.NOTIFICATION_EMAIL msg["From"] = settings.NOTIFICATION_FROM if hasattr(settings, "SUPPRESS_NOTIFICATIONS") and settings.SUPPRESS_NOTIFICATIONS: print msg.as_string() else: sendmail(msg) # Now call our super to actually delete the object super(PgModel, self).delete()