diff --git a/templates/footer.html b/templates/footer.html new file mode 100644 index 0000000..2018aaf --- /dev/null +++ b/templates/footer.html @@ -0,0 +1,5 @@ +
+ +
\ No newline at end of file diff --git a/templates/json2html_template.html b/templates/json2html_template.html new file mode 100644 index 0000000..0c50be3 --- /dev/null +++ b/templates/json2html_template.html @@ -0,0 +1,40 @@ + + \ No newline at end of file diff --git a/templates/table_template.html b/templates/table_template.html new file mode 100644 index 0000000..5b9224b --- /dev/null +++ b/templates/table_template.html @@ -0,0 +1,13 @@ + + + + + {{ @json2html_template.html }} + + + +
+
+ {{ @footer.html }} + + \ No newline at end of file diff --git a/templates_engine.py b/templates_engine.py new file mode 100644 index 0000000..7324d4e --- /dev/null +++ b/templates_engine.py @@ -0,0 +1,33 @@ +""" +Features: +include files +insert variables from context dict +""" +from os.path import join +import re +templates_dir = 'templates' + +variable_pattern: re.Pattern = re.compile(r'\{\{ .* }}') +include_pattern: re.Pattern = re.compile(r'\{\{ @.* }}') + + +def render(template_name: str, context: dict): + template_path = join(templates_dir, template_name) + template = get_file_content(template_path) + + for include_statement in re.findall(include_pattern, template): + file_include = include_statement.split(' ')[1][1:] + include_content = get_file_content(join(templates_dir, file_include)) + template = template.replace(include_statement, include_content) + + for var_to_replace in re.findall(variable_pattern, template): + key = var_to_replace.split(' ')[1] + template = template.replace(var_to_replace, context[key]) + + return template + + +def get_file_content(filename: str) -> str: + with open(filename, 'r', encoding='utf-8') as file: + # it will throw exception if file doesn't exist + return file.read() diff --git a/web/__init__.py b/web/__init__.py index 79b1a41..61c0d59 100644 --- a/web/__init__.py +++ b/web/__init__.py @@ -3,7 +3,7 @@ import falcon import json from model import model -from web import dynamic_js +from templates_engine import render model.open_model() @@ -17,9 +17,13 @@ class SquadsInfoByTagShort: class SquadsInfoByTagShortHtml: def on_get(self, req: falcon.request.Request, resp: falcon.response.Response, tag: str) -> None: resp.content_type = falcon.MEDIA_HTML - model_answer = model.list_squads_by_tag(tag) - - resp.text = dynamic_js.activity_table_html_template.replace('{items}', json.dumps(model_answer)) + resp.text = render( + 'table_template.html', + { + 'target_column_name': 'None', + 'target_new_url': '' + } + ) class SquadsInfoByTagExtended: @@ -36,12 +40,13 @@ class SquadsInfoByTagExtended: class SquadsInfoByTagExtendedHtml: def on_get(self, req: falcon.request.Request, resp: falcon.response.Response, tag: str) -> None: resp.content_type = falcon.MEDIA_HTML - model_answer = model.list_squads_by_tag_with_tags(tag) - - for squad in model_answer: - squad['user_tags'] = utils.humanify_resolved_user_tags(utils.resolve_user_tags(squad['user_tags'])) - - resp.text = dynamic_js.activity_table_html_template.replace('{items}', json.dumps(model_answer)) + resp.text = render( + 'table_template.html', + { + 'target_column_name': 'None', + 'target_new_url': '' + } + ) application = falcon.App() diff --git a/web/dynamic_js.py b/web/dynamic_js.py deleted file mode 100644 index 179a2cf..0000000 --- a/web/dynamic_js.py +++ /dev/null @@ -1,33 +0,0 @@ -activity_table_html_template = """ - - - - - - - - - -""" \ No newline at end of file