-
Notifications
You must be signed in to change notification settings - Fork 0
/
default_web.py
161 lines (128 loc) · 6.15 KB
/
default_web.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
__author__ = 'mlaird'
import default_app_db
import json
from general.html_functions import *
def main_page():
output = ["<h3>Main Page</h3>"]
for t in default_app_db.db_tables:
output.append("<p><a href='/%s'>%s Management Page</a></p>" % (t, default_app_db.db_tables[t]['label']))
return output
def table_management_page(table_name, form_data=None):
if form_data:
add_res = default_app_db.add_table_row(table_name, form_data)
else:
add_res = False
output = ["<h3>%s Management</h3>" % default_app_db.db_tables[table_name]['label'], "<p><b>Known entries:</b></p>"]
entries = default_app_db.get_all_table_rows(table_name)
if len(entries) > 0:
entries_table = make_html_table_from_db_table(default_app_db.db_tables[table_name], entries, 'dbTable')
output.extend(entries_table.return_table())
output.extend("<p><a href='/%s/edit'>Edit these entries.</a></p>" % table_name)
else:
output.append("<p>There are no entries in the database.</p>")
if form_data:
if add_res:
output.append("<p>New entry added!</p>")
else:
output.append("<p>New entry could not be added to the database.</p>")
new_entry_form = make_html_form_from_db_table_dict(default_app_db.db_tables[table_name],
default_app_db.get_joined_table_rows(table_name))
new_entry_form.set_title('Add new entry:')
output.extend(new_entry_form.return_form())
output.append("<p><a href='/'>Return to the main page.</a></p>")
output.append("<script type='text/javascript'>document.onload = "
"setTableHeaderClickFunctions('dbTable','headerRow');</script>")
return output
def list_rows_for_editing_page(table_name):
output = ["<h3>Edit %s</h3>" % default_app_db.db_tables[table_name]['label'], "<p><b>Known entries:</b></p>"]
entries = default_app_db.get_all_table_rows(table_name)
if len(entries) > 0:
entries_table = HtmlTable()
for e in entries:
row_to_add = []
id_string = ""
for uc in default_app_db.db_tables[table_name]['unique_columns']:
id_string += "%s " % e[uc]
row_to_add.append(id_string)
edit_form = HtmlForm()
delete_form = HtmlForm()
edit_form.open_form = "<form method='post' action='/%s/edit'>" % table_name
delete_form.open_form = "<form method='post' action='/%s/delete'>" % table_name
for c in default_app_db.db_tables[table_name]['columns']:
edit_form.add_hidden_input(c['name'], e[c['name']])
delete_form.add_hidden_input(c['name'], e[c['name']])
edit_form.add_submit_button('Edit')
delete_form.add_submit_button('Delete')
row_to_add.append(''.join(edit_form.return_form()))
row_to_add.append(''.join(delete_form.return_form()))
entries_table.add_row(row_to_add)
output.extend(entries_table.return_table())
output.append("<p><a href='/'>Return to the main page.</a></p>")
return output
def edit_db_row_page(table_name, form_data):
output = ["<h3>Edit %s</h3>" % default_app_db.db_tables[table_name]['label']]
if 'confirm_edit' in form_data.keys():
if default_app_db.update_table_row(table_name, form_data):
output.append('<p>Database entry updated.</p>')
else:
output.append('<p>Database entry could not be updated.</p>')
edit_entry_form = make_html_form_from_db_table_dict(default_app_db.db_tables[table_name],
default_app_db.get_joined_table_rows(table_name),
form_data)
edit_entry_form.add_hidden_input('confirm_edit', True)
for col_name in form_data.keys():
if '_old_' != col_name[5:]:
edit_entry_form.add_hidden_input('_old_' + col_name, form_data[col_name])
output.extend(edit_entry_form.return_form())
output.append("<p><a href='/'>Return to the main page.</a></p>")
return output
def delete_db_row_page(table_name, form_data):
output = ["<h3>Delete Entry</h3>"]
if 'confirm_delete' in form_data.keys():
if default_app_db.delete_table_row(table_name, form_data):
output.append("<p>Database entry deleted.</p>")
else:
output.append("<p>Database entry could not be deleted.</p>")
else:
id_string = ""
for uc in default_app_db.db_tables[table_name]['unique_columns']:
id_string += "%s " % form_data[uc]
output.append("<p>You are going to delete the entry '%s' from the '%s' table.</p>" %
(id_string, default_app_db.db_tables[table_name]['label']))
confirm_delete_form = HtmlForm()
confirm_delete_form.add_text_line('<b>Are you sure?</b>')
confirm_delete_form.add_submit_button('Delete')
confirm_delete_form.add_hidden_input('confirm_delete', True)
for col_name in form_data.keys():
confirm_delete_form.add_hidden_input(col_name, form_data[col_name])
output.extend(confirm_delete_form.return_form())
output.append("<p><a href='/'>Return to the main page.</a></p>")
return output
def not_found_error_page():
output = ["<p>This page could not be found.</p>"]
return output
def api_get(table_name):
rows = default_app_db.get_all_table_rows(table_name)
output = json.dumps(rows)
return output
def api_post(table_name, form_data):
result = default_app_db.add_table_row(table_name, form_data)
if result:
output = {"status": "success"}
else:
output = {"status": "error"}
return json.dumps(output)
def api_put(table_name, form_data):
result = default_app_db.update_table_row(table_name, form_data)
if result:
output = {"status": "success"}
else:
output = {"status": "error"}
return json.dumps(output)
def api_delete(table_name, form_data):
result = default_app_db.delete_table_row(table_name, form_data)
if result:
output = {"status": "success"}
else:
output = {"status": "error"}
return json.dumps(output)