|
|
05525d |
|
|
|
05525d |
|
|
|
b838e2 |
import html
|
|
|
b838e2 |
import urllib.parse
|
|
|
b838e2 |
|
|
|
b838e2 |
from translator import Translator
|
|
|
b838e2 |
|
|
|
b838e2 |
|
|
|
b838e2 |
class Result:
|
|
|
b838e2 |
def __init__(self, code, headers, data):
|
|
|
b838e2 |
assert(type(code) is str)
|
|
|
b838e2 |
assert(type(headers) is list)
|
|
|
b838e2 |
self.code = code
|
|
|
b838e2 |
self.headers = headers
|
|
|
b838e2 |
self.data = data
|
|
|
b838e2 |
|
|
|
b838e2 |
def complete(self, start_response):
|
|
|
b838e2 |
start_response(self.code, self.headers)
|
|
|
b838e2 |
return self.data
|
|
|
b838e2 |
|
|
|
b838e2 |
|
|
|
b838e2 |
class Answer(Translator):
|
|
|
b838e2 |
def __init__(self, request):
|
|
|
b838e2 |
super().__init__()
|
|
|
b838e2 |
|
|
|
05525d |
self.request = request
|
|
|
05525d |
self.urlprefix = self.request.server.config['urlprefix']
|
|
|
05525d |
self.urldataprefix = self.request.server.config['urldataprefix']
|
|
|
05525d |
|
|
|
05525d |
self.status = "200 OK"
|
|
|
b838e2 |
self.headers = list()
|
|
|
05525d |
self.datalist = None
|
|
|
05525d |
|
|
|
05525d |
self.data = None
|
|
|
05525d |
self.text = ''
|
|
|
05525d |
self.html = ''
|
|
|
05525d |
|
|
|
572081 |
self.uipath = list()
|
|
|
05525d |
self.content = ''
|
|
|
b838e2 |
self.errors = list()
|
|
|
b838e2 |
self.fields = dict()
|
|
|
05525d |
self.template = None
|
|
|
b838e2 |
|
|
|
b838e2 |
self.objects = dict()
|
|
|
05525d |
|
|
|
b838e2 |
def htmlescape(self, text):
|
|
|
e5b0ac |
return html.escape(str('' if text is None else text))
|
|
|
b838e2 |
def urlescape(self, text):
|
|
|
e5b0ac |
return urllib.parse.quote(str('' if text is None else text))
|
|
|
05525d |
|
|
|
b838e2 |
def e(self, text):
|
|
|
b838e2 |
return self.htmlescape(text)
|
|
|
b838e2 |
def ue(self, text):
|
|
|
b838e2 |
return self.urlescape(text)
|
|
|
b838e2 |
def te(self, text):
|
|
|
b838e2 |
return self.e( self.t(text) )
|
|
|
b838e2 |
|
|
|
b838e2 |
def encodedfield(self, name):
|
|
|
b838e2 |
return self.e( self.fields.get(str(name), '') )
|
|
|
b838e2 |
|
|
|
572081 |
def add_uipath_entry(self, title, path):
|
|
|
572081 |
assert(type(title) is str)
|
|
|
572081 |
assert(type(path) is list)
|
|
|
572081 |
self.uipath.append((title, path))
|
|
|
05525d |
|
|
|
b838e2 |
def complete_redirect(self, path = None):
|
|
|
b838e2 |
self.status = '303 See Other'
|
|
|
e5b0ac |
self.headers.append( ('Location', self.request.get_urlpath_escaped(path)) )
|
|
|
b838e2 |
return Result(self.status, self.headers, list())
|
|
|
b838e2 |
|
|
|
b838e2 |
def complete_error(self, status = None):
|
|
|
b838e2 |
if not status is None:
|
|
|
b838e2 |
self.status = status
|
|
|
b838e2 |
return Result(self.status, list(), list())
|
|
|
b838e2 |
|
|
|
05525d |
def complete_data(self, data = None):
|
|
|
05525d |
if not data is None:
|
|
|
05525d |
self.data = data
|
|
|
05525d |
if self.data is None:
|
|
|
05525d |
self.data = bytes()
|
|
|
05525d |
if not type(self.data) is bytes:
|
|
|
05525d |
self.data = bytes(str(self.data), "utf8")
|
|
|
05525d |
if not self.data and self.status == "200 OK":
|
|
|
05525d |
self.status = "204 No Content"
|
|
|
05525d |
|
|
|
05525d |
size = 65536
|
|
|
05525d |
self.datalist = []
|
|
|
05525d |
for i in range(0, len(self.data), size):
|
|
|
05525d |
self.datalist.append(self.data[i:i + size])
|
|
|
b838e2 |
return Result(self.status, self.headers, self.datalist)
|
|
|
05525d |
|
|
|
05525d |
def complete_text(self, text = None):
|
|
|
05525d |
if not text is None:
|
|
|
05525d |
self.text = text
|
|
|
05525d |
if self.text is None:
|
|
|
05525d |
self.text = ""
|
|
|
05525d |
return self.complete_data(text)
|
|
|
05525d |
|
|
|
05525d |
def complete_html(self, html = None):
|
|
|
05525d |
if not html is None:
|
|
|
05525d |
self.html = html
|
|
|
e5b0ac |
self.headers.append( ('Content-Type','text/html') );
|
|
|
05525d |
return self.complete_text( self.html )
|
|
|
05525d |
|
|
|
05525d |
def complete_content(self, content = None, template = None):
|
|
|
05525d |
if not content is None:
|
|
|
05525d |
self.content = content
|
|
|
05525d |
if not template is None:
|
|
|
05525d |
self.template = template
|
|
|
05525d |
return self.complete_html( self.template.wrap(self) if self.template else self.content )
|
|
|
05525d |
|