Implement ability for moderators to send notices to organisations
authorMagnus Hagander <magnus@hagander.net>
Tue, 26 Jun 2012 12:11:22 +0000 (14:11 +0200)
committerMagnus Hagander <magnus@hagander.net>
Tue, 26 Jun 2012 12:34:26 +0000 (14:34 +0200)
Notices entered will be sent to the organisations email address - so there
needs to be one (if not, the notification field doesn't show up).

Notifications also go in the database, and show up on each object so you
can see the previous history of it, and get emailed to the slaves list.

Finally, it's possible to reject-with-notification, in which case the
notification is sent off to the user and after that the object is deleted.
The notification history stays in the database, but is not linked anywhere
(but can be viewed from the admin interface on that model directly).
Unfortunately, this seems to cause double notifications to the slaves list,
but we'll have to live with that for now.

Closes #137

pgweb/core/admin.py
pgweb/core/models.py
pgweb/util/admin.py
templates/admin/change_form_pgweb.html

index 75ff45d66a0467e08b8fd9d239341edcf96aa561..ff2c44b2059ff827f65b31f9588a7307db141f44 100644 (file)
@@ -20,4 +20,5 @@ admin.site.register(OrganisationType)
 admin.site.register(Organisation, OrganisationAdmin)
 admin.site.register(ImportedRSSFeed)
 admin.site.register(ImportedRSSItem)
+admin.site.register(ModerationNotification)
 
index 50248f8f4c58c670ea65e4be5e8ee64386185bbd..7cc9f156eca089a86722e8b8c31adb6e7abc844d 100644 (file)
@@ -119,3 +119,19 @@ class UserProfile(models.Model):
        user = models.ForeignKey(User, null=False, blank=False, unique=True, primary_key=True)
        sshkey = models.TextField(null=False, blank=True, verbose_name="SSH key", help_text= "Paste one or more public keys in OpenSSH format, one per line.")
        lastmodified = models.DateTimeField(null=False, blank=False, auto_now=True)
+
+# Notifications sent for any moderated content.
+# Yes, we uglify it by storing the type of object as a string, so we don't
+# end up with a bazillion fields being foreign keys. Ugly, but works.
+class ModerationNotification(models.Model):
+       objectid = models.IntegerField(null=False, blank=False, db_index=True)
+       objecttype = models.CharField(null=False, blank=False, max_length=100)
+       text = models.TextField(null=False, blank=False)
+       author = models.CharField(null=False,  blank=False, max_length=100)
+       date = models.DateTimeField(null=False, blank=False, auto_now=True)
+
+       def __unicode__(self):
+               return "%s id %s (%s): %s" % (self.objecttype, self.objectid, self.date, self.text[:50])
+
+       class Meta:
+               ordering = ('-date', )
index 33c12b73173acdb57d5bc1a58329b9f5dc3a65da..0948ebc5e7411a0a17d6be195290d4b840b4c9cc 100644 (file)
@@ -1,4 +1,11 @@
 from django.contrib import admin
+from django.conf import settings
+
+from email.mime.text import MIMEText
+
+from pgweb.core.models import ModerationNotification
+from util.misc import sendmail
+
 
 class PgwebAdmin(admin.ModelAdmin):
        """
@@ -17,6 +24,108 @@ class PgwebAdmin(admin.ModelAdmin):
                        fld.widget.attrs['class'] = fld.widget.attrs['class'] + ' markdown_preview'
                return fld
 
+       def change_view(self, request, object_id, extra_context=None):
+               if self.model.send_notification:
+                       # Anything that sends notification supports manual notifications
+                       if extra_context == None:
+                               extra_context = dict()
+                       extra_context['notifications'] = ModerationNotification.objects.filter(objecttype=self.model.__name__, objectid=object_id).order_by('date')
+
+               return super(PgwebAdmin, self).change_view(request, object_id, extra_context)
+
+       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['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 register_pgwebadmin(model):
        admin.site.register(model, PgwebAdmin)
 
+
+def _get_notification_text(remove, obj, txt):
+       objtype = obj.__class__._meta.verbose_name
+       if remove:
+               return """You recently submitted a %s to postgresql.org.
+
+This submission has been rejected by a moderator, with the following comment:
+
+%s
+""" % (objtype, txt)
+       else:
+               return """You recently submitted a %s to postgresql.org.
+
+During moderation, this item has received comments that need to be
+addressed before it can be approved. The comment given by the moderator is:
+
+%s
+
+Please go to https://www.postgresql.org/account/ and make any changes
+request, and your submission will be re-moderated.
+""" % (objtype, txt)
+
+
+
+def _get_moderator_notification_text(remove, obj, txt, moderator):
+       return """Moderator %s made a comment to a pending object:
+Object type: %s
+Object id: %s
+Comment: %s
+Delete after comment: %s
+""" % (moderator,
+          obj.__class__._meta.verbose_name,
+          obj.id,
+          txt,
+          remove and "Yes" or "No",
+          )
index 57917a634f426c7cec901980dd5395b45aa06873..2aef6102569aa63869bb803f3873d378491ab654 100644 (file)
@@ -23,3 +23,24 @@ Note that the summary field can use
 </script>
 {%endblock%}
 
+{%if notifications%}
+{%block after_field_sets%}
+<h4>Notifications sent for this item</h4>
+<ul>
+ {%for n in notifications%}
+ <li>{{n.text}} by {{n.author}} sent at {{n.date}}</li>
+ {%empty%}
+ <li>No notifications sent for this item</li>
+ {%endfor%}
+</ul>
+<p>
+{%if original.org.email%}
+New notification: <input type="text" name="new_notification" style="width:400px;" /> (<b>Note!</b> This comment is emailed to the organization!)<br/>
+<input type="checkbox" name="remove_after_notify">Delete after notification</>
+{%else%}
+Organisation has <b>no email</b>, so cannot send notifications to it!
+{%endif%}
+</p>
+<hr/>
+{%endblock%}
+{%endif%}