-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault_app.py
91 lines (69 loc) · 3.6 KB
/
default_app.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
__author__ = 'mlaird'
from wsgiref.simple_server import make_server
from pprint import pformat
import urlparse
import default_web
from default_app_db import db_tables, check_schema
def get_form_data(wsgi_input, content_length):
form_data = pformat(wsgi_input.read(content_length))
form_data = form_data.replace("'", "")
form_data = urlparse.parse_qs(form_data)
for d in form_data:
if type(form_data[d]) is list:
form_data[d] = form_data[d][0]
return form_data
def application(environ, start_response):
response_params = {'html_wrapper_open': '<div class=container>',
'html_wrapper_close': '</div>',
'bootstrap': 'true'}
web_response = default_web.WebResponse(response_params)
web_response.add_head("<script src='/general/js_functions.js'></script>")
default_web.HtmlTable.table_class_default = 'table table-striped table-bordered'
request_path = environ['PATH_INFO'][1:]
if request_path == '':
web_response.add_body(default_web.main_page())
elif request_path in db_tables:
if environ['REQUEST_METHOD'] != 'POST':
web_response.add_body(default_web.table_management_page(request_path))
else:
form_data = get_form_data(environ['wsgi.input'], int(environ['CONTENT_LENGTH']))
web_response.add_body(default_web.table_management_page(request_path, form_data))
elif request_path[-4:] == 'edit' and request_path[:-5] in db_tables:
if environ['REQUEST_METHOD'] != 'POST':
web_response.add_body(default_web.list_rows_for_editing_page(request_path[:-5]))
else:
form_data = get_form_data(environ['wsgi.input'], int(environ['CONTENT_LENGTH']))
web_response.add_body(default_web.edit_db_row_page(request_path[:-5], form_data))
elif request_path[-6:] == 'delete' and request_path[:-7] in db_tables:
form_data = get_form_data(environ['wsgi.input'], int(environ['CONTENT_LENGTH']))
web_response.add_body(default_web.delete_db_row_page(request_path[:-7], form_data))
elif request_path[:3] == 'api' and request_path[4:] in db_tables:
web_response.set_content_type('json')
if environ['REQUEST_METHOD'] == 'GET':
web_response.add_body(default_web.api_get(request_path[4:]))
elif environ['REQUEST_METHOD'] == 'POST':
form_data = get_form_data(environ['wsgi.input'], int(environ['CONTENT_LENGTH']))
web_response.add_body(default_web.api_post(request_path[4:], form_data))
elif environ['REQUEST_METHOD'] == 'PUT':
form_data = get_form_data(environ['wsgi.input'], int(environ['CONTENT_LENGTH']))
web_response.add_body(default_web.api_put(request_path[4:], form_data))
elif environ['REQUEST_METHOD'] == 'DELETE':
form_data = get_form_data(environ['wsgi.input'], int(environ['CONTENT_LENGTH']))
web_response.add_body(default_web.api_delete(request_path[4:], form_data))
elif request_path == 'general/js_functions.js':
with open('general/js_functions.js') as f:
web_response.add_body(f.readlines())
web_response.set_content_type('javascript')
else:
web_response.set_status('Not Found')
web_response.add_body(default_web.not_found_error_page())
web_response.make_headers()
start_response(web_response.status, web_response.headers)
response = web_response.response
return response
if check_schema():
httpd = make_server('', 8080, application)
try:
httpd.serve_forever()
except KeyboardInterrupt:
httpd.server_close()