first (broken) attempts with web views for bureaus

workspace
Brendan Howell 4 years ago
parent 27d44a08b9
commit 56aec9118e

@ -62,6 +62,20 @@ def add_api(apistr, name=""):
return decorator return decorator
def add_webview(webstr, name, in_menu=True):
""" decorator for making a method available on the web-admin"""
def decorator(func):
""" the decorator iteself """
@functools.wraps(func)
def func_wrap(*args, **kwargs):
""" this is to avoid roaching the namespace """
return func(*args, **kwargs)
func_wrap.webview = webstr
func_wrap.name = name
return func_wrap
return decorator
class LogPrinter(logging.Handler): class LogPrinter(logging.Handler):
""" """
LogPrinter prints logs on a receipt printer for screenless debugging. LogPrinter prints logs on a receipt printer for screenless debugging.
@ -329,7 +343,8 @@ class Bureau(object):
method = getattr(self, member) method = getattr(self, member)
# ignore anything that is not a method with command or api details # ignore anything that is not a method with command or api details
if not (callable(method) and (hasattr(method, "command") or if not (callable(method) and (hasattr(method, "command") or
hasattr(method, "api"))): hasattr(method, "api") or
hasattr(method, "webview"))):
continue continue
if hasattr(method, "command"): if hasattr(method, "command"):
self.commands[method.command] = method self.commands[method.command] = method
@ -353,6 +368,17 @@ class Bureau(object):
method(api_detail) method(api_detail)
else: else:
self.send("IR", "addapi", api_detail) self.send("IR", "addapi", api_detail)
elif hasattr(method, "webview"):
self.webview[method.webview] = method
webview_detail = { "name": method.name,
"prefix": self.prefix,
"webview": method.webview,
"desc": method.__doc__ }
if self.prefix == "IR":
method = getattr(self, "add_webview")
method(webview_detail)
else:
self.send("IR", "addweb", webview_detail)
self.log.debug("registered:") self.log.debug("registered:")
self.log.debug(str(self.commands)) self.log.debug(str(self.commands))

@ -99,6 +99,28 @@ class InhumanResources(Bureau):
self.menu[prefix]["apis"][cmd] = {"name": cmdname, self.menu[prefix]["apis"][cmd] = {"name": cmdname,
"desc": desc} "desc": desc}
@add_api("addweb", "Register Custom Web View")
def add_webview(self, data):
try:
prefix = data["prefix"]
webview = data["webview"]
viewname = data["name"]
desc = data["desc"]
except KeyError as e:
self.log.error("cannot add invalid command:", e)
return
if prefix not in self.menu:
self.log.error("cannot add web view" + webview +
"to non-existent prefix" + prefix)
else:
self.menu[prefix]["webviews"][webview] = {"name": viewname,
"desc": desc}
def callback(request):
self.send(prefix, webview, data=request)
self.webapp.register_webview(prefix, webview, callback)
@add_command("menu", "Print Menu") @add_command("menu", "Print Menu")
def print_menu(self): def print_menu(self):
""" """

@ -90,6 +90,11 @@ class IhrApp(Bottle):
if not(bureau in self.bureaus): if not(bureau in self.bureaus):
self.bureaus.append(bureau) self.bureaus.append(bureau)
def register_webview(self, prefix, view, callback):
def wrapped_cb():
callback(request.forms)
self.route("/" + prefix + "/" + view, callback=wrapped_cb)
def register_crud(self, data): def register_crud(self, data):
# adds callbacks for crud operations on a database # adds callbacks for crud operations on a database
pass pass

Loading…
Cancel
Save