[web-cvp] Trigger an email when admin submit an application form 57/46357/2
authorgrakiss <grakiss.wanglei@huawei.com>
Sat, 28 Oct 2017 01:53:51 +0000 (21:53 -0400)
committerLeo wang <grakiss.wanglei@huawei.com>
Wed, 1 Nov 2017 08:27:37 +0000 (08:27 +0000)
JIRA: DOVETAIL-542

Trigger an email to cvp@opnfv.org when admin submit an application form.

Change-Id: Ie73d5a6380b5f5d1b0507564dc1f067047284f99
Signed-off-by: grakiss <grakiss.wanglei@huawei.com>
cvp/opnfv_testapi/common/utils.py [new file with mode: 0644]
cvp/opnfv_testapi/resources/application_handlers.py

diff --git a/cvp/opnfv_testapi/common/utils.py b/cvp/opnfv_testapi/common/utils.py
new file mode 100644 (file)
index 0000000..107c709
--- /dev/null
@@ -0,0 +1,43 @@
+import logging
+import smtplib
+from email.mime.text import MIMEText
+
+LOG = logging.getLogger(__name__)
+LOG.setLevel(logging.DEBUG)
+
+
+def send_email(subject, content):
+    MAIL_LIST = ['cvp@opnfv.org']
+    HOST = "smtp.gmail.com"
+    USER = "opnfv.cvp"
+    PASSWD = "opnfv@cvp"
+
+    sender = 'cvp<{}@gmail.com>'.format(USER)
+    msg = MIMEText(content, _subtype='plain')
+    msg['Subject'] = subject
+    msg['From'] = sender
+    msg['To'] = ";".join(MAIL_LIST)
+
+    _send_email(HOST, sender, USER, PASSWD, MAIL_LIST, msg)
+
+
+def _send_email(host,
+                sender,
+                user,
+                passwd,
+                receivers,
+                msg):
+
+    client = smtplib.SMTP()
+    try:
+        client.connect(host, 25)
+        LOG.debug('Success to connect server')
+        client.starttls()
+        client.login(user, passwd)
+        LOG.debug('Success to login')
+        LOG.debug('Start to sending email')
+        client.sendmail(sender, receivers, msg.as_string())
+        client.close()
+    except Exception:
+        LOG.exception('Error when sending email')
+        raise
index 6986b9e..144b224 100644 (file)
@@ -14,6 +14,7 @@ from tornado import gen
 from bson import objectid
 
 from opnfv_testapi.common.config import CONF
+from opnfv_testapi.common import utils
 from opnfv_testapi.resources import handlers
 from opnfv_testapi.resources import application_models
 from opnfv_testapi.tornado_swagger import swagger
@@ -107,9 +108,48 @@ class ApplicationsCLHandler(GenericApplicationHandler):
         if not ret:
             self.finish_request({'code': '403', 'msg': msg})
             return
-
         self._create(miss_fields=miss_fields, carriers=carriers)
 
+        self._send_email()
+
+    def _send_email(self):
+
+        data = self.table_cls.from_dict(self.json_args)
+        subject = "[OPNFV CVP]New OPNFV CVP Application Submission"
+        content = """Hi CVP Reviewer,
+
+This is a new application:
+
+    Organization Name: {},
+    Organization Website: {},
+    Product Name: {},
+    Product Specifications: {},
+    Product Documentation: {},
+    Product Categories: {},
+    Primary Name: {},
+    Primary Email: {},
+    Primary Address: {},
+    Primary Phone: {},
+    User ID Type: {},
+    User ID: {}
+
+Best Regards,
+CVP Team
+        """.format(data.organization_name,
+                   data.organization_web,
+                   data.product_name,
+                   data.product_spec,
+                   data.product_documentation,
+                   data.product_categories,
+                   data.prim_name,
+                   data.prim_email,
+                   data.prim_address,
+                   data.prim_phone,
+                   data.id_type,
+                   data.user_id)
+
+        utils.send_email(subject, content)
+
 
 class ApplicationsGURHandler(GenericApplicationHandler):
     @swagger.operation(nickname="deleteAppById")