Create an HTML response content.
Can be used when all pages of a webapp are sharing the same header, nav and footer. Then, only one tree is created for all pages, and its body->main is changed depending on the requested page.
Create an HTML response content.
Can be used when all pages of a webapp are sharing the same header, nav and footer. Then, only one tree is created for all pages, and its body->main is changed depending on the requested page.
Create a HTTPD Response instance with a default response.
Useful when creating dynamically the HTML Tree for a webapp. The "main" part of the body is re-created every time bake() is invoked. Here, it's loaded from a static file.
def __init__(self, name='index.html', tree=None, **kwargs):
"""Create a HTTPD Response instance with a default response.
Useful when creating dynamically the HTML Tree for a webapp.
The "main" part of the body is re-created every time bake() is invoked.
Here, it's loaded from a static file.
:param name: (str) Filename of the body main content.
"""
self._name = name
super(WebSiteResponse, self).__init__(name, tree)
self._status = HTTPDStatus()
self._bake()
Override. Return the current HTML page name.
def page(self) -> str:
"""Override. Return the current HTML page name.
:return: (str) Name of the file containing the body->main.
"""
return self._name
Process the given events.
The given event name must match a function of the event's manager.
Processing an event may change the content of the tree. In that case,
the dirty
method must be turned into True: it will invalidate the
deprecated content (invalidate) and re-generate a new one (bake).
def _process_events(self, events, **kwargs) -> bool:
"""Process the given events.
The given event name must match a function of the event's manager.
Processing an event may change the content of the tree. In that case,
the `dirty` method must be turned into True: it will invalidate the
deprecated content (_invalidate) and re-generate a new one (_bake).
:param events (dict): key=event_name, value=event_value
:return: (bool)
"""
self._status.code = 200
return True
Remove all children nodes of the body "main".
Delete the body main content and nothing else.
def _invalidate(self) -> None:
"""Remove all children nodes of the body "main".
Delete the body main content and nothing else.
"""
node = self._htree.body_main
node.clear_children()
Create the dynamic page content in HTML.
Load the body->main content from a file and add it to the tree.
def _bake(self) -> None:
"""Create the dynamic page content in HTML.
Load the body->main content from a file and add it to the tree.
"""
logging.debug(' -> Set {:s} content to the body->main'.format(self._name))
with codecs.open(self._name, 'r', 'utf-8') as fp:
lines = fp.readlines()
if self._htree.get_body_main() is not None:
self._htree.body_main.set_value(' '.join(lines))