[yocto] [PATCH 1/1] Send email notification on publication

Amanda Brindle amanda.r.brindle at intel.com
Thu Oct 19 14:27:27 PDT 2017


When publishing a layer, send an email notification to all of that
layer's maintainers.

Fixes [YOCTO #11208]

Signed-off-by: Amanda Brindle <amanda.r.brindle at intel.com>
---
 layerindex/views.py                          | 45 +++++++++++++++++++++++++++-
 templates/layerindex/publishemail.txt        |  9 ++++++
 templates/layerindex/publishemailsubject.txt |  1 +
 3 files changed, 54 insertions(+), 1 deletion(-)
 create mode 100644 templates/layerindex/publishemail.txt
 create mode 100644 templates/layerindex/publishemailsubject.txt

diff --git a/layerindex/views.py b/layerindex/views.py
index bcf6671..a4cd05f 100644
--- a/layerindex/views.py
+++ b/layerindex/views.py
@@ -5,7 +5,7 @@
 # Licensed under the MIT license, see COPYING.MIT for details
 
 import sys
-from django.shortcuts import get_object_or_404, render
+from django.shortcuts import get_object_or_404, get_list_or_404, render
 from django.http import HttpResponse, HttpResponseRedirect, HttpResponseForbidden, Http404
 from django.core.urlresolvers import reverse, reverse_lazy, resolve
 from django.core.exceptions import PermissionDenied
@@ -261,6 +261,49 @@ def _check_url_branch(kwargs):
 def publish(request, name):
     if not (request.user.is_authenticated() and request.user.has_perm('layerindex.publish_layer')):
         raise PermissionDenied
+    layeritem = get_object_or_404(LayerItem, name=name)
+    layerbranch = get_object_or_404(LayerBranch, layer=layeritem)
+    layer_url = request.build_absolute_uri(reverse('layer_item', args=(layerbranch.branch, layeritem.name)))
+    maintainers = get_list_or_404(LayerMaintainer, layerbranch=layerbranch)
+    from_email = settings.SUBMIT_EMAIL_FROM
+    subjecttext = get_template('layerindex/publishemailsubject.txt')
+    bodytext = get_template('layerindex/publishemail.txt')
+    maintainer_names = ""
+    plural = ""
+
+    # compile list of maintainer names
+    if len(maintainers) > 1:
+        maintainer_names = ", ".join(m.name for m in maintainers[:-1])
+        if len(maintainers) > 2:
+            maintainer_names += ","
+        maintainer_names += " and " + maintainers[-1].name
+        plural = "s"
+    else:
+        maintainer_names = maintainers[0].name
+
+    # find appropriate help contact
+    for user in User.objects.all():
+        if user.username != 'root' and user.is_active:
+            help_contact = user
+            break
+
+    # create subject from subject template
+    d = Context({
+        'layer_name': layeritem.name,
+        'site_name': request.META['HTTP_HOST'],
+    })
+    subject = subjecttext.render(d).rstrip()
+
+    d = Context({
+        'maintainer_names': maintainer_names,
+        'plural': plural,
+        'layer_name': layeritem.name,
+        'layer_url': layer_url,
+        'help_contact': help_contact,
+    })
+    body = bodytext.render(d)
+    tasks.send_email.apply_async((subject, body, from_email, [m.email for m in maintainers]))
+
     return _statuschange(request, name, 'P')
 
 def _statuschange(request, name, newstatus):
diff --git a/templates/layerindex/publishemail.txt b/templates/layerindex/publishemail.txt
new file mode 100644
index 0000000..6789905
--- /dev/null
+++ b/templates/layerindex/publishemail.txt
@@ -0,0 +1,9 @@
+Hi {{ maintainer_names }},
+
+You are listed as the maintainer{{plural}} for the new layer, {{ layer_name }}. This layer has now been published. You can view it at the following URL:
+
+ {{ layer_url }}
+
+If you need to make changes to this layer's entry in the index in the future, create an account (using this email address) if you haven't already, and you should then be able to click on "Edit layer" on the layer's detail page and make the desired changes. If you need any assistance please contact {{ help_contact.email }}.
+
+Thanks!
diff --git a/templates/layerindex/publishemailsubject.txt b/templates/layerindex/publishemailsubject.txt
new file mode 100644
index 0000000..1d49ea7
--- /dev/null
+++ b/templates/layerindex/publishemailsubject.txt
@@ -0,0 +1 @@
+{{site_name}}: {{layer_name }} published
-- 
2.7.4




More information about the yocto mailing list