Răsfoiți Sursa

set template path

Daniel Sheffield 1 an în urmă
părinte
comite
01ba7d9996

+ 17 - 38
app/rest/pyapi.py

@@ -20,7 +20,9 @@ from bottle import (
     template,
     HTTPError,
     static_file,
+    TEMPLATE_PATH,
 )
+TEMPLATE_PATH.append("app/rest/templates")
 from psycopg import connect
 from psycopg.sql import SQL, Literal
 import matplotlib.pyplot as plt
@@ -158,10 +160,7 @@ def get_form(action, method, filter_data, data):
         'group': 2,
     }[x])
     in_chart = data['$/unit'].apply(lambda x: (x or False) and True)
-    return template(
-        'app/rest/templates/form',
-        action=action,
-        method=method,
+    return template('form', action=action, method=method,
         **{
             k: {
                 "name": k,
@@ -224,7 +223,7 @@ def trend():
     if request.query_string != normalized:
         return redirect(f'{path}?{normalized}')
     
-    loading = template("app/rest/templates/loading", progress=[])
+    loading = template("loading", progress=[])
     if request.query_string in CACHE:
         if LOCK.acquire(block=False):
             try:
@@ -263,37 +262,37 @@ def trend_internal(path, query):
                 raise abort(400, f"Unsupported unit {unit}")
 
             progress.append({ "name": "Loading data", "status": ""})
-            yield template("app/rest/templates/loading", progress=progress)
+            yield template("loading", progress=progress)
             data = get_data(query_manager, **fields)
 
             progress[-1]["status"] = "done"
-            yield template("app/rest/templates/loading", progress=progress)
+            yield template("loading", progress=progress)
 
             if data.empty:
                 raise abort(404, f"No data for {fields}")
             
             progress.append({ "name": "Loading chart", "status": ""})
-            yield template("app/rest/templates/loading", progress=progress)
+            yield template("loading", progress=progress)
             pivot = data.pivot_table(index=['ts_raw',], columns=['product',], values=['$/unit'], aggfunc='mean')
             pivot.columns = pivot.columns.droplevel()
             plt.figure(facecolor='black', figsize=[16, 9])
             line(pivot, xlabel='Time', ylabel=f'$ / {unit}')
             
             progress[-1]["status"] = "done"
-            yield template("app/rest/templates/loading", progress=progress)
+            yield template("loading", progress=progress)
             
             progress.append({ "name": "Rendering chart", "status": ""})
-            yield template("app/rest/templates/loading", progress=progress)
+            yield template("loading", progress=progress)
 
             f = StringIO()
             plt.savefig(f, format='svg')
             form = get_form(path, 'get', get_filter(request.query, allow=PARAMS), data)
             
             progress[-1]["status"] = "done"
-            yield template("app/rest/templates/loading", progress=progress)
+            yield template("loading", progress=progress)
             
-            final = template("app/rest/templates/trend", form=form, svg=f.getvalue())
-            resp = lambda: template("app/rest/templates/trend", form=form, svg=f.getvalue())
+            final = template("trend", form=form, svg=f.getvalue())
+            resp = lambda: template("trend", form=form, svg=f.getvalue())
 
     except HTTPError as e:
         resp = lambda exception=e: exception
@@ -308,7 +307,7 @@ def table():
     title = request.query['title'] if 'title' in request.query.keys() else None
     foot = request.query['foot'] == 'True' and "last()" if 'foot' in request.query.keys() else "-1"
     response.content_type = 'application/xhtml+xml; charset=utf-8'
-    return template("app/rest/templates/query-to-xml-xslt", title=title, foot=foot)
+    return template("query-to-xml-xslt", title=title, foot=foot)
 
 @route('/grocery/groups')
 def groups():
@@ -331,12 +330,7 @@ FROM (
     finally:
         conn.commit()
     response.content_type = 'application/xhtml+xml; charset=utf-8'
-    return template(
-        "app/rest/query-to-xml",
-        title="Groups",
-        foot=True,
-        xml=xml
-    )
+    return template("query-to-xml", title="Groups", foot=True, xml=xml)
 
 @route('/grocery/categories')
 def categories():
@@ -359,12 +353,7 @@ FROM (
     finally:
         conn.commit()
     response.content_type = 'application/xhtml+xml; charset=utf-8'
-    return template(
-        "app/rest/templates/query-to-xml",
-        title="Categories",
-        foot=True,
-        xml=xml
-    )
+    return template("query-to-xml", title="Categories", foot=True, xml=xml)
 
 @route('/grocery/products')
 def products():
@@ -388,12 +377,7 @@ FROM (
     finally:
         conn.commit()
     response.content_type = 'application/xhtml+xml; charset=utf-8'
-    return template(
-        "app/rest/templates/query-to-xml",
-        title="Products",
-        foot=True,
-        xml=xml
-    )
+    return template("query-to-xml", title="Products", foot=True, xml=xml)
 
 @route('/grocery/tags')
 def tags():
@@ -413,11 +397,6 @@ SELECT query_to_xml_and_xmlschema({inner}, false, false, ''::text)
     finally:
         conn.commit()
     response.content_type = 'application/xhtml+xml; charset=utf-8'
-    return template(
-        "app/rest/templates/query-to-xml",
-        title="Tags",
-        foot="false",
-        xml=xml
-    )
+    return template("query-to-xml", title="Tags", foot="false", xml=xml)
 
 run(host='0.0.0.0', port=6772, server='gunicorn')

+ 0 - 5
app/rest/templates/data-item.tpl

@@ -1,5 +0,0 @@
-<tr>
-    <td>{{ product }}</td>
-    <td>{{ category }}</td>
-    <td>{{ group }}</td>
-</tr>

+ 0 - 1
app/rest/templates/filter-heading.tpl

@@ -1 +0,0 @@
-<th>{{fname.title()}}</th>

+ 3 - 3
app/rest/templates/filter-set.tpl

@@ -1,11 +1,11 @@
 <div class="pure-g">
   <%
     for filter in (product, category, group):
-      include('app/rest/templates/include-exclude', **filter)
+      include('include-exclude', **filter)
     end
   %>
   <%
-    include('app/rest/templates/include-exclude', **tags)
-    include('app/rest/templates/select-one', **units)
+    include('include-exclude', **tags)
+    include('select-one', **units)
   %>
 </div>

+ 1 - 1
app/rest/templates/form.tpl

@@ -1,6 +1,6 @@
 <form id="filter" style="display: inline-flex"  method="{{ method }}" action="{{ action }}">
     <%
-    include('app/rest/templates/filter-set',
+    include('filter-set',
       product=product, category=category, group=group,
       tags=tags, units=units)
     %>

+ 2 - 2
app/rest/templates/include-exclude.tpl

@@ -6,7 +6,7 @@
     </div>
   </div>
   <%
-    include('app/rest/templates/select', id=f"{name}-include", name=name,
+    include('select', id=f"{name}-include", name=name,
       option_groups=_include["option_groups"] if "option_groups" in _include else [{
         "name": None,
         "options": _include["options"]
@@ -16,7 +16,7 @@
 
   <%
   if defined("_exclude"):
-    include('app/rest/templates/select', id=f"{name}-exclude", name=name,
+    include('select', id=f"{name}-exclude", name=name,
       option_groups=_exclude["option_groups"] if "option_groups" in _exclude else [{
         "name": None,
         "options": _exclude["options"]

+ 1 - 1
app/rest/templates/loading.tpl

@@ -18,7 +18,7 @@ body {
       <span class="loader"></span>
       <%
       for indicator in progress:
-          include('app/rest/templates/progress', **indicator)
+          include('progress', **indicator)
       end
       %>
     </div>

+ 1 - 1
app/rest/templates/optgroup.tpl

@@ -1,7 +1,7 @@
 <optgroup label="{{name}}">\\
 % for opt in options:
 
-%   include('app/rest/templates/option', **opt)
+%   include('option', **opt)
 % end
 
 </optgroup>

+ 1 - 1
app/rest/templates/select-one.tpl

@@ -5,6 +5,6 @@
       <h3>{{name.title()}}</h3>
     </div>
   </div>
-  <% include('app/rest/templates/select', id=f"{name}-select-one", name=name, options=options) %>
+  <% include('select', id=f"{name}-select-one", name=name, options=options) %>
 </div>
 </div>

+ 4 - 4
app/rest/templates/select.tpl

@@ -2,22 +2,22 @@
 % setdefault("option_groups", [{ "name": None, "options": (defined("options") and options) or [] }])
 <div class="pure-u-1">
 %  if defined("label"):
-%    include('app/rest/templates/label', id=id, label=label)
+%    include('label', id=id, label=label)
 %  end
 
 <select id="{{id}}" name="{{name}}" size=10 {{multiple}} style="width: 98%; margin: 1em">
 %  if defined("hint"):
-%    include('app/rest/templates/option', value=hint, disabled=True)
+%    include('option', value=hint, disabled=True)
 %  end
 
 %  for group in option_groups:
 %    if group["name"] is None:
 %      for opt in group["options"]:
 
-%        include('app/rest/templates/option', **opt)
+%        include('option', **opt)
 %      end
 %    else:
-%      include('app/rest/templates/optgroup', **group)
+%      include('optgroup', **group)
 %    end
 %  end
 %

+ 1 - 1
test/rest/templates/test_include-exclude.py

@@ -53,4 +53,4 @@ from bottle import template
     } }),
 ])
 def test_include_exclude_render_exact(expected, params):
-    assert template('app/rest/templates/include-exclude', **params) == expected
+    assert template('include-exclude', **params) == expected

+ 7 - 7
test/rest/templates/test_optgroup.py

@@ -33,7 +33,7 @@ from bottle import template
     }, ] }),
 ])
 def test_optgroup_render_exact(expected, params):
-    assert template('app/rest/templates/optgroup', **params) == expected
+    assert template('optgroup', **params) == expected
 
 
 @mark.parametrize('name', [
@@ -68,15 +68,15 @@ def test_optgroup_render_includes(name, value, selected, disabled, display):
             exp_exceptions.append(ex)
 
     if not exp_exceptions:
-        assert template('app/rest/templates/optgroup', name=name, options=[ options, ]) is not None
-        assert template('app/rest/templates/optgroup', name=name, options=[ options, options ]) is not None
+        assert template('optgroup', name=name, options=[ options, ]) is not None
+        assert template('optgroup', name=name, options=[ options, options ]) is not None
         return
 
     with raises(tuple(exp_exceptions)):
-        template('app/rest/templates/optgroup', name=name, options=[ options, ]) is not None
+        template('optgroup', name=name, options=[ options, ]) is not None
     
     with raises(tuple(exp_exceptions)):
-        template('app/rest/templates/optgroup', name=name, options=[ options, options ]) is not None
+        template('optgroup', name=name, options=[ options, options ]) is not None
     
     return
 
@@ -100,10 +100,10 @@ def test_optgroup_render(name, options):
             exp_exceptions.append(ex)
 
     if not exp_exceptions:
-        assert template('app/rest/templates/optgroup', **params) is not None
+        assert template('optgroup', **params) is not None
         return
 
     with raises(tuple(exp_exceptions)):
-        assert template('app/rest/templates/optgroup', **params) is not None
+        assert template('optgroup', **params) is not None
 
     return

+ 1 - 1
test/rest/templates/test_select.py

@@ -42,4 +42,4 @@ from bottle import template
     }, ] }),
 ])
 def test_select_render_exact(expected, params):
-    assert template('app/rest/templates/select', **params) == expected
+    assert template('select', **params) == expected