-
Notifications
You must be signed in to change notification settings - Fork 2
/
users.py
336 lines (303 loc) · 11.9 KB
/
users.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
import pymysql.cursors
import datetime
import traceback
import json
import ConfigParser
from posts import getRange
config = ConfigParser.ConfigParser()
config.read('db.cfg')
connection = pymysql.connect(host=config.get('database','host'),
user=config.get('database','username'),
password=config.get('database','password'),
db = config.get('database','db'),
charset = 'utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
#Method to follow a user
def follow(data):
currentUserId = data["UserId"]
followUserId = data["Follow"]
count = -1
try:
with connection.cursor() as cursor:
sql = "SELECT COUNT(*) FROM `FollowDetails` WHERE `UserId` = %s AND `FollowingUserId` = %s"
cursor.execute(sql, (currentUserId, followUserId))
count = cursor.fetchone()
if count["COUNT(*)"] > 0:
#Update the row
with connection.cursor() as cursor:
sql = "UPDATE `FollowDetails` SET `isDeleted` = %s WHERE `UserId` = %s AND `FollowingUserId` = %s"
cursor.execute(sql, (0, currentUserId, followUserId))
else:
with connection.cursor() as cursor:
sql = "INSERT INTO `FollowDetails` (`UserId`, `FollowingUserId`, `IsDeleted`) VALUES (%s, %s, %s)"
cursor.execute(sql, (currentUserId, followUserId, 0))
connection.commit()
return 1;
except Exception, e:
print traceback.print_exc()
return -1
#Method to verify user - password
def checkPassword(data):
username = data["username"]
password = data["password"]
try:
#Fetch password of user
with connection.cursor() as cursor:
sql = "SELECT `Password` from `Users` where `DisplayName` = %s"
cursor.execute(sql, (username))
results = cursor.fetchone();
print password
print str(results[u'Password'])
if(str(results[u'Password']) == password):
print "success"
return 1
connection.commit()
return 0
except Exception, e:
print traceback.print_exc()
return -1
#Method to create a new user
def createUser(data):
print "Function called - createUser"
username = data["username"]
password = data["password"]
age = data["age"]
about = data["about"]
date = "2016-04-20 19:17:00"
try:
#Create a new user
with connection.cursor() as cursor:
sql = """INSERT INTO `Users` (`Reputation`, `CreationDate`, `DisplayName`, `LastAccessDate`, `Location`, `AboutMe`, `Views`, `UpVotes`, `DownVotes`, `AccountId`, `Age`, `Password`) VALUES(NULL,%s,%s,NULL,NULL,%s,NULL,NULL,NULL,NULL,%s,%s)""";
cursor.execute(sql, (date, username, about, age, password))
connection.commit()
return 1
except Exception, e:
print traceback.print_exc()
return -1
#Method to get user id
def getUserId(username):
try:
#Get user id
with connection.cursor() as cursor:
sql = "SELECT `Id` from `Users` where `DisplayName` = %s"
count = cursor.execute(sql, (username))
result = cursor.fetchone()
if count > 0:
return result[u'Id']
return -1
except Exception, e:
print traceback.print_exc()
return -1
#Method to get user id
def getUserName(userId):
try:
#Get user id
with connection.cursor() as cursor:
sql = "SELECT `DisplayName` from `Users` where `Id` = %s"
count = cursor.execute(sql, (userId))
result = cursor.fetchone()
if count > 0:
return result[u'DisplayName']
return -1
except Exception, e:
print traceback.print_exc()
return -1
#Unfollow method
def unFollow(data):
currentUserId = data["UserId"]
followUserId = data["Follow"]
count = -1
try:
with connection.cursor() as cursor:
sql = "SELECT COUNT(*) FROM `FollowDetails` WHERE `UserId` = %s AND `FollowingUserId` = %s"
cursor.execute(sql, (currentUserId, followUserId))
count = cursor.fetchone()
count = count["COUNT(*)"]
if count > 0:
#unfollow by updating the row
with connection.cursor() as cursor:
sql = "UPDATE `FollowDetails` SET `isDeleted` = %s WHERE `UserId` = %s AND `FollowingUserId` = %s"
cursor.execute(sql, (1, currentUserId, followUserId))
else:
return -1;
connection.commit()
return 1
except Exception, e:
print traceback.print_exc()
return -1
#get user details
def getUserDetails(data):
userId = data
returnData = {}
userData = {}
try:
with connection.cursor() as cursor:
sql = "SELECT `Id`, `DisplayName`, `AboutMe`, `CreationDate` FROM `Users` WHERE `Id` = %s"
rowCount = cursor.execute(sql, (userId))
if rowCount > 0:
result = cursor.fetchone()
DisplayName = result["DisplayName"]
AboutMe = result["AboutMe"]
CreationDate = result["CreationDate"]
UserId = result["Id"]
returnData = {
"UserId" : UserId,
"DisplayName" : DisplayName,
"AboutMe" : AboutMe,
"CreationDate" : str(CreationDate)
}
return returnData
except Exception, e:
print traceback.print_exc()
return -1
def getAllDetailsOfUser(data):
userId = data
returnData = {}
userData = {}
followers = []
following = []
try:
userData = getUserDetails(userId)
print(userId)
if userData == -1:
return -1
with connection.cursor() as cursor:
sql = "SELECT `FollowingUserId` FROM `FollowDetails` WHERE `UserId` = %s AND `isDeleted` = 0"
followingCount = cursor.execute(sql, (userId))
print(followingCount)
if followingCount > 0:
res = cursor.fetchall()
for row in res:
followId = row[u'FollowingUserId']
temp = getUserDetails(followId)
followers.append(temp)
sql = "SELECT `UserId` FROM `FollowDetails` WHERE `FollowingUserId` = %s AND `isDeleted` = 0"
followerCount = cursor.execute(sql, (userId))
print(followerCount)
if followerCount > 0:
res = cursor.fetchall()
for row in res:
followerID = row[u'UserId']
temp = getUserDetails(followerID)
following.append(temp)
returnData = {
"UserData" : userData,
"Followers" : followers,
"Following" : following
}
return returnData
except Exception, e:
print traceback.print_exc()
return -1
def isActive(userId):
returnVal = -1
postcount = 0
commentcount = 0
try:
with connection.cursor() as cursor:
sql = "SELECT COUNT(*) AS COUNT FROM Posts WHERE OwnerUserId = %s"
cursor.execute(sql, (userId))
postcount = cursor.fetchone()
postcount = postcount["COUNT"]
with connection.cursor() as cursor:
sql = "SELECT COUNT(*) AS COUNT FROM Comments WHERE UserId = %s"
cursor.execute(sql, (userId))
commentcount = cursor.fetchone()
commentcount = commentcount["COUNT"]
connection.commit()
if postcount >= 10 or commentcount >= 30:
returnVal = 1
else:
returnVal = 0
return returnVal
except Exception, e:
print traceback.print_exc()
return -1
def getMyQuestions(userId):
#pageNum = (int(page)-1) * 10;
data = {}
try:
with connection.cursor() as cursor:
sql = "SELECT P.Id, P.Title, P.ViewCount, P.OwnerUserId, P.OwnerDisplayName, P.FavouriteCount, P.Tags, \
P.AnswerCount, P.CreationDate, IFNULL(P.Usefulness,0) AS Usefulness from Posts as P where P.PostTypeId = 1 and P.OwnerUserId = %s"
rowCount = cursor.execute(sql, (userId))
if rowCount > 0:
results = cursor.fetchall()
#print(results)
usefulnessCounts = []
viewCounts = []
for row in results:
viewCounts.append(int(row[u'ViewCount']))
usefulnessCounts.append(int(row[u'Usefulness']))
viewCounts.sort()
usefulnessCounts.sort()
splitAt = rowCount / 3
v1 = viewCounts[:splitAt]
v2 = viewCounts[splitAt:splitAt*2]
v3 = viewCounts[splitAt*2:]
u1 = usefulnessCounts[:splitAt]
u2 = usefulnessCounts[splitAt:splitAt*2]
u3 = usefulnessCounts[splitAt*2:]
for row in results:
id = row[u'Id']
sqlVup = "SELECT count(Id) as count from Votes where VoteTypeId = 2 and PostId = %s"
sqlVdown = "SELECT count(Id) as count from Votes where VoteTypeId = 3 and PostId = %s"
upCount = cursor.execute(sqlVup, (id))
up = cursor.fetchone()
downCount = cursor.execute(sqlVdown, (id))
down = cursor.fetchone()
row[u'CreationDate'] = str(row[u'CreationDate'])
row[u'UpVotes'] = up[u'count']
row[u'DownVotes'] = down[u'count']
row[u'ViewCountRank'] = getRange(v1, v2, v3, row[u'ViewCount'])
row[u'UsefulnessRank'] = getRange(u1, u2, u3, row[u'Usefulness'])
data = results
return data
except Exception, e:
print traceback.print_exc()
return -1
def getMyAnswerQuestions(userId):
#pageNum = (int(page)-1) * 10;
data = {}
try:
with connection.cursor() as cursor:
sql = "SELECT P.Id, P.Title, P.ViewCount, P.OwnerUserId, P.OwnerDisplayName, P.FavouriteCount, P.Tags, \
P.AnswerCount, P.CreationDate, IFNULL(P.Usefulness,0) AS Usefulness from Posts as P where P.PostTypeId = 1 \
AND P.Id IN (SELECT Ps.ParentId FROM Posts as Ps WHERE Ps.PostTypeId = 2 AND Ps.OwnerUserId = %s)"
rowCount = cursor.execute(sql, (userId))
if rowCount > 0:
results = cursor.fetchall()
#print(results)
usefulnessCounts = []
viewCounts = []
for row in results:
viewCounts.append(int(row[u'ViewCount']))
usefulnessCounts.append(int(row[u'Usefulness']))
viewCounts.sort()
usefulnessCounts.sort()
splitAt = rowCount / 3
v1 = viewCounts[:splitAt]
v2 = viewCounts[splitAt:splitAt*2]
v3 = viewCounts[splitAt*2:]
u1 = usefulnessCounts[:splitAt]
u2 = usefulnessCounts[splitAt:splitAt*2]
u3 = usefulnessCounts[splitAt*2:]
for row in results:
id = row[u'Id']
sqlVup = "SELECT count(Id) as count from Votes where VoteTypeId = 2 and PostId = %s"
sqlVdown = "SELECT count(Id) as count from Votes where VoteTypeId = 3 and PostId = %s"
upCount = cursor.execute(sqlVup, (id))
up = cursor.fetchone()
downCount = cursor.execute(sqlVdown, (id))
down = cursor.fetchone()
row[u'CreationDate'] = str(row[u'CreationDate'])
row[u'UpVotes'] = up[u'count']
row[u'DownVotes'] = down[u'count']
row[u'ViewCountRank'] = getRange(v1, v2, v3, row[u'ViewCount'])
row[u'UsefulnessRank'] = getRange(u1, u2, u3, row[u'Usefulness'])
data = results
return data
except Exception, e:
print traceback.print_exc()
return -1
#print(getMyAnswerQuestions(2526083))