-
Notifications
You must be signed in to change notification settings - Fork 4
/
blog.py
217 lines (146 loc) · 4.8 KB
/
blog.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/python
import bottle, redis, time, hashlib
from bottle import route, run, request, response, template, view, static_file
r = redis.Redis(host='localhost', port=6379, db=0)
ERROR_NOT_LOGGED_IN = 0
ERROR_NOT_AUTHORIZED = 1
PERMISSION_ADMIN = 0
PERMISSION_POST = 1
errors = {ERROR_NOT_LOGGED_IN:"Not logged in.", ERROR_NOT_AUTHORIZED:"Not authorized to view that page."}
@route('/css/:filename')
def send_css(filename):
return static_file(filename, root='./css')
@route("/")
def index():
return getRecentPosts()
@route("/post/:id")
def getPostById(id):
node = {'post':['title', 'body', 'userName', 'datestamp']}
return template('node', globalVars=globalVars, node=node)
@route("/recent")
def getRecentPosts():
recentPosts = r.lrange("posts:recent", 0, 9)
posts = {}
for x in range(0, len(recentPosts)):
posts[x] = getPost(recentPosts[x])
#return template('recent', posts=posts, globalVars=globalVars)
return render('recent', posts)
def render(type, dict=None):
refreshSession()
return template(type, passed=dict, globalVars=globalVars)
@route("/test")
def test():
error(ERROR_NOT_AUTHORIZED)
@route("/post")
def getPostPage():
auth(PERMISSION_POST)
return render('post')
@route("/post", method="POST")
def newPost():
pid = createPost(request.forms.get('title'), time.time(), getCurrentUser(), request.forms.get('body'))
if pid is not False:
url = generateUrl(pid)
return template('message', globalVars=globalVars, message={'success':"Sucessfully created %s"%(url)})
def auth(type):
userName = getCurrentUser()
print userName
if not userName:
error(ERROR_NOT_LOGGED_IN)
permissions = getPermissions(userName)
if str(type) in permissions:
return True
error(ERROR_NOT_AUTHORIZED)
def getPermissions(userName):
return r.smembers("users:%s:permissions"%(userName))
def error(type):
return bottle.redirect("/error/%s"%(type))
def generateUrl(pid):
return r.get('global:siteURL') + "post/%s"%(str(pid))
def createPost(title, datestamp, user, body):
try:
r.incr('global:nextPostId')
pid = r.get('global:nextPostId')
r.set('posts:%s:title'%(pid), title)
r.set('posts:%s:body'%(pid), body)
r.set('posts:%s:datestamp'%(pid), datestamp)
r.set('posts:%s:user'%(pid), handle)
r.lpush('posts:recent', pid)
return pid
except Exception:
print Exception[0]
return False
def refreshSession():
globalVars['loggedInUser'] = getCurrentUser()
@route("/admin")
def admin():
auth(PERMISSION_ADMIN)
userInfo = getUserInfo(getCurrentUser())
return template('admin', globalVars=globalVars, userInfo=userInfo)
@route("/admin/login")
def login():
userName = "max"
getOrCreateSession(userName)
return "Success"
@route("/error/:error")
def errorPage(error):
return errors[int(error)]
def getOrCreateSession(userName):
if getCurrentUser():
print "User %s already logged in."%(getCurrentUser())
return True
m = hashlib.md5()
m.update(userName)
m.update(str(time.time()))
sessionId = m.hexdigest()
print "New session for %s: %s"%(userName, sessionId)
response.set_cookie('sessionId', sessionId, path='/')
r.setex("sessions:%s:userName"%(sessionId), userName, 260000)
return True
def getUserName(sessionId):
return r.get('sessions:%s:userName'%(sessionId))
def getCurrentSessionId():
return request.COOKIES.get('sessionId')
def getCurrentUser():
sessionId = request.COOKIES.get('sessionId')
print sessionId
if sessionId is None:
return None
userName = r.get('sessions:%s:userName'%(sessionId))
return userName
def getUserInfo(userName):
userInfo = {}
userInfo['userName'] = userName
userInfo['realName'] = r.get("users:%s:realName"%(userName))
userInfo['email'] = r.get("users:%s:email"%(userName))
userInfo['submissions'] = r.lrange("users:%s:submissions"%(userName), 0, -1)
userInfo['role'] = r.lrange("users:%s:roles"%(userName), 0, -1)
return userInfo
def getPost(id):
post = {}
post['title'] = r.get("posts:%s:title"%(id))
post['datestamp'] = r.get("posts:%s:datestamp"%(id))
post['user'] = r.get("posts:%s:user"%(id))
post['body'] = r.get("posts:%s:body"%(id))
post['summary'] = r.get("posts:%s:summary"%(id))
post['tagIds'] = r.smembers("posts:%s:tagIds"%(id))
return post
def auths(type, request):
sessionId = request.COOKIES.get('sessionId')
if sessionId is None:
print "No Session ID"
return False
print sessionId
userName = r.get('session:%s:userName'%(sessionId))
print "Username: " + str(userName)
permissions = r.smembers('users:%s:permissions'%(userName))
print permissions
if type in permissions:
print "User %s authorized for %s"%(userName, type)
return True
globalVars = {}
globalVars['siteTitle'] = r.get('global:siteTitle')
globalVars['siteURL'] = r.get('global:siteURL')
globalVars['loggedInUser'] = False
if __name__ == "__main__":
bottle.debug(True)
run( host="localhost", port=8080, reloader=True)