-
Notifications
You must be signed in to change notification settings - Fork 0
/
meep_example_app.py
131 lines (96 loc) · 4.08 KB
/
meep_example_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
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
import meeplib
import traceback
import cgi
import meepcookie
def initialize():
# create a default user
u = meeplib.User('test', 'foo')
# create a single message
meeplib.Message('my title', 'This is my message!', u)
# done.
class MeepExampleApp(object):
"""
WSGI app object.
"""
def index(self, environ, start_response):
start_response("200 OK", [('Content-type', 'text/html')])
username = 'test'
return ["""you are logged in as user: %s.<p><a href='/m/add'>Add a message</a><p><a href='/login'>Log in</a><p><a href='/logout'>Log out</a><p><a href='/m/list'>Show messages</a>""" % (username,)]
def login(self, environ, start_response):
# hard code the username for now; this should come from Web input!
username = 'test'
# retrieve user
user = meeplib.get_user(username)
# set content-type
headers = [('Content-type', 'text/html')]
cookie_name, cookie_val = \
meepcookie.make_set_cookie_header('username',
user.username)
print "setting cookie!!!"
headers.append((cookie_name, cookie_val))
# send back a redirect to '/'
k = 'Location'
v = '/'
headers.append((k, v))
start_response('302 Found', headers)
return "no such content"
def logout(self, environ, start_response):
# does nothing
headers = [('Content-type', 'text/html')]
# send back a redirect to '/'
k = 'Location'
v = '/'
headers.append((k, v))
start_response('302 Found', headers)
return "no such content"
def list_messages(self, environ, start_response):
messages = meeplib.get_all_messages()
s = []
for m in messages:
s.append('id: %d<p>' % (m.id,))
s.append('title: %s<p>' % (m.title))
s.append('message: %s<p>' % (m.post))
s.append('author: %s<p>' % (m.author.username))
s.append('<hr>')
s.append("<a href='../../'>index</a>")
headers = [('Content-type', 'text/html')]
start_response("200 OK", headers)
return ["".join(s)]
def add_message(self, environ, start_response):
headers = [('Content-type', 'text/html')]
start_response("200 OK", headers)
return """<form action='add_action' method='POST'>Title: <input type='text' name='title'><br>Message:<input type='text' name='message'><br><input type='submit'></form>"""
def add_message_action(self, environ, start_response):
form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ)
title = form['title'].value
message = form['message'].value
username = 'test'
user = meeplib.get_user(username)
new_message = meeplib.Message(title, message, user)
headers = [('Content-type', 'text/html')]
headers.append(('Location', '/m/list'))
start_response("302 Found", headers)
return ["message added"]
def __call__(self, environ, start_response):
# store url/function matches in call_dict
call_dict = { '/': self.index,
'/login': self.login,
'/logout': self.logout,
'/m/list': self.list_messages,
'/m/add': self.add_message,
'/m/add_action': self.add_message_action
}
# see if the URL is in 'call_dict'; if it is, call that function.
url = environ['PATH_INFO']
fn = call_dict.get(url)
if fn is None:
start_response("404 Not Found", [('Content-type', 'text/html')])
return ["Page not found."]
try:
return fn(environ, start_response)
except:
tb = traceback.format_exc()
x = "<h1>Error!</h1><pre>%s</pre>" % (tb,)
status = '500 Internal Server Error'
start_response(status, [('Content-type', 'text/html')])
return [x]