-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhack.py
More file actions
211 lines (199 loc) · 6.53 KB
/
hack.py
File metadata and controls
211 lines (199 loc) · 6.53 KB
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
# filename: happy_birthday.py
"""A basic (single function) API written using hug"""
import hug
import connectDb
import uuid
from falcon import HTTP_400
import time
@hug.post('/happy_birthday')
def happy_birthday(name, age:hug.types.number=1):
"""Says happy birthday to a user"""
return "Happy {age} Birthday {name}!".format(**locals())
VERSION = 1
@hug.post('/event/add', versions=VERSION)
def addEvent(startTime, userId, eventName, location, period, flag:hug.types.number=0):
period = period / 1000
if startTime == 0:
startTime = int(time.time())
else:
startTime = int(startTime / 1000)
print ('Uuuuuuu', startTime)
print ('Uuuuuuu', type(startTime))
try:
cnx = connectDb.getConn()
with cnx.cursor() as cursor:
sql = 'INSERT INTO TB_EVENT (eventId, eventName, eventStarttime, eventLocation, eventPeriod, eventFlag, userId ) VALUES(\'{0}\', \'{1}\', \'{2}\', \'{3}\', \'{4}\', \'{5}\', \'{6}\')'.format(str(uuid.uuid4()), eventName, startTime, location, period, flag, userId)
print (sql)
cursor.execute(sql)
resResult = "success"
cnx.commit()
except Exception as err:
resResult = "error occurred"
print (err)
finally:
cnx.close()
return resResult
@hug.post('/event/delete', versions=VERSION)
def deleteEvent(userId, eventId, response=None):
try:
cnx = connectDb.getConn()
with cnx.cursor() as cursor:
sqlSelect = 'SELECT * FROM TB_EVENT WHERE eventId=\'{0}\''.format(eventId)
cursor.execute(sqlSelect)
resResult = cursor.fetchall()
if resResult:
sqlDelete = 'DELETE FROM TB_EVENT WHERE eventId=\'{0}\''.format(eventId)
cursor.execute(sqlDelete)
result = "delete success"
cnx.commit()
else :
response.status = HTTP_400
result = "event not found"
except Exception as err:
result = "error occurred"
print (err)
finally:
cnx.close()
return result
@hug.get('/event/query', versions=VERSION)
def queryEvent(userId, eventId="DEFAULT"):
"""
return
{
eventList: [
{
eventId: '',
eventName: '',
eventStarttime: '',
eventLocation: '',
eventPeriod: '',
eventFlag: '0 1 2'
},
{
},
]
}
"""
try:
cnx = connectDb.getConn()
with cnx.cursor() as cursor:
if eventId == "DEFAULT":
sql = 'SELECT * FROM TB_EVENT WHERE userId=\'{0}\''.format(userId)
else :
sql = 'SELECT * FROM TB_EVENT WHERE eventId=\'{0}\' AND userId=\'{1}\''.format(eventId, userId)
cursor.execute(sql)
resResult = cursor.fetchall()
# print (resResult)
cnx.commit()
except Exception as err:
resResult = "error occurred"
print (err)
finally:
print (cnx)
cnx.close()
# print ('returning ..... >>>', resResult)
return resResult
@hug.post('/event/modify', versions=VERSION)
def modifyEvent(userId, eventId, eventFlag):
try:
cnx = connectDb.getConn()
with cnx.cursor() as cursor:
sql = 'UPDATE TB_EVENT SET eventFlag=\'{0}\' WHERE eventId=\'{1}\' AND userId=\'{2}\'\
'.format(eventFlag, eventId, userId)
cursor.execute(sql)
resResult = cursor.fetchall()
if eventFlag == 1:
sqlend = 'UPDATE TB_EVENT SET eventEndTime=\'{0}\' WHERE eventId=\'{1}\' AND userId=\'{2}\''.format(int(time.time()), eventId, userId)
print (sqlend)
cursor.execute(sqlend)
print ('result............' ,resResult)
cnx.commit()
except Exception as err:
resResult = "error occurred"
print (err)
finally:
cnx.close()
return resResult
@hug.post('/user/login', versions=VERSION)
def loginUser(userId, userPassword, response=None):
print (userId)
print (userPassword)
try:
cnx = connectDb.getConn()
with cnx.cursor() as cursor:
sql = 'SELECT userPassword FROM TB_USER WHERE userId =\'{0}\''.format(userId)
print (sql)
cursor.execute(sql)
resResult = cursor.fetchall()
print (resResult)
if (resResult[0]['userPassword'] == userPassword):
print (111111111)
result = 'success'
else:
response.status = HTTP_400
print (2222222222)
result = 'auth error'
cnx.commit()
except Exception as err:
result = "auth error"
response.status = HTTP_400
print (err)
finally:
cnx.close()
return result
@hug.post('/user/register', versions=VERSION)
def registerUser(userId, userPassword):
print (userId)
print (type (userId))
print (userPassword)
print (type (userPassword))
try:
cnx = connectDb.getConn()
with cnx.cursor() as cursor:
sql = 'INSERT INTO TB_USER VALUES(\'{0}\', \'{1}\')'.format(userId, userPassword)
print (sql)
cursor.execute(sql)
resResult = "success"
cnx.commit()
except Exception as err:
resResult = "error occurred"
print (err)
finally:
cnx.close()
return resResult
@hug.post('/location/post', versions=VERSION)
def postLocation(location):
print (location)
# try:
cnx = connectDb.getConn()
with cnx.cursor() as cursor:
sql = 'INSERT INTO TB_LOCATION VALUES(\'{0}\')'.format(location)
print (sql)
cursor.execute(sql)
resResult = "success"
cnx.commit()
cnx.close()
# except Exception as err:
# resResult = "error occurred"
# response.status = HTTP_400
# print (err)
# finally:
# cnx.close()
# return resResult
@hug.get('/location/get', versions=VERSION)
def postLocation():
try:
cnx = connectDb.getConn()
with cnx.cursor() as cursor:
sql = 'SELECT * FROM TB_LOCATION'
print (sql)
cursor.execute(sql)
resResult = cursor.fetchall()
cnx.commit()
except Exception as err:
resResult = "error occurred"
response.status = HTTP_400
print (err)
finally:
cnx.close()
return resResult