/
main.py
executable file
·175 lines (143 loc) · 5.09 KB
/
main.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
#!/usr/bin/python
import web
import status
import util
import bcrypt
import uuid
urls = (
'/', 'Index',
'/u/questions/([0-9]+)', 'RealQPage',
'/u/([a-zA-Z_.]+)', 'Static',
'/q/?', 'QList',
'/q/([0-9]+)/?', 'QPage',
'/s/logout/?', 'Logout',
'/s/login/?', 'Login',
'/s/register/?', 'Register'
)
class Index(object):
def GET(self):
sess = util.get_sess()
raise status.ApiReturn('static/index', sess)
class Static(object):
def GET(self,page):
sess = util.get_sess()
raise status.ApiReturn('static/' + page, sess)
class RealQPage(object):
def GET(self,q):
sess = util.get_sess()
raise status.ApiReturn('static/question', q, sess)
class QList(object):
def GET(self):
params = web.input(limit=20, offset=0)
res2 = util.select('qlist JOIN users ON qlist.uid = users.id', what='qlist.id AS id, users.name AS name, qlist.opt1 AS opt1, qlist.opt2 AS opt2', limit=params.limit, order='qlist.id DESC', offset=params.offset)
res = [r for r in res2]
if len(res) < 1 and params.offset>0:
raise status.ApiError('403 Invalid Page')
raise status.ApiReturn('templates/qlist', res)
def POST(self):
sess = util.get_sess()
if not sess:
raise status.ApiError('401 Unauthorized')
qx = web.input()
try:
util.insert('qlist', uid=sess.uid, opt1=qx.opt1, opt2=qx.opt2)
except AttributeError:
raise status.ApiError('401 Missing Input(s)')
raise status.ApiError('200 OK')
class QPage(object):
def GET(self, pid):
params = web.input(limit=20, offset=0)
qx = util.select_one('qlist JOIN users ON qlist.uid = users.id', where='qlist.id=$id', vars={'id': pid})
if qx is None:
raise status.ApiError('401 Invalid Question')
res2 = util.select('alist JOIN qlist ON qlist.id = alist.qid JOIN users ON qlist.uid = users.id', where='qid=$id', limit=params.limit, offset=params.offset, order='alist.id DESC', vars={'id': pid})
res = [r for r in res2]
if len(res) < 1 and params.offset>0:
raise status.ApiError('403 Invalid Page')
sess = util.get_sess()
raise status.ApiReturn('templates/question', qx, res)
def POST(self, pid):
qx = util.select_one('qlist', where='id=$id', vars={'id': pid})
if qx is None:
raise status.ApiError('401 Invalid Question')
sess = util.get_sess()
data = web.data()
util.insert('alist', ans=data, up=0, down=0, qid=qx.id)
raise status.ApiError('200 OK')
class Register(object):
def GET(self):
raise status.ApiReturn('templates/register')
def POST(self):
var = web.input()
if 'fb' in var:
raise status.ApiError('200 OK')
try:
if var.pword != var.repword:
raise status.ApiError('403 Field Mismatch')
hpword = bcrypt.hashpw(var.pword, bcrypt.gensalt())
res = util.insert('users', email=var.email, pword=hpword, name=var.name)
sess = str(uuid.uuid4())[:64]
values = {
'sess': sess,
'uid': res
}
util.insert('sessions', **values)
web.setcookie('wsid_login', sess, expires=86400, path='/')
except KeyError as err:
raise status.ApiError('401 Unauthorized')
raise status.ApiError('200 OK')
class Logout(object):
def POST(self):
try:
sess_key = web.cookies().wsid_login
sess = util.select_one('sessions', where='sess=$s', vars={'s': sess_key})
web.setcookie('wsid_login',sess_key,expires=-1)
print "del", util.delete('sessions', where='sess=$s', vars={'s': sess_key})
except AttributeError:
raise status.ApiError('401 Not logged in')
except KeyError:
raise status.ApiError('401 Invalid session')
raise status.ApiError('200 OK')
class Login(object):
def GET(self):
xsrf = str(uuid.uuid1())
util.insert('xsrf', token=xsrf)
raise status.ApiReturn('templates/login', xsrf)
def user_auth(self, email, pword):
user = util.select_one('users', where='email=$e', vars={'e':email})
if user is None:
return None
hashed = user.pword
if bcrypt.hashpw(pword, hashed) == hashed:
return user
return None
def POST(self):
var = web.input()
if 'fb' in var:
xsrf = util.select_one('xsrf', where='token=$tok', vars={'tok': var.xsrf})
if xsrf is None:
raise status.ApiError('401 Unauthorized')
try:
xsrf = util.select_one('xsrf', where='token=$tok', vars={'tok': var.xsrf})
if xsrf is None:
raise status.ApiError('401 Unauthorized')
user = self.user_auth(var.email, var.pword)
if user is None:
print "this one"
raise status.ApiError('401 Unauthorized')
sess = str(uuid.uuid4())[:64]
values = {
'sess': sess,
'uid': user['id']
}
util.insert('sessions', **values)
web.setcookie('wsid_login', sess, path='/')
except AttributeError as err:
print "that one"
raise status.ApiError('401 Unauthorized (%s)' % err)
web.redirect('/')
#raise status.ApiError('200 OK')
if __name__ == "__main__":
app = web.application(urls, globals())
app.internalerror = web.debugerror
app.run()