-
Notifications
You must be signed in to change notification settings - Fork 0
/
html_renderer.py
413 lines (353 loc) · 13.4 KB
/
html_renderer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
from page_config import get_public_pages, get_registration_pages, get_page_config
import json
import re
class View():
def make(self, page_name, user=None):
"""
Builds and returns the html/javascript/css for a given page.
Parameters
----------
arg1 : page_name
Name of page being rendered
arg2 : user
User object if the user is signed in
Returns
-------
string
html/javascript/css
"""
if(user):
self.user_attributes = user.get_attributes()
else:
self.user_attributes = {}
js_options = self.get_js_options(page_name, user)
view_parts = self.get_view_parts(page_name)
page_content_signed = 'page-content-signed-in'
if page_name in get_public_pages():
top_nav = ''
side_nav = ''
page_content_signed = 'page-content-signed-out'
else:
top_nav = view_parts['top_nav.html'].format(
nav_links=view_parts['nav_links.html'].format(
current_step=user.get_current_step(),
active_home = self.is_active('active_home', page_name),
active_activities = self.is_active('active_activities', page_name),
active_register = self.is_active('active_register', page_name)
),
user=self.user_attributes
)
side_nav = view_parts['side_nav.html'].format(
nav_links=view_parts['nav_links.html'].format(
current_step=user.get_current_step(),
active_home = self.is_active('active_home', page_name),
active_activities = self.is_active('active_activities', page_name),
active_register = self.is_active('active_register', page_name)
)
)
header = view_parts['header.html'].format(
w3_css=view_parts['w3.css'],
style_css=view_parts['style.css']
)
return view_parts['common.html'].format(
header=header,
view_content= self.get_view_content(
view_parts,
page_name
),
footer=view_parts['footer.html'],
common_js=view_parts['common.js'],
top_nav=top_nav,
side_nav=side_nav,
js_options=js_options,
page_content_signed=page_content_signed,
user=self.user_attributes
)
def get_view_content(self, view_parts, page_name):
"""
Returns html from the views/ html directory. If its a registration page,
it will add the registration specific content
----------
arg1 : view_parts
Dictionary of cached view html strings
arg2 : page_name
Name of page being rendered
Returns
-------
string
html/javascript/css
"""
view_html = view_parts[page_name + '.html']
if page_name in get_registration_pages():
register_form = self.get_register_form(page_name)
view_html = view_html.format(
register_form=register_form
)
return view_html
def get_register_form(self, page_name):
"""
Builds and returns the form and form fields html.
----------
arg1 : page_name
Name of page being rendered
Returns
-------
string
html/javascript/css
"""
form_html = '<form class="w3-container w3-card-4 w3-sand">'
form_html += self.get_progress_bar(page_name)
page_config = get_page_config(page_name)
for row in page_config['layout']:
form_html += '<div class="w3-row row">'
for field in row:
form_html += self.field_factory(field, page_config['fields'][field])
form_html += '</div>'
form_html += '<p><button type="button" id="submit" class="w3-btn w3-padding w3-dark-grey" style="width:120px">Submit ❯</button></p>'
form_html += '</form>'
return form_html
def get_progress_bar(self, page_name):
"""
Builds and returns the progress bar html.
----------
arg1 : page_name
Name of page being rendered
Returns
-------
string
html/javascript/css
"""
if self.user_attributes['status'] == 'complete':
percent = '100'
else:
if page_name == 'register_rsvp':
percent = '0'
elif page_name == 'register_profile':
percent = '25'
elif page_name == 'register_activities':
percent = '50'
elif page_name == 'register_hotel':
percent = '75'
progress_bar = '<div class="w3-round progress-bar">'
progress_bar += '<div class="w3-container w3-round w3-deep-orange" style="width:{percent}%">{percent}%</div>'
progress_bar += '</div>'
return progress_bar.format(percent=percent)
def field_factory(self, field_name, field_meta):
"""
Factory pattern for building and returning specific form field html
----------
arg1 : field_name
Name of field being rendered
arg2 : field_meta
Field specific meta data
Returns
-------
string
html/javascript/css
"""
if field_meta['type'] == 'boolean':
return self.get_boolean_field(field_name, field_meta)
elif field_meta['type'] == 'varchar':
return self.get_varchar_field(field_name, field_meta)
elif field_meta['type'] == 'email':
return self.get_varchar_field(field_name, field_meta)
elif field_meta['type'] == 'radio':
return self.get_radio_field(field_name, field_meta)
elif field_meta['type'] == 'dropdown':
return self.get_dropdown_field(field_name, field_meta)
elif field_meta['type'] == 'html':
return self.get_html_field(field_name, field_meta)
else:
return ''
def get_boolean_field(self, field_name, field_meta):
"""
Builds and returns the html for a yes/no radio button
----------
arg1 : field_name
Name of field being rendered
arg2 : field_meta
Field specific meta data
Returns
-------
string
html/javascript/css
"""
values = ['Yes', 'No']
field_class = self.get_field_class(field_meta, 'radio')
type = 'radio'
field_html = ''
label = '<label class="w3-text-grey">{label_value}</label><br>'
field_html += label.format(label_value=field_meta['label'])
for value in values:
if field_name in self.user_attributes and self.user_attributes[field_name] == self.snake_case(value):
checked = 'checked'
else:
checked = ''
field_html += '<input name="{name}" class="{field_class}" type="{type}" value="{value}" {checked}> <label class="w3-text-grey">{label}</label> '
field_html = field_html.format(name=field_name, field_class=field_class, type=type, value=self.snake_case(value), label=value, checked=checked)
return field_html
def get_varchar_field(self, field_name, field_meta):
"""
Builds and returns the html for an input box
----------
arg1 : field_name
Name of field being rendered
arg2 : field_meta
Field specific meta data
Returns
-------
string
html/javascript/css
"""
field_class = self.get_field_class(field_meta, 'input')
type = 'input'
field_html = ''
label = '<label class="w3-text-grey">{label_value}</label><br>'
if field_name in self.user_attributes:
saved_value = self.user_attributes[field_name]
else:
saved_value = ''
field_html += label.format(label_value=field_meta['label'])
field_html += '<input name="{name}" class="{field_class}" type="{type}" value="{saved_value}">'
field_html = field_html.format(name=field_name, field_class=field_class, type=type, saved_value=saved_value)
return field_html
def get_radio_field(self, field_name, field_meta):
"""
Builds and returns the html for a list of radio buttons
----------
arg1 : field_name
Name of field being rendered
arg2 : field_meta
Field specific meta data
Returns
-------
string
html/javascript/css
"""
field_class = self.get_field_class(field_meta, 'radio')
type = 'radio'
field_html = ''
label = '<label class="w3-text-grey">{label_value}</label><br>'
field_html += label.format(label_value=field_meta['label'])
for value in field_meta['values']:
if field_name in self.user_attributes and self.user_attributes[field_name] == self.snake_case(value):
checked = 'checked'
else:
checked = ''
field_html += '<input name="{name}" class="{field_class}" type="{type}" value="{value}" {checked}> <label class="w3-text-grey">{label}</label><br> '
field_html = field_html.format(name=field_name, field_class=field_class, type=type, value=self.snake_case(value), label=value, checked=checked)
return field_html
def get_dropdown_field(self, field_name, field_meta):
"""
Builds and returns the html for a dropdown field
----------
arg1 : field_name
Name of field being rendered
arg2 : field_meta
Field specific meta data
Returns
-------
string
html/javascript/css
"""
type = 'select'
field_html = ''
field_html += '<select class="w3-select w3-text-grey" name="{name}">'.format(name=field_name, label_value=field_meta['label'])
for value in field_meta['values']:
if field_name in self.user_attributes and self.user_attributes[field_name] == self.snake_case(value):
selected ='selected'
else:
selected = ''
field_html += '<option class="w3-text-grey" value="{value}" {selected}>{label}</option>'
field_html = field_html.format(value=self.snake_case(value), label=value, selected=selected)
field_html += '</select>'
return field_html
def get_html_field(self, field_name, field_meta):
"""
Builds and returns the html a configured block of html (text between fields)
----------
arg1 : field_name
Name of field being rendered
arg2 : field_meta
Field specific meta data
Returns
-------
string
html/javascript/css
"""
return field_meta['html']
def get_field_class(self, field_meta, type):
"""
Builds and returns css classes for specific fields
----------
arg1 : field_meta
Field specific meta data
arg2 : type
the field type
Returns
-------
string
html/javascript/css
"""
field_class = 'w3-border w3-' + type
if field_meta['required'] == True:
field_class += ' w3-leftbar'
return field_class
def get_view_parts(self, page_name):
view_parts = {}
view_item_names = ['header.html', 'footer.html', 'w3.css', 'style.css',
'common.html', 'common.js', 'nav_links.html', 'top_nav.html',
'side_nav.html']
view_item_names.append(page_name + '.html')
for item in view_item_names:
with open('views/' + item, encoding='utf8') as file:
view_parts[item] = file.read()
return view_parts
def get_js_options(self, page_name, user):
"""
Inserts serverside javascript variables
----------
arg1 : field_meta
Field specific meta data
arg2 : user
The user attributes or empty dict
Returns
-------
string
json string
"""
js_options = {'page_name':page_name}
return json.dumps(js_options, ensure_ascii=False)
def is_active(self, link, page_name):
"""
The active link for the side navigation menu
----------
arg1 : link
Field specific meta data
arg2 : page_name
Name of page being rendered
Returns
-------
string
Returns the word active or empty string
"""
if link == 'active_home' and page_name == 'home':
return 'active'
if link == 'active_activities' and page_name == 'activities':
return 'active'
if link == 'active_register' and page_name in get_registration_pages():
return 'active'
return ''
def snake_case(self, string):
"""
Formats radio/dropdown save values into snake_case
----------
arg1 : string
String being formatted
Returns
-------
string
Returns formatted string
"""
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', string.replace(" ", ""))
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()