-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.py
277 lines (244 loc) · 10.6 KB
/
server.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import tornado.ioloop
import tornado.web
import tornado.options
import tornado.httpserver
from tornado.options import define, options, parse_command_line
import os.path
import bcrypt
from PIL import Image
from mysql import mysql
from mysql import DB_Exception
from server_api import *
from display_api import *
import json
import config.settings
import httplib2
from apiclient import discovery
from pprint import pprint
import datetime
from display_object import *
define('port',default=3000,help='run the server on the given port',type=int)
#define('log_file_prefix',default='server.log',help='log file name',type=str)
class BaseHandler(tornado.web.RequestHandler):
def get_current_user(self):
return self.get_secure_cookie("user")
class MainHandler(BaseHandler):
def get(self):
user = self.get_current_user()
if user:
imgs = display_image(user.decode("utf-8"))
texts = display_text(user.decode("utf-8"))
data_types = display_data_types()
self.render("index.html",user = user,imgs=imgs,data_types=data_types,texts=texts)
else:
self.redirect("/signin")
class DisplayHandler(BaseHandler):
def get(self):
user = self.get_current_user().decode('utf-8')
data_type = self.get_argument('type')
page = self.get_argument('page')
if data_type == 'image':
data = display_image(user)
elif data_type == 'text':
data = display_text(user)
data_types = display_data_types()
transform_date_string(data)
convert_img_file_path(self,data,data_types)
self.write(dict(type=data_type,data=data,data_types=data_types,page=page))
class SignupHandler(BaseHandler):
def get(self):
if self.get_current_user().decode('utf-8') == 'admin':
self.render('signup.html',flash=None)
else:
self.redirect('/')
def post(self):
userArgUtil = UserArgumentsUtil(self)
user_info = userArgUtil.getArguments()
ret = check_user_existed_or_signup(user_info)
if 'error' in ret:
self.render('signup.html',flash=ret['error'])
else:
self.render('signup.html',flash=ret['flash'])
class LoginHandler(BaseHandler):
def get(self):
incorrect = self.get_secure_cookie("incorrect")
if incorrect and int(incorrect) >= 20:
self.write('<center>Blocked</center>')
return
self.render('signin.html',flash=None)
def post(self):
incorrect = self.get_secure_cookie("incorrect")
if incorrect and int(incorrect) >= 20:
self.write('<center>Blocked</center>')
return
userArgUtil = UserArgumentsUtil(self)
user_info = userArgUtil.getArguments()
ret = check_user_password(user_info)
if 'success' in ret:
self.set_secure_cookie("user",self.get_argument("username"))
self.set_secure_cookie("incorrect","0")
self.redirect(self.reverse_url("main"))
elif 'fail' in ret:
incorrect = self.get_secure_cookie("incorrect") or 0
increased = str(int(incorrect)+1)
self.set_secure_cookie("incorrect",increased)
self.render('signin.html',flash=[ret['fail'],"You can still try"+str(20-int(increased))+" times"])
class ChangePasswdHandler(BaseHandler):
def get(self):
self.render("changepasswd.html",flash=None)
def post(self):
userEditArgUtil = UserEditArgumentsUtil(self)
send_msg = userEditArgUtil.getArguments()
receive_msg = change_password(send_msg)
if receive_msg['result'] == "success":
flash = "Change password success"
else:
flash = "Change password failed "+receive_msg['error']
self.render("changepasswd.html",flash=flash)
class LogoutHandler(BaseHandler):
def get(self):
self.clear_cookie("user")
self.redirect(self.get_argument("next",self.reverse_url("main")))
class UploadHandler(BaseHandler):
def get(self):
user = self.get_current_user()
data_types = display_data_types()
if user:
self.render("upload.html",flash=None,data_types=data_types)
else:
self.redirect("/signin")
def post(self):
receive_msg = {}
upload_path = os.path.join(os.path.dirname(__file__),"static","img")
thumbnail_filepath = os.path.join(os.path.dirname(__file__),"static","thumbnail")
display_object = get_upload_meta_data(self)
if self.get_argument('type') == 'image':
try:
display_object.__class__ = DisplayImage
file_metas=self.request.files['file']
for meta in file_metas:
filepath = os.path.join(upload_path,meta['filename'])
display_object.filepath = filepath
store_image(filepath,meta['body'])
receive_msg = upload_image_insert_db(display_image=display_object)
filepath = receive_msg["img_system_filepath"]
thumbnail_filepath=os.path.join(thumbnail_filepath,receive_msg["img_thumbnail_name"])
store_thumbnail_image(filepath,thumbnail_filepath)
data_types = display_data_types()
self.render("upload.html",flash="Upload finish!",data_types=data_types)
except:
self.redirect("/upload")
else:
display_object.__class__ = DisplayText
receive_msg = upload_text_insert_db(display_text=display_object)
text_file = get_upload_text_data(self)
if text_file['result'] == 'success':
text_file.pop('result')
with open(receive_msg["text_system_dir"],"w") as fp:
print(json.dumps(text_file),file=fp)
data_types = display_data_types()
self.render("upload.html",flash="Upload finish!",data_types=data_types)
class EditHandler(BaseHandler):
def get(self):
img = None
text = None
text_content = None
data_types = display_data_types()
img_id = self.get_argument("img_id",default=None)
if img_id:
img = get_img_meta(img_id)
else:
text_id = self.get_argument("text_id")
text_content = read_text_data(text_id)
text = get_text_meta(text_id)
self.render("edit.html",img=img,text=text,data_types=data_types,flash=None,text_content=text_content)
def post(self):
img = None
text = None
text_content = None
receive_msg = {}
display_object = get_upload_meta_data(self)
if self.get_argument("type") == "image":
display_object.__class__ = DisplayImage
display_object.id = tornado.escape.xhtml_escape(self.get_argument("img_id"))
receive_msg = edit_image_data(display_image=display_object)
if receive_msg["result"] == "success":
flash = "Edit "+self.get_argument("img_id")+" successed "
else:
flash = "Edit "+self.get_argument("img_id")+" failed "
img = get_img_meta(self.get_argument("img_id"))
else:
display_object.__class__ = DisplayText
display_object.id = tornado.escape.xhtml_escape(self.get_argument("text_id"))
display_object.invisible_title = display_object.id
display_object.text_file = get_upload_text_data(self)
receive_msg = edit_text_data(display_text=display_object)
if receive_msg["result"] == "success":
flash = "Edit "+self.get_argument("text_id")+" successed "
else:
flash = "Edit "+self.get_argument("text_id")+" failed "
text = get_text_meta(self.get_argument("text_id"))
text_content = read_text_data(display_object.id)
data_types = display_data_types()
self.render("edit.html",img=img,text=text,data_types=data_types,flash=flash,text_content=text_content)
class DeleteHandler(BaseHandler):
def get(self):
send_msg = {}
user_name = self.get_current_user().decode("utf-8")
send_msg["server_dir"] = os.path.dirname(__file__)
send_msg["target_id"] = tornado.escape.xhtml_escape(self.get_argument("target_id"))
send_msg["user_id"] = get_user_id(user_name)
receive_msg = delete_image_or_text_data(send_msg)
if receive_msg["result"] == "success":
flash = "delete "+send_msg["target_id"]+" success!"
else:
flash = receive_msg["error"]
self.redirect("/")
class addTypeHandler(BaseHandler):
def get(self):
self.render("add_type.html",flash=None)
def post(self):
send_msg = {}
send_msg["type_name"] = tornado.escape.xhtml_escape(self.get_argument('type_name'))
receive_msg = add_new_data_type(send_msg)
if receive_msg["result"] == "success":
flash = 'ADD TYPE SUCCESS!'
else:
flash = 'ADD TYPE FAILED! '+receive_msg["error"]
self.render('add_type.html',flash=flash)
class googleApiHandler(BaseHandler):
def get(self):
if self.get_argument('code',default=None):
code = self.get_argument('code')
credentials = exchange_code_and_store_credentials(code)
else:
credentials = get_credentials(self)
self.redirect('/')
def main():
parse_command_line()
base_dir = os.path.dirname(__file__)
settings = {
"cookie_secret": config.settings.server['cookie_secret'],
"template_path":os.path.join(base_dir,"template"),
"static_path":os.path.join(base_dir,"static"),
"debug":True,
"autoreload":True
}
application = tornado.web.Application([
tornado.web.url(r"/",MainHandler,name="main"),
tornado.web.url(r"/signin",LoginHandler,name="signin"),
tornado.web.url(r"/signup",SignupHandler,name="signup"),
tornado.web.url(r"/changepw",ChangePasswdHandler,name="changepw"),
tornado.web.url(r"/signout",LogoutHandler,name="signout"),
tornado.web.url(r"/upload",UploadHandler,name="upload"),
tornado.web.url(r"/edit",EditHandler,name="edit"),
tornado.web.url(r"/delete",DeleteHandler,name="delete"),
tornado.web.url(r"/addType",addTypeHandler,name="addType"),
tornado.web.url(r"/googleapi",googleApiHandler,name="googleapi"),
tornado.web.url(r"/display",DisplayHandler,name="display"),
],**settings)
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()