Browse Source

use rebase instaed of include for select options

Daniel Sheffield 1 năm trước cách đây
mục cha
commit
0533144aa9

+ 1 - 1
app/rest/pyapi.py

@@ -142,7 +142,7 @@ def get_option_groups(data, filter_data, k, g, _type):
         assert set(selected) - set(unselected) == set(selected), f"{set(selected)} {set(unselected)}"
         
         yield {
-            "name": group,
+            "optgroup": group,
             "options": sorted(map(lambda x: {
                 "selected": x[0],
                 "value": f"{prefix}{x[1]}",

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

@@ -7,8 +7,7 @@
   </div>
   <%
     include('select', id=f"{name}-include", name=name,
-      option_groups=_include["option_groups"] if "option_groups" in _include else [{
-        "name": None,
+      children=_include["option_groups"] if "option_groups" in _include else [{
         "options": _include["options"]
       }],
       hint="Include", multiple=True)
@@ -17,8 +16,7 @@
   <%
   if defined("_exclude"):
     include('select', id=f"{name}-exclude", name=name,
-      option_groups=_exclude["option_groups"] if "option_groups" in _exclude else [{
-        "name": None,
+      children=_exclude["option_groups"] if "option_groups" in _exclude else [{
         "options": _exclude["options"]
       }], hint="Exclude", multiple=True)
   end

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

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

+ 5 - 0
app/rest/templates/options.tpl

@@ -0,0 +1,5 @@
+% defined("optgroup") and rebase('optgroup', name=optgroup)
+% for opt in options:
+
+%   include('option', **opt)
+% end

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

@@ -5,6 +5,9 @@
       <h3>{{name.title()}}</h3>
     </div>
   </div>
-  <% include('select', id=f"{name}-select-one", name=name, options=options) %>
+  <% include('select', id=f"{name}-select-one", name=name,
+     children=[{
+        "options": options
+     }]) %>
 </div>
 </div>

+ 5 - 11
app/rest/templates/select.tpl

@@ -1,5 +1,5 @@
+% from bottle import template
 % multiple = (get("multiple", False) and "multiple") or ""
-% setdefault("option_groups", [{ "name": None, "options": (defined("options") and options) or [] }])
 <div class="pure-u-1">
 %  if defined("label"):
 %    include('label', id=id, label=label)
@@ -10,17 +10,11 @@
 %    include('option', value=hint, disabled=True)
 %  end
 
-%  for group in option_groups:
-%    if group["name"] is None:
-%      for opt in group["options"]:
-
-%        include('option', **opt)
-%      end
-%    else:
-%      include('optgroup', **group)
+%  for child in children:
+%    if "optgroup" in child and child["optgroup"] is None:
+%      del child["optgroup"]
 %    end
+{{!template('options', **child)}}
 %  end
-%
-
 </select>
 </div>

+ 12 - 13
test/rest/templates/test_optgroup.py

@@ -1,4 +1,3 @@
-import pytest
 from pytest import mark, raises
 from bottle import template
 
@@ -6,7 +5,7 @@ from bottle import template
     ("""<optgroup label="Group">
   <option value="val-to-backend"  selected>val-displayed</option>
 </optgroup>""", {
-    "name": "Group", "options": [
+    "optgroup": "Group", "options": [
     {
         "value": "val-to-backend",
         "selected": True,
@@ -14,7 +13,7 @@ from bottle import template
     }, ] }),
     ("""<optgroup label="Group">
   <option value="val-to-backend" disabled >val-to-backend</option>
-</optgroup>""", { "name": "Group", "options": [
+</optgroup>""", { "optgroup": "Group", "options": [
     {
         "value": "val-to-backend",
         "disabled": True,
@@ -22,7 +21,7 @@ from bottle import template
     ("""<optgroup label="Group">
   <option value="val1-to-backend"  >val1</option>
   <option value="val2-to-backend" disabled >val2-to-backend</option>
-</optgroup>""", { "name": "Group", "options": [
+</optgroup>""", { "optgroup": "Group", "options": [
     {
         "value": "val1-to-backend",
         "display": "val1",
@@ -33,7 +32,7 @@ from bottle import template
     }, ] }),
 ])
 def test_optgroup_render_exact(expected, params):
-    assert template('optgroup', **params) == expected
+    assert template('options', **params) == expected
 
 
 @mark.parametrize('name', [
@@ -68,21 +67,21 @@ def test_optgroup_render_includes(name, value, selected, disabled, display):
             exp_exceptions.append(ex)
 
     if not exp_exceptions:
-        assert template('optgroup', name=name, options=[ options, ]) is not None
-        assert template('optgroup', name=name, options=[ options, options ]) is not None
+        assert template('options', optgroup=name, options=[ options, ]) is not None
+        assert template('options', optgroup=name, options=[ options, options ]) is not None
         return
 
     with raises(tuple(exp_exceptions)):
-        template('optgroup', name=name, options=[ options, ]) is not None
+        template('options', optgroup=name, options=[ options, ]) is not None
     
     with raises(tuple(exp_exceptions)):
-        template('optgroup', name=name, options=[ options, options ]) is not None
+        template('options', optgroup=name, options=[ options, options ]) is not None
     
     return
 
 @mark.parametrize('name', [
-    ({}, NameError, "name 'name' is not defined"),
-    *[ { "name": v } for v in ("", "group", None)],
+    {},
+    *[ { "optgroup": v } for v in ("", "group", None)],
 ])
 @mark.parametrize('options', [
     ({}, NameError, "name 'options' is not defined"),
@@ -100,10 +99,10 @@ def test_optgroup_render(name, options):
             exp_exceptions.append(ex)
 
     if not exp_exceptions:
-        assert template('optgroup', **params) is not None
+        assert template('options', **params) is not None
         return
 
     with raises(tuple(exp_exceptions)):
-        assert template('optgroup', **params) is not None
+        assert template('options', **params) is not None
 
     return

+ 46 - 0
test/rest/templates/test_select-one.py

@@ -0,0 +1,46 @@
+import pytest
+from pytest import mark, raises
+from bottle import template
+
+@mark.parametrize('expected, params', [
+    ("""<div class="pure-u-1-3 pure-u-lg-1-8">
+<div class="pure-g">
+  <div class="pure-u-1">
+    <div class="l-box">
+      <h3>Unit</h3>
+    </div>
+  </div>
+<div class="pure-u-1">
+
+<select id="unit-select-one" name="unit" size=10  style="width: 98%; margin: 1em">
+
+
+  <option value="Bags"  >Bags</option>
+  <option value="Bunches"  >Bunches</option>
+  <option value="L"  >L</option>
+  <option value="Pieces"  >Pieces</option>
+  <option value="g"  >g</option>
+  <option value="kg"  selected>kg</option>
+  <option value="mL"  >mL</option>
+</select>
+</div></div>
+</div>""", {
+    "name": "unit", "options": [{
+        "value": "Bags",
+    }, {
+        "value": "Bunches",
+    }, {
+        "value": "L",
+    }, {
+        "value": "Pieces",
+    }, {
+        "value": "g",
+    }, {
+        "value": "kg",
+        "selected": True,
+    }, {
+        "value": "mL",
+    }, ]}, ),
+])
+def test_select_one_render_exact(expected, params):
+    assert template('select-one', **params) == expected

+ 39 - 4
test/rest/templates/test_select.py

@@ -14,7 +14,7 @@ from bottle import template
 </select>
 </div>""", {
     "id": "select-id", "label": "Choose: ", "name": "select-name",
-    "hint": "hint", "multiple": True, "option_groups": [{ "name": "Group", "options": [
+    "hint": "hint", "multiple": True, "children": [{"optgroup": "Group", "options": [
     {
         "value": "val1-to-backend",
         "display": "val1",
@@ -22,7 +22,7 @@ from bottle import template
     {
         "value": "val2",
         "disabled": True,
-    }, ] }, ]}),
+    }, ]}, ]}),
     ("""<div class="pure-u-1">
 
 <select id="select-unit-id" name="unit" size=10  style="width: 98%; margin: 1em">
@@ -32,14 +32,49 @@ from bottle import template
   <option value="val2"  selected>val2</option>
 </select>
 </div>""",{
-        "id": "select-unit-id", "name": "unit", "options": [
+    "id": "select-unit-id", "name": "unit", "children": [{ "options": [
     {
         "value": "val1",
     },
     {
         "value": "val2",
         "selected": True,
-    }, ] }),
+    }, ]}, ]}),
+    ("""<div class="pure-u-1">
+<label for="select-id">Choose: </label>
+<select id="select-id" name="select-name" size=10 multiple style="width: 98%; margin: 1em">
+  <option value="hint" disabled >hint</option>
+<optgroup label="Group">
+  <option value="val1-to-backend"  >val1</option>
+  <option value="val2" disabled >val2</option>
+</optgroup>
+
+  <option value="val1"  >val1</option>
+  <option value="val2"  selected>val2</option>
+</select>
+</div>""",{
+    "id": "select-id", "label": "Choose: ", "name": "select-name",
+    "hint": "hint", "multiple": True, "children": [
+        {"optgroup": "Group", "options": [
+            {
+                "value": "val1-to-backend",
+                "display": "val1",
+            },
+            {
+                "value": "val2",
+                "disabled": True,
+            }
+        ]},
+        { "options": [
+            {
+                "value": "val1",
+            },
+            {
+                "value": "val2",
+                "selected": True,
+            },
+        ]},
+    ]})
 ])
 def test_select_render_exact(expected, params):
     assert template('select', **params) == expected