Skip to content

Commit 001a9db

Browse files
author
khz
committed
added support for select, textarea and actively show related objects in bs4 views
1 parent aa6ce3f commit 001a9db

File tree

4 files changed

+106
-22
lines changed

4 files changed

+106
-22
lines changed

pythononwheels/start/powlib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class powDecNew():
152152
def __init__(self):
153153
self.relations = {}
154154

155-
#
155+
156156
# below you can find the relation decorators
157157
# These types of relations are implementd:
158158
# Functional notion => sqlalchemy documentation notion
@@ -164,7 +164,7 @@ def __init__(self):
164164
# many_to_many => Many To Many
165165
# is_tree => Adjacence List
166166
#
167-
167+
168168
def has_many_and_belongs_to(self, child_as_str, backref=False):
169169
##
170170
#

pythononwheels/start/stubs/scaffold_edit_view.bs4

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,42 @@
9595
{% set class_hidden = "" %}
9696
{% end %}
9797
{% set _type = elem.schema[key]["type"] %}
98-
{% if _type == "string" or _type == "number" or _type == "integer" or _type == "float" %}
98+
{% if _type == "string" %}
99+
{% if "allowed" in model.schema[key].keys() %}
100+
<div class="form-group row">
101+
<label for="{{key}}" class="col-2 col-form-label">{{key}}</label>
102+
<div class="col-10">
103+
<select class="form-control" name="{{key}}" id="{{key}}">
104+
{% for val in model.schema[key]["allowed"] %}
105+
{% if val == elem.get(key) %}
106+
<option selected>{{val}}</option>
107+
{% else %}
108+
<option>{{val}}</option>
109+
{% end %}
110+
{% end %}
111+
</select>
112+
</div>
113+
</div>
114+
{% else %}
115+
{% if "maxlength" in model.schema[key].keys() and model.schema[key]["maxlength"] > 40 %}
116+
<div class="form-group row">
117+
<label for="{{key}}" class="col-2 col-form-label">{{key}}</label>
118+
<div class="col-10">
119+
<textarea class="form-control" name="{{key}}" id="{{key}}"
120+
rows={{model.schema[key]["maxlength"] / 30 }}></textarea>
121+
</div>
122+
</div>
123+
{% else %}
124+
<div class="form-group row">
125+
<label for="{{key}}" class="col-2 col-form-label">{{key}}</label>
126+
<div class="col-10">
127+
<input type="text" class="form-control" name="{{key}}" id="{{key}}"
128+
value="" />
129+
</div>
130+
</div>
131+
{% end %}
132+
{% end %}
133+
{% elif _type == "number" or _type == "integer" or _type == "float" %}
99134
<div class="form-group row">
100135
<label for="{{key}}" class="col-2 col-form-label">{{key}}</label>
101136
<div class="col-10">
@@ -109,11 +144,12 @@
109144
<div class="col-sm-10">
110145
<div class="form-check">
111146
<label class="form-check-label">
112-
<input class="form-check-input" type="checkbox" name="{{key}}" id="{{key}}"
147+
<input class="form-check-input" type="hidden" name="{{key}}" id="{{key}}" value="0" />
148+
<input class="form-check-input" type="checkbox" name="{{key}}" id="{{key}}" value="1"
113149
{% if elem.get(key) == True %}
114150
checked="checked"
115151
{% end %}
116-
> {{key}}
152+
/> {{key}}
117153
</label>
118154
</div>
119155
</div>

pythononwheels/start/stubs/scaffold_new_view.bs4

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,40 @@
7575
</div>
7676

7777
<form id="create_form" name="create_form">
78-
7978
{% for key in model.schema.keys() %}
8079
{% if key not in cfg.myapp["internal_fields"] %}
8180
{% set _type = model.schema[key]["type"] %}
8281
{% if _type == "string" %}
83-
<div class="form-group row">
84-
<label for="{{key}}" class="col-2 col-form-label">{{key}}</label>
85-
<div class="col-10">
86-
<input type="text" class="form-control" name="{{key}}" id="{{key}}"
87-
value="" />
88-
</div>
89-
</div>
82+
{% if "allowed" in model.schema[key].keys() %}
83+
<div class="form-group row">
84+
<label for="{{key}}" class="col-2 col-form-label">{{key}}</label>
85+
<div class="col-10">
86+
<select class="form-control" name="{{key}}" id="{{key}}">
87+
{% for elem in model.schema[key]["allowed"] %}
88+
<option>{{elem}}</option>
89+
{% end %}
90+
</select>
91+
</div>
92+
</div>
93+
{% else %}
94+
{% if "maxlength" in model.schema[key].keys() and model.schema[key]["maxlength"] > 40 %}
95+
<div class="form-group row">
96+
<label for="{{key}}" class="col-2 col-form-label">{{key}}</label>
97+
<div class="col-10">
98+
<textarea class="form-control" name="{{key}}" id="{{key}}"
99+
rows={{model.schema[key]["maxlength"] / 30 }}></textarea>
100+
</div>
101+
</div>
102+
{% else %}
103+
<div class="form-group row">
104+
<label for="{{key}}" class="col-2 col-form-label">{{key}}</label>
105+
<div class="col-10">
106+
<input type="text" class="form-control" name="{{key}}" id="{{key}}"
107+
value="" />
108+
</div>
109+
</div>
110+
{% end %}
111+
{% end %}
90112
{% elif _type == "number" or _type == "integer" or _type == "float" %}
91113
<div class="form-group row">
92114
<label for="{{key}}" class="col-2 col-form-label">{{key}}</label>
@@ -101,7 +123,8 @@
101123
<div class="col-sm-10">
102124
<div class="form-check">
103125
<label class="form-check-label">
104-
<input class="form-check-input" type="checkbox" name="{{key}}" id="{{key}}"> {{key}}
126+
<input class="form-check-input" type="hidden" name="{{key}}" id="{{key}}" value="0" />
127+
<input class="form-check-input" type="checkbox" name="{{key}}" id="{{key}}" value="1" /> {{key}}
105128
</label>
106129
</div>
107130
</div>
@@ -115,14 +138,26 @@
115138
</div>
116139
</div>
117140
{% elif _type == "list" %}
118-
<div class="form-group row">
119-
<label for="{{key}}" class="col-2 col-form-label">{{key}}</label>
120-
<div class="col-10">
121-
<input type="text" class="form-control" name="{{key}}" id="{{key}}"
122-
value="" />
123-
</div>
124-
</div>
125-
{% end %}
141+
{% if "allowed" in model.schema[key].keys() %}
142+
<div class="form-group row">
143+
<label for="{{key}}" class="col-2 col-form-label">{{key}}</label>
144+
<div class="col-10">
145+
<select multiple class="form-control" name="{{key}}" id="{{key}}">
146+
{% for elem in model.schema[key]["allowed"] %}
147+
<option>{{elem}}</option>
148+
{% end %}
149+
</select>
150+
</div>
151+
</div>
152+
{% else %}
153+
<div class="form-group row">
154+
<label for="{{key}}" class="col-2 col-form-label">{{key}}</label>
155+
<div class="col-10">
156+
<input type="text" class="form-control" name="{{key}}" id="{{key}}"
157+
value="" />
158+
</div>
159+
</div>
160+
{% end %}
126161
{% end %}
127162
{% end %}
128163

pythononwheels/start/stubs/scaffold_show_view.bs4

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<p class="lead">See your data below:</p>
1111
</div>
1212
</div>
13+
<h2>Data:</h2>
1314
<table class="table table-bordered">
1415

1516
{% for key in model.schema.keys() %}
@@ -21,6 +22,18 @@
2122

2223
</table>
2324
<hr>
25+
<h2>Related Data Objects:</h2>
26+
<table class="table table-bordered">
27+
{% for rel in model.get_relations() %}
28+
{% for idx, val in enumerate(getattr(data, rel)) %}
29+
<tr>
30+
<td><b><a href="/{{rel}}/{{getattr(val, "id")}}">{{idx}}</a></b></td>
31+
<td>{{val}}</td>
32+
</tr>
33+
{% end %}
34+
{% end %}
35+
</table>
36+
<hr>
2437

2538
<a href="/{{base_route_rest}}" class="btn btn-primary">Back to {{handler_name}}.list</a>
2639

0 commit comments

Comments
 (0)