-
Notifications
You must be signed in to change notification settings - Fork 9
/
appMVC.py
172 lines (130 loc) · 3.89 KB
/
appMVC.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
# ===========================
# Example of MVC pattern on pure Python. Whiten for "Use Python in the Web"
# course. Institute Mathematics and Computer Science at Ural Federal University
# in 2014.
#
# By Pahaz Blinov.
# ===========================
__author__ = 'pahaz'
# ===========================
#
# Utilities
#
# ===========================
from cgi import escape
from urlparse import parse_qs
def http_status(code):
return "200 OK" if code == 200 else "404 Not Found"
# ===========================
#
# Model
#
# ===========================
import shelve
class TextModel(object):
DB_FILE = 'main.db'
def __init__(self):
self._db = shelve.open(self.DB_FILE)
def get(self, name, default_value):
return self._db.get(name, default_value)
def all(self):
return self._db.keys()
def set(self, key, value):
self._db[key] = value
self._db.sync()
def delete(self, key):
del self._db[key]
self._db.sync()
# ===========================
#
# Controller and Router
#
# ===========================
class Router(object):
def __init__(self):
self._paths = {}
def route(self, environ, start_response):
path = environ['PATH_INFO']
query_dict = parse_qs(environ['QUERY_STRING'])
if path in self._paths:
res = self._paths[path](query_dict)
else:
res = self.default_response(query_dict)
return res
def register(self, path, callback):
self._paths[path] = callback
def default_response(self, *args):
return 404, "Nooo 404!"
class TextController(object):
@staticmethod
def index(query_dict):
text = query_dict.get('id', [''])[0]
text = model.get(text, '')
titles = model.all()
context = {
'titles': titles,
'text': text,
}
return 200, view_text.render(context)
@staticmethod
def add(query_dict):
key = query_dict.get('k', [''])[0]
value = query_dict.get('v', [''])[0]
model.set(key, value)
context = {'url': "/text"}
return 200, view_redirect.render(context)
# ===========================
#
# View
#
# ===========================
class TextView(object):
@staticmethod
def render(context):
context['titles'] = [
'<li>{0}</li>'.format(x) for x in context['titles']
]
context['titles'] = '\n'.join(context['titles'])
t = """
<form method="GET">
<input type=text name=id />
<input type=submit value=read />
</form>
<form method="GET" action="/text/add" >
<input type=text name=k /> <input type=text name=v />
<input type=submit value=write />
</form>
<div style="color: gray;">{text}</div>
<ul>{titles}</ul>
"""
return t.format(**context)
class RedirectView(object):
@staticmethod
def render(context):
return '<meta http-equiv="refresh" content="0; url={url}" />' \
.format(**context)
# ===========================
#
# Main
#
# ===========================
rout = Router()
model = TextModel()
view_text = TextView()
view_redirect = RedirectView()
controller = TextController()
rout.register('/', lambda x: (200, "Index HI!"))
rout.register('/text', controller.index)
rout.register('/text/add', controller.add)
# ===========================
#
# WSGI
#
# ===========================
def application(environ, start_response):
http_status_code, response_body = rout.route(environ, start_response)
# response_body += '<br><br> The request ENV: {0}'.format(repr(environ))
http_status_code_and_msg = http_status(http_status_code)
response_headers = [('Content-Type', 'text/html')]
start_response(http_status_code_and_msg, response_headers)
return [response_body] # it could be any iterable.