notification for new publications
authorJan Szejko <janek37@gmail.com>
Fri, 20 Jan 2017 11:27:24 +0000 (12:27 +0100)
committerJan Szejko <janek37@gmail.com>
Fri, 20 Jan 2017 11:27:24 +0000 (12:27 +0100)
apps/catalogue/migrations/0011_document_published.py [new file with mode: 0644]
apps/catalogue/models/document.py
apps/catalogue/views.py
apps/organizations/forms.py
redakcja/settings/__init__.py
redakcja/utlis.py [new file with mode: 0644]

diff --git a/apps/catalogue/migrations/0011_document_published.py b/apps/catalogue/migrations/0011_document_published.py
new file mode 100644 (file)
index 0000000..c2246f6
--- /dev/null
@@ -0,0 +1,25 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+def set_published(apps, schema_editor):
+    Document = apps.get_model('catalogue', 'Document')
+    Document.objects.exclude(publish_log=None).update(published=True)
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('catalogue', '0010_auto_20170119_1321'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='document',
+            name='published',
+            field=models.BooleanField(default=False),
+        ),
+        migrations.RunPython(set_published),
+    ]
index 38c2110..4f08480 100755 (executable)
@@ -26,6 +26,8 @@ class Document(Ref):
     assigned_to = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, related_name='assignments')
     deleted = models.BooleanField(default=False)
     tags = models.ManyToManyField(Tag, blank=True)
+    # we need to know if it were ever published (for notifications)
+    published = models.BooleanField(default=False)
 
     # Where to cache searchable stuff from metadata?
     # Probably in some kind of search index.
index bde9dc0..658b3d4 100644 (file)
@@ -13,6 +13,7 @@ from django.conf import settings
 from django.contrib import auth
 from django.contrib.auth.models import User
 from django.contrib.auth.decorators import login_required
+from django.contrib.sites.models import Site
 from django.core.urlresolvers import reverse
 from django import http
 from django.http import Http404, HttpResponse
@@ -24,6 +25,7 @@ from django.views.decorators.http import require_POST
 from catalogue import forms
 from catalogue.helpers import active_tab
 from librarian import BuildError
+from redakcja.utlis import send_notify_email
 from .constants import STAGES
 from .models import Document, Plan
 from dvcs.models import Revision
@@ -404,7 +406,17 @@ def publish(request, pk):
         # FIXME: check if in tree
         # if PublishRecord.objects.filter(revision=rev, document=doc).exists():
         #     return http.HttpResponse('exists')
+        if not doc.published:
+            site = Site.objects.get_current()
+            send_notify_email(
+                'New published document in MIL/PEER',
+                '''New published document in MIL/PEER: %s. View it in browser: https://%s%s.
+
+--
+MIL/PEER team.''' % (doc.meta()['title'], site.domain, reverse('catalogue_html', args=[doc.pk])))
         PublishRecord.objects.create(revision=rev, document=doc, user=request.user)
+        doc.published = True
+        doc.save()
         if request.is_ajax():
             return http.HttpResponse('ok')
         else:
index 298da48..5580766 100644 (file)
@@ -4,10 +4,9 @@
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
 from django import forms
-from constance import config
 from django.contrib.sites.models import Site
-from django.core.mail import send_mail
 
+from redakcja.utlis import send_notify_email
 from .models import Organization, UserCard, countries
 
 
@@ -21,13 +20,12 @@ class OrganizationForm(forms.ModelForm):
     def save(self, commit=True):
         organization = super(OrganizationForm, self).save(commit=commit)
         site = Site.objects.get_current()
-        send_mail(
+        send_notify_email(
             'New organization in MIL/PEER',
             '''New organization in MIL/PEER: %s. View their profile: https://%s%s.
 
 --
-MIL/PEER team.''' % (organization.name, site.domain, organization.get_absolute_url()),
-            'milpeer@mdrn.pl', [config.NOTIFY_EMAIL])
+MIL/PEER team.''' % (organization.name, site.domain, organization.get_absolute_url()))
         return organization
 
 
index 68a2c2f..39823f5 100644 (file)
@@ -20,7 +20,8 @@ DATABASES = {
 
 CONSTANCE_CONFIG = {
     'NOTIFY_EMAIL': ('zofia.radziwill@nowoczesnapolska.org.pl',
-                     'e-mail address for notifications about new organizations and published projects'),
+                     'e-mail addresses (separated with semicolons) to be notified about new organizations '
+                     'and published documents'),
 }
 
 CONSTANCE_BACKEND = 'constance.backends.database.DatabaseBackend'
diff --git a/redakcja/utlis.py b/redakcja/utlis.py
new file mode 100644 (file)
index 0000000..806d106
--- /dev/null
@@ -0,0 +1,12 @@
+# -*- coding: utf-8 -*-
+#
+# This file is part of MIL/PEER, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
+#
+from constance import config
+from django.core.mail import send_mail
+
+
+def send_notify_email(subject, content):
+    addresses = [part.strip() for part in config.NOTIFY_EMAIL.split(';') if '@' in part]
+    send_mail(subject, content, 'milpeer@mdrn.pl', addresses)