diff options
Diffstat (limited to 'cvp/opnfv_testapi')
-rw-r--r-- | cvp/opnfv_testapi/resources/application_handlers.py | 43 | ||||
-rw-r--r-- | cvp/opnfv_testapi/router/url_mappings.py | 4 |
2 files changed, 47 insertions, 0 deletions
diff --git a/cvp/opnfv_testapi/resources/application_handlers.py b/cvp/opnfv_testapi/resources/application_handlers.py index 144b2241..258c1aa2 100644 --- a/cvp/opnfv_testapi/resources/application_handlers.py +++ b/cvp/opnfv_testapi/resources/application_handlers.py @@ -30,6 +30,49 @@ class GenericApplicationHandler(handlers.GenericApiHandler): self.table_cls = application_models.Application +class ApplicationsLogoHandler(GenericApplicationHandler): + @web.asynchronous + @gen.coroutine + def post(self): + role = self.get_secure_cookie(auth_const.ROLE) + if role.find('administrator') == -1: + msg = 'Only administrator is allowed to upload logos' + self.finish_request({'code': '-1', 'msg': msg}) + return + + fileinfo = self.request.files['file'][0] + fname = fileinfo['filename'] + location = '3rd_party/static/testapi-ui/assets/img/' + fh = open(location + fname, 'w') + fh.write(fileinfo['body']) + msg = 'Successfully uploaded logo: ' + fname + resp = {'code': '1', 'msg': msg} + self.finish_request(resp) + + +class ApplicationsGetLogoHandler(GenericApplicationHandler): + def get(self, filename): + location = '3rd_party/static/testapi-ui/assets/img/' + filename + self.set_header('Content-Type', 'application/force-download') + self.set_header('Content-Disposition', + 'attachment; filename=%s' % filename) + try: + with open(location, "rb") as f: + try: + while True: + _buffer = f.read(4096) + if _buffer: + self.write(_buffer) + else: + f.close() + self.finish() + return + except Exception: + raise web.HTTPError(404) + except Exception: + raise web.HTTPError(500) + + class ApplicationsCLHandler(GenericApplicationHandler): @swagger.operation(nickname="queryApplications") @web.asynchronous diff --git a/cvp/opnfv_testapi/router/url_mappings.py b/cvp/opnfv_testapi/router/url_mappings.py index 83190ee4..e1d4c181 100644 --- a/cvp/opnfv_testapi/router/url_mappings.py +++ b/cvp/opnfv_testapi/router/url_mappings.py @@ -24,6 +24,10 @@ mappings = [ (r"/api/v1/tests", test_handlers.TestsCLHandler), (r"/api/v1/tests/([^/]+)", test_handlers.TestsGURHandler), + (r"/api/v1/cvp/applications/getlogo/([^/]+)", + application_handlers.ApplicationsGetLogoHandler), + (r"/api/v1/cvp/applications/uploadlogo", + application_handlers.ApplicationsLogoHandler), (r"/api/v1/cvp/applications", application_handlers.ApplicationsCLHandler), (r"/api/v1/cvp/applications/([^/]+)", application_handlers.ApplicationsGURHandler), |