2018-05-14 19:45:26 +00:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import re
|
2018-05-14 17:12:42 +00:00
|
|
|
|
2018-05-14 19:45:26 +00:00
|
|
|
from flask import make_response, render_template
|
|
|
|
from flask_restful import reqparse
|
|
|
|
|
2018-06-05 17:43:01 +00:00
|
|
|
from config_app._init_config import ROOT_DIR
|
|
|
|
|
2018-05-14 19:45:26 +00:00
|
|
|
|
|
|
|
def truthy_bool(param):
|
|
|
|
return param not in {False, 'false', 'False', '0', 'FALSE', '', 'null'}
|
|
|
|
|
|
|
|
|
|
|
|
DEFAULT_JS_BUNDLE_NAME = 'configapp'
|
|
|
|
PARAM_REGEX = re.compile(r'<([^:>]+:)*([\w]+)>')
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
TYPE_CONVERTER = {
|
|
|
|
truthy_bool: 'boolean',
|
|
|
|
str: 'string',
|
|
|
|
basestring: 'string',
|
|
|
|
reqparse.text_type: 'string',
|
|
|
|
int: 'integer',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def _list_files(path, extension, contains=""):
|
|
|
|
""" Returns a list of all the files with the given extension found under the given path. """
|
|
|
|
|
|
|
|
def matches(f):
|
|
|
|
return os.path.splitext(f)[1] == '.' + extension and contains in os.path.splitext(f)[0]
|
|
|
|
|
|
|
|
def join_path(dp, f):
|
|
|
|
# Remove the static/ prefix. It is added in the template.
|
2018-06-05 17:43:01 +00:00
|
|
|
return os.path.join(dp, f)[len(ROOT_DIR) + 1 + len('config_app/static/'):]
|
2018-05-14 19:45:26 +00:00
|
|
|
|
2018-06-05 17:43:01 +00:00
|
|
|
filepath = os.path.join(os.path.join(ROOT_DIR, 'config_app/static/'), path)
|
2018-05-14 19:45:26 +00:00
|
|
|
return [join_path(dp, f) for dp, _, files in os.walk(filepath) for f in files if matches(f)]
|
|
|
|
|
|
|
|
|
|
|
|
def render_page_template(name, route_data=None, js_bundle_name=DEFAULT_JS_BUNDLE_NAME, **kwargs):
|
2018-05-14 17:12:42 +00:00
|
|
|
""" Renders the page template with the given name as the response and returns its contents. """
|
2018-05-14 19:45:26 +00:00
|
|
|
main_scripts = _list_files('build', 'js', js_bundle_name)
|
2018-05-14 17:12:42 +00:00
|
|
|
|
|
|
|
contents = render_template(name,
|
|
|
|
route_data=route_data,
|
2018-05-14 19:45:26 +00:00
|
|
|
main_scripts=main_scripts,
|
2018-05-14 17:12:42 +00:00
|
|
|
**kwargs)
|
|
|
|
|
|
|
|
resp = make_response(contents)
|
|
|
|
resp.headers['X-FRAME-OPTIONS'] = 'DENY'
|
|
|
|
return resp
|
2018-05-14 19:45:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def fully_qualified_name(method_view_class):
|
|
|
|
return '%s.%s' % (method_view_class.__module__, method_view_class.__name__)
|
|
|
|
|
|
|
|
|