blob: 23ee97bfea4dbcceb35490ef784fea3cf35167b1 [file] [log] [blame]
tiernoc94c3df2018-02-09 15:38:54 +01001"""
2Contains html text in variables to make and html response
3"""
4
5import yaml
6from http import HTTPStatus
7
8__author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
9
10html_start = """
11 <!DOCTYPE html>
12<html>
13<head>
14 <link href="/osm/static/style.css" rel="stylesheet">
15<title>Welcome to OSM</title>
16</head>
17<body>
18 <div id="osm_topmenu">
19 <div>
20 <a href="https://osm.etsi.org"> <img src="/osm/static/OSM-logo.png" height="42" width="100" style="vertical-align:middle"> </a>
21 <a>( {} )</a>
tiernof27c79b2018-03-12 17:08:42 +010022 <a href="/osm/vnfpkgm/v1/vnf_packages_content">VNFDs </a>
23 <a href="/osm/nsd/v1/ns_descriptors_content">NSDs </a>
24 <a href="/osm/nslcm/v1/ns_instances_content">NSs </a>
25 <a href="/osm/admin/v1/users">USERs </a>
26 <a href="/osm/admin/v1/projects">PROJECTs </a>
27 <a href="/osm/admin/v1/tokens">TOKENs </a>
28 <a href="/osm/admin/v1/tokens?METHOD=DELETE">logout </a>
tiernoc94c3df2018-02-09 15:38:54 +010029 </div>
30 </div>
31"""
32
33html_body = """
34<h1>{item}</h1>
35"""
36
37html_end = """
38</body>
39</html>
40"""
41
42html_body_error = "<h2> Error <pre>{}</pre> </h2>"
43
44
45
46html_auth2 = """
47<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
48<html>
49<head><META http-equiv="Content-Type" content="text/html; charset=UTF-8">
50 <link href="/osm/static/style.css" rel="stylesheet">
51 <title>OSM Login</title>
52</head>
53<body>
54 <div id="osm_header">
55 <div>
56 <a href="https://osm.etsi.org"> <h1><img src="/osm/static/OSM-logo.png" style="vertical-align:middle"></h1> </a>
57 </div>
58 </div>
59 <div id="osm_error_message">
60 <h1>{error}</h1>
61 </div>
62 <div class="gerritBody" id="osm_body">
63 <h1>Sign in to OSM</h1>
tiernof27c79b2018-03-12 17:08:42 +010064 <form action="/osm/admin/v1/tokens" id="login_form" method="POST">
tiernoc94c3df2018-02-09 15:38:54 +010065 <table style="border: 0;">
66 <tr><th>Username</th><td><input id="f_user" name="username" size="25" tabindex="1" type="text"></td></tr>
67 <tr><th>Password</th><td><input id="f_pass" name="password" size="25" tabindex="2" type="password"></td></tr>
68 <tr><td><input tabindex="3" type="submit" value="Sign In"></td></tr>
69 </table>
70 </form>
71 <div style="clear: both; margin-top: 15px; padding-top: 2px; margin-bottom: 15px;">
72 <div id="osm_footer">
73 <div></div>
74 </div>
75 </div>
76 </div>
77 <script src="/osm/static/login.js"> </script>
78</body>
79</html>
80"""
81
82html_upload_body = """
83<form action="/osm{}" method="post" enctype="multipart/form-data">
84 <h3> <table style="border: 0;"> <tr>
85 <td> Upload {} descriptor (tar.gz) file: <input type="file" name="descriptor_file"/> </td>
86 <td> <input type="submit" value="Upload"/> </td>
87 </tr> </table> </h3>
88</form>
89"""
90
91
92def format(data, request, response, session):
93 """
94 Format a nice html response, depending on the data
95 :param data:
96 :param request: cherrypy request
97 :param response: cherrypy response
98 :return: string with teh html response
99 """
100 response.headers["Content-Type"] = 'text/html'
101 if response.status == HTTPStatus.UNAUTHORIZED.value:
102 if response.headers.get("WWW-Authenticate") and request.config.get("auth.allow_basic_authentication"):
103 response.headers["WWW-Authenticate"] = "Basic" + response.headers["WWW-Authenticate"][6:]
104 return
105 else:
106 return html_auth2.format(error=data)
107 body = html_body.format(item=request.path_info)
108 if response.status and response.status > 202:
109 body += html_body_error.format(yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False))
110 elif isinstance(data, (list, tuple)):
tiernof27c79b2018-03-12 17:08:42 +0100111 if request.path_info == "/vnfpkgm/v1/vnf_packages_content":
tierno0f98af52018-03-19 10:28:22 +0100112 body += html_upload_body.format(request.path_info, "VNFD")
tiernof27c79b2018-03-12 17:08:42 +0100113 elif request.path_info == "/nsd/v1/ns_descriptors_content":
tierno0f98af52018-03-19 10:28:22 +0100114 body += html_upload_body.format(request.path_info, "NSD")
tiernoc94c3df2018-02-09 15:38:54 +0100115 for k in data:
tiernof27c79b2018-03-12 17:08:42 +0100116 if isinstance(k, dict):
117 data_id = k.pop("_id", None)
118 elif isinstance(k, str):
119 data_id = k
tiernoc94c3df2018-02-09 15:38:54 +0100120 body += '<p> <a href="/osm/{url}/{id}">{id}</a>: {t} </p>'.format(url=request.path_info, id=data_id, t=k)
121 elif isinstance(data, dict):
122 if "Location" in response.headers:
123 body += '<a href="{}"> show </a>'.format(response.headers["Location"])
124 else:
125 body += '<a href="/osm/{}?METHOD=DELETE"> <img src="/osm/static/delete.png" height="25" width="25"> </a>'.format(request.path_info)
126 body += "<pre>" + yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False) + "</pre>"
tierno0f98af52018-03-19 10:28:22 +0100127 elif data is None:
128 if request.method == "DELETE" or "METHOD=DELETE" in request.query_string:
129 body += "<pre> deleted </pre>"
tiernoc94c3df2018-02-09 15:38:54 +0100130 else:
131 body = str(data)
132 user_text = " "
133 if session:
134 if session.get("username"):
135 user_text += "user: {}".format(session.get("username"))
136 if session.get("project_id"):
137 user_text += ", project: {}".format(session.get("project_id"))
138 return html_start.format(user_text) + body + html_end
139 #yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False)
140 # tags=False,
141 # encoding='utf-8', allow_unicode=True)
142
143