From ae87e84610828007acf2c455607eaa970debc46e Mon Sep 17 00:00:00 2001
From: Pete Resnick <presnick@qti.qualcomm.com>
Date: Sat, 16 Jul 2016 17:09:21 +0000
Subject: [PATCH] Generate message to Secretary and IESG when AD changes a
 draft state to "Approved".

Fixes issue #796. Commit ready for merge.
 - Legacy-Id: 11687
---
 ietf/doc/mails.py                             | 13 ++++++++++++-
 ietf/doc/views_draft.py                       |  9 +++++++--
 ietf/templates/doc/mail/ad_approval_email.txt | 10 ++++++++++
 3 files changed, 29 insertions(+), 3 deletions(-)
 create mode 100644 ietf/templates/doc/mail/ad_approval_email.txt

diff --git a/ietf/doc/mails.py b/ietf/doc/mails.py
index 75ef27638..2e51996ba 100644
--- a/ietf/doc/mails.py
+++ b/ietf/doc/mails.py
@@ -28,7 +28,18 @@ def email_state_changed(request, doc, text, mailtrigger_id=None):
               dict(text=text,
                    url=settings.IDTRACKER_BASE_URL + doc.get_absolute_url()),
               cc=cc)
-
+    
+def email_approved(request, doc, text):
+	to = "iesg@iesg.org"
+	bcc = "iesg-secretary@ietf.org"
+	frm = request.user.person.formatted_email()
+	send_mail(request, to, frm,
+			  "Approved: %s" % doc.filename_with_rev(),
+			  "doc/mail/ad_approval_email.txt",
+			  dict(text=text,
+				   docname=doc.filename_with_rev()),
+			  bcc=bcc)
+    
 def email_stream_changed(request, doc, old_stream, new_stream, text=""):
     """Email the change text to the notify group and to the stream chairs"""
     streams = []
diff --git a/ietf/doc/views_draft.py b/ietf/doc/views_draft.py
index 40677842a..1fd8a2a13 100644
--- a/ietf/doc/views_draft.py
+++ b/ietf/doc/views_draft.py
@@ -22,7 +22,7 @@ from ietf.doc.mails import ( email_pulled_from_rfc_queue, email_resurrect_reques
     email_resurrection_completed, email_state_changed, email_stream_changed,
     email_stream_state_changed, email_stream_tags_changed, extra_automation_headers,
     generate_publication_request, email_adopted, email_intended_status_changed,
-    email_iesg_processing_document )
+    email_iesg_processing_document, email_approved )
 from ietf.doc.utils import ( add_state_change_event, can_adopt_draft,
     get_tags_for_stream_id, nice_consensus,
     update_reminder, update_telechat, make_notify_changed_event, get_initial_notify,
@@ -69,6 +69,7 @@ def change_state(request, name):
     """Change IESG state of Internet Draft, notifying parties as necessary
     and logging the change as a comment."""
     doc = get_object_or_404(Document, docalias__name=name)
+
     if (not doc.latest_event(type="started_iesg_process")) or doc.get_state_slug() == "expired":
         raise Http404
 
@@ -77,6 +78,7 @@ def change_state(request, name):
     if request.method == 'POST':
         form = ChangeStateForm(request.POST)
         form.docname=name
+
         if form.is_valid():
             new_state = form.cleaned_data['state']
             prev_state = doc.get_state("draft-iesg")
@@ -84,6 +86,7 @@ def change_state(request, name):
             tag = form.cleaned_data['substate']
             comment = form.cleaned_data['comment'].strip()
 
+
             # tag handling is a bit awkward since the UI still works
             # as if IESG tags are a substate
             prev_tags = doc.tags.filter(slug__in=IESG_SUBSTATE_TAGS)
@@ -114,7 +117,9 @@ def change_state(request, name):
                 doc.save()
 
                 email_state_changed(request, doc, msg,'doc_state_edited')
-
+                
+                if new_state.slug == "approved" and new_tags == [] and has_role(request.user, "Area Director"):
+					email_approved(request, doc, comment)
 
                 if prev_state and prev_state.slug in ("ann", "rfcqueue") and new_state.slug not in ("rfcqueue", "pub"):
                     email_pulled_from_rfc_queue(request, doc, comment, prev_state, new_state)
diff --git a/ietf/templates/doc/mail/ad_approval_email.txt b/ietf/templates/doc/mail/ad_approval_email.txt
new file mode 100644
index 000000000..7c7564708
--- /dev/null
+++ b/ietf/templates/doc/mail/ad_approval_email.txt
@@ -0,0 +1,10 @@
+{% load mail_filters %}{% autoescape off %}
+{% filter wordwrap:73 %}Secretary (Bcc'ed):
+
+{{ docname }} has been approved.
+
+{{ text }}
+
+{% endfilter %}
+
+{% endautoescape%}