aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMilo Casagrande <milo.casagrande@linaro.org>2015-01-14 10:41:00 +0100
committerMilo Casagrande <milo.casagrande@linaro.org>2015-01-14 10:41:00 +0100
commit5381a63aa3c75e09c710ecc8d44973e7f61cdf27 (patch)
tree6c207393358082492ad8faab29a138496ad3b8c3
parent6b2f9881d5a17d99ba9a20b08ff7878a91046b74 (diff)
Create new handler for /send URL.
Change-Id: I6da5e1516a02ea4357d4365e87643266f6f2bd21
-rw-r--r--app/handlers/send.py90
-rw-r--r--app/urls.py5
2 files changed, 93 insertions, 2 deletions
diff --git a/app/handlers/send.py b/app/handlers/send.py
new file mode 100644
index 0000000..51de4b7
--- /dev/null
+++ b/app/handlers/send.py
@@ -0,0 +1,90 @@
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+"""This module is used to send boot and build report email."""
+
+import bson
+import datetime
+
+import handlers.base as hbase
+import handlers.common as hcommon
+import handlers.response as hresponse
+import models
+import taskqueue.tasks as taskq
+
+
+# pylint: disable=too-many-public-methods
+class SendHandler(hbase.BaseHandler):
+ """Handle the /send URLs."""
+
+ def __init__(self, application, request, **kwargs):
+ super(SendHandler, self).__init__(application, request, **kwargs)
+
+ @staticmethod
+ def _valid_keys(method):
+ return hcommon.SEND_VALID_KEYS.get(method, None)
+
+ def _post(self, *args, **kwargs):
+ response = hresponse.HandlerResponse(202)
+
+ json_obj = kwargs["json_obj"]
+ db_options = kwargs["db_options"]
+ mail_options = self.settings["mailoptions"]
+
+ countdown = json_obj.get(models.DELAY_KEY, self.settings["senddelay"])
+ if countdown is None:
+ countdown = self.settings["senddelay"]
+
+ when = (
+ datetime.datetime.now(tz=bson.tz_util.utc) +
+ datetime.timedelta(seconds=countdown)
+ )
+ response.reason = (
+ "Email report scheduled to be sent at '%s' UTC" %
+ when.isoformat()
+ )
+
+ taskq.schedule_boot_report.apply_async(
+ [json_obj, db_options, mail_options, countdown])
+
+ return response
+
+ def execute_delete(self, *args, **kwargs):
+ """Perform DELETE pre-operations.
+
+ Check that the DELETE request is OK.
+ """
+ response = None
+
+ if self.validate_req_token("DELETE"):
+ response = hresponse.HandlerResponse(501)
+ else:
+ response = hresponse.HandlerResponse(403)
+ response.reason = hcommon.NOT_VALID_TOKEN
+
+ return response
+
+ def execute_get(self, *args, **kwargs):
+ """Execute the GET pre-operations.
+
+ Checks that everything is OK to perform a GET.
+ """
+ response = None
+
+ if self.validate_req_token("GET"):
+ response = hresponse.HandlerResponse(501)
+ else:
+ response = hresponse.HandlerResponse(403)
+ response.reason = hcommon.NOT_VALID_TOKEN
+
+ return response
diff --git a/app/urls.py b/app/urls.py
index f3098b9..b3fd9b5 100644
--- a/app/urls.py
+++ b/app/urls.py
@@ -1,5 +1,3 @@
-# Copyright (C) 2014 Linaro Ltd.
-#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
@@ -25,6 +23,7 @@ import handlers.defconf
import handlers.job
import handlers.lab
import handlers.report
+import handlers.send
import handlers.subscription
import handlers.token
import handlers.version
@@ -71,6 +70,7 @@ _REPORT_URL = url(
handlers.report.ReportHandler,
name="response"
)
+_SEND_URL = url(r"/send(?P<sl>/)?", handlers.send.SendHandler, name="send")
APP_URLS = [
_BATCH_URL,
@@ -84,4 +84,5 @@ APP_URLS = [
_TOKEN_URL,
_VERSION_URL,
_REPORT_URL,
+ _SEND_URL
]