mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 22:11:14 +02:00
Display an upload form (upload is not yet ready)
This commit is contained in:
1 parent
210aa6f12a
commit
f7cd09d5fb
30 files changed
+234
-100
No files matched your search
@@ -20,9 +20,12 @@ import jsonschema
|
||||
import aiohttp.web
|
||||
import logging
|
||||
import sys
|
||||
import jinja2
|
||||
|
||||
from ..version import __version__
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
renderer = jinja2.Environment(loader=jinja2.PackageLoader('gns3server', 'templates'))
|
||||
|
||||
|
||||
class Response(aiohttp.web.Response):
|
||||
@@ -47,6 +50,28 @@ class Response(aiohttp.web.Response):
|
||||
log.debug(json.loads(self.body.decode('utf-8')))
|
||||
return super().start(request)
|
||||
|
||||
def html(self, answer):
|
||||
"""
|
||||
Set the response content type to text/html and serialize
|
||||
the content.
|
||||
|
||||
:param anwser The response as a Python object
|
||||
"""
|
||||
|
||||
self.content_type = "text/html"
|
||||
self.body = answer.encode('utf-8')
|
||||
|
||||
def template(self, template_filename, **kwargs):
|
||||
"""
|
||||
Render a template
|
||||
|
||||
:params template: Template name
|
||||
:params kwargs: Template parameters
|
||||
"""
|
||||
template = renderer.get_template(template_filename)
|
||||
kwargs["gns3_version"] = __version__
|
||||
self.html(template.render(**kwargs))
|
||||
|
||||
def json(self, answer):
|
||||
"""
|
||||
Set the response content type to application/json and serialize
|
||||
|
||||
+12
-4
@@ -81,8 +81,11 @@ class Route(object):
|
||||
# This block is executed only the first time
|
||||
output_schema = kw.get("output", {})
|
||||
input_schema = kw.get("input", {})
|
||||
api_version = kw.get("version", 1)
|
||||
cls._path = "/v{version}{path}".format(path=path, version=api_version)
|
||||
api_version = kw.get("api_version", 1)
|
||||
if api_version is None:
|
||||
cls._path = path
|
||||
else:
|
||||
cls._path = "/v{version}{path}".format(path=path, version=api_version)
|
||||
|
||||
def register(func):
|
||||
route = cls._path
|
||||
@@ -123,8 +126,13 @@ class Route(object):
|
||||
response.set_status(500)
|
||||
exc_type, exc_value, exc_tb = sys.exc_info()
|
||||
lines = traceback.format_exception(exc_type, exc_value, exc_tb)
|
||||
tb = "".join(lines)
|
||||
response.json({"message": tb, "status": 500})
|
||||
if api_version is not None:
|
||||
tb = "".join(lines)
|
||||
response.json({"message": tb, "status": 500})
|
||||
else:
|
||||
tb = "\n".join(lines)
|
||||
response.html("<h1>Internal error</h1><pre>{}</pre>".format(tb))
|
||||
|
||||
return response
|
||||
|
||||
cls._routes.append((method, cls._path, control_schema))
|
||||
|
||||
Reference in new issue
Block a user