-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
384 lines (346 loc) · 12.3 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import os.path
import mysql.connector
import os
from binascii import hexlify
import tornado.web
from tornado.options import define, options
from datetime import datetime
define("port", default=1104, help="run on the given port", type=int)
class Application(tornado.web.Application):
def __init__(self):
handlers = [
# GET METHOD :
(r"/getticketmod", getticketmod),
(r"/getticketcli", getticketcli),
# POST METHOD :
(r"/signup", signup),
(r"/login",login),
(r"/logout", logout),
(r"/sendticket", sendticket),
(r"/restoticketmod", restoticketmod),
(r"/changestatus", changestatus),
(r"/closeticket", closeticket),
]
settings = dict()
super(Application, self).__init__(handlers, **settings)
self.db = mysql.connector.connect(
host="localhost",
user="root",
passwd="bahar1234",
database="ticket",
auth_plugin = "mysql_native_password"
)
class BaseHandler(tornado.web.RequestHandler):
@property
def db(self):
return self.application.db
def check_user(self,user):
print(user)
temp = self.db.cursor()
temp.execute("SELECT * FROM new_table WHERE username = '{}'".format(user))
resuser = temp.fetchall()
if resuser:
print("true :]")
return True
else :
print("false!")
return False
def check_api(self, api):
temp = self.db.cursor()
temp.execute("SELECT * FROM new_table WHERE token = '{}'".format(api))
resuser = temp.fetchall()
if resuser:
print(resuser)
temp.execute("UPDATE new_table SET token = %s WHERE username = %s ", ('', resuser[0][0]))
self.db.commit()
return True
else:
return False
def check_auth(self, username, password):
temp = self.db.cursor()
temp.execute("SELECT * FROM new_table WHERE username = '{}' and password = '{}'".format(username, password))
resuser = temp.fetchall()
if resuser:
api_token = hexlify(os.urandom(16)).decode('utf-8')
print(api_token)
temp.execute("UPDATE new_table SET token = %s WHERE username = %s ", (api_token, username))
self.db.commit()
return api_token
else:
return False
def getUserName(self, token):
temp = self.db.cursor()
temp.execute("SELECT * FROM new_table WHERE token = '{}'".format(token))
resuser = temp.fetchall()
if resuser:
return resuser[0]
else:
return False
# signup
class signup(BaseHandler):
def post(self, *args, **kwargs):
username = self.get_argument('username')
passWord = self.get_argument('password')
if not self.check_user(username):
print(username, passWord)
temp = self.db.cursor()
temp.execute("INSERT INTO new_table (username, password, token) VALUES (%s, %s ,%s)",(username,passWord,''))
self.db.commit()
output = {
'message' : 'Signed Up :]',
'code': '200'
}
self.write(output)
else:
output = {
'message': 'This username already exist!',
'code': '400'
}
self.write(output)
# login
class login(BaseHandler):
def post(self, *args, **kwargs):
username = self.get_argument('username')
passWord = self.get_argument('password')
token = self.check_auth(username, passWord)
print(token)
if token:
print(username, passWord)
output = {
'message': 'Logged in :]',
'code': '200',
'token': token
}
self.write(output)
else:
output = {
'message': 'your password or username is wrong :/',
'code': '400'
}
self.write(output)
# logout
class logout(BaseHandler):
def post(self):
token = self.get_argument('token')
if self.check_api(token):
output = {
'message': 'Logged Out',
'code': '200'
}
self.write(output)
else:
output = {
'message': 'Wrong Token!',
'code': '400'
}
self.write(output)
# sendTicket
class sendticket(BaseHandler):
def post(self):
token = self.get_argument('token')
text = self.get_argument('text')
subject = self.get_argument('subject')
user = self.getUserName(token)
time = datetime.now()
date = time.strftime('%Y-%m-%d %H:%M:%S')
if user:
temp = self.db.cursor()
temp.execute("INSERT INTO new_table2 (username, body, status, subject, date) VALUES (%s, %s ,%s ,%s, %s)", (user[0], text, 'open', subject,date))
self.db.commit()
output = {
'message': 'Ticket is Sent :]',
'code': '200'
}
self.write(output)
else:
output = {
'message': 'Wrong Token!',
'code': '400'
}
self.write(output)
# restoticketmod
# for admin
class restoticketmod(BaseHandler):
def post(self):
token = self.get_argument('token')
text = self.get_argument('text')
commentId = self.get_argument('commentId')
user = self.getUserName(token)
if user:
if user[3] is 1:
temp = self.db.cursor()
temp.execute("UPDATE new_table2 SET answer = %s ,status = %s WHERE id = %s ", (text, 'closed', int(commentId)))
self.db.commit()
output = {
'message': 'Response to Ticket With id -{}- Sent Successfully'.format(commentId),
'code': '200'
}
else:
output = {
'message': 'you are not admin',
'code': '400'
}
self.write(output)
else:
output = {
'message': 'Wrong Token',
'code': '400'
}
self.write(output)
# change status
#for admin
class changestatus(BaseHandler):
def post(self):
token = self.get_argument('token')
commentId = self.get_argument('commentId')
status = self.get_argument('status')
user = self.getUserName(token)
if user:
if user[3] is 1:
temp = self.db.cursor()
temp.execute("UPDATE new_table2 SET status = %s WHERE id = %s ", (status, int(commentId)))
self.db.commit()
output = {
'message': 'Status Ticket With id -{}- Changed Successfully'.format(commentId),
'code': '200'
}
else:
output = {
'message': 'you are not admin',
'code': '200'
}
self.write(output)
else:
output = {
'message': 'Wrong Token',
'code': '400'
}
self.write(output)
# closeTicket
# for customer
class closeticket(BaseHandler):
def post(self):
token = self.get_argument('token')
commentId = self.get_argument('commentId')
user = self.getUserName(token)
if user:
flag = False
temp = self.db.cursor()
temp.execute("SELECT * FROM new_table2 WHERE username = '{}'".format(user[0]))
resuser = temp.fetchall()
for index in resuser:
if index[0] is int(commentId):
flag = True
temp = self.db.cursor()
temp.execute("UPDATE new_table2 SET status = %s WHERE id = %s ", ('closed', int(commentId)))
self.db.commit()
output = {
'message': 'Ticket With id -{} Closed Successfully'.format(commentId),
'code': '200'
}
self.write(output)
if flag is False:
output = {
'message': 'Id is wrong :/',
'code': '400'
}
self.write(output)
else:
output = {
'message': 'Wrong Token',
'code': '400'
}
self.write(output)
# getTicket
class getticketcli(BaseHandler):
def get(self):
token = self.get_argument('token')
status = self.get_argument('status')
print(type(status))
user = self.getUserName(token)
if user:
temp = self.db.cursor()
if user[3] is 0:
temp.execute("SELECT * FROM new_table2 WHERE username = '{}'".format(user[0]))
resuser = temp.fetchall()
print(resuser)
output = {
'code': '200'
}
c = 0
for index in resuser:
tempout = {}
if index[2] == status:
tempout["subject"] = index[5]
tempout["id"] = index[0]
tempout["status"] = index[2]
tempout["body"] = index[3]
tempout["answer"] = index[4]
tempout["date"] = index[6]
#tempout["date"] = '2019-04-21 11:54:04'
output['block {}'.format(c)] = tempout
c = c + 1
output["tickets"] = 'There Are -{}- Ticket'.format(c)
self.write(output)
else:
output = {
'message': 'you are admin user',
'code': '400'
}
self.write(output)
else:
output = {
'message': 'Wrong Token',
'code': '400'
}
self.write(output)
# getTickeMod
class getticketmod(BaseHandler):
def get(self):
token = self.get_argument('token')
status = self.get_argument('status')
print(type(status))
user = self.getUserName(token)
if user:
temp = self.db.cursor()
if user[3] is 0:
output = {
'message': 'you are normal user',
'code': '400'
}
self.write(output)
else:
temp.execute("SELECT * FROM new_table2")
resuser = temp.fetchall()
print(resuser)
output = {
'code': '200'
}
c = 0
for index in resuser:
tempout={}
if index[2] == status:
tempout["subject"] = index[5]
tempout["id"] = index[0]
tempout["status"] = index[2]
tempout["body"] = index[3]
tempout["answer"] = index[4]
tempout["date"] = str(index[6])
print(type(index[6]))
output['block {}'.format(c)] = tempout
c = c + 1
output["tickets"] = 'There Are -{}- Ticket'.format(c)
self.write(output)
else:
output = {
'message': 'Wrong Token',
'code': '400'
}
self.write(output)
def main():
tornado.options.parse_command_line()
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(options.port)
tornado.ioloop.IOLoop.current().start()
if __name__ == "__main__":
main()