Skip to content

Reuse userId in all queries #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions feedback.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ def thanksReceived(username):
data = cur.fetchall()
return data[0][0]

def featuredImages(username):
def featuredImages(userId):
awards = ['Featured_pictures_on_Wikimedia_Commons', 'Quality_images']
sqlins = []
for award in awards:
sqlins.append('"' + award + '"')
sqlin = ", ".join(sqlins)
with conn.cursor() as cur:
sql = 'select count(cl_from), cl_to from categorylinks where cl_to in (%s) and cl_type="file" and cl_from in (select log_page from logging_userindex where log_type="upload" and log_user=(select user_id from user where user_name="%s")) group by cl_to;' % (sqlin, username)
sql = 'select count(cl_from), cl_to from categorylinks where cl_to in (%s) and cl_type="file" and cl_from in (select log_page from logging_userindex where log_type="upload" and log_user=%d) group by cl_to;' % (sqlin, userId)
cur.execute(sql)
data = cur.fetchall()
response = {}
Expand All @@ -36,23 +36,23 @@ def featuredImages(username):
response[award] = 0
return response

def articlesUsingImages(username):
def articlesUsingImages(userId):
with conn.cursor() as cur:
sql = 'select count(*) from globalimagelinks where gil_to in (select log_title from logging_userindex where log_type="upload" and log_user=(select user_id from user where user_name="%s"));' % username
sql = 'select count(*) from globalimagelinks where gil_to in (select log_title from logging_userindex where log_type="upload" and log_user=%d);' % userId
cur.execute(sql)
data = cur.fetchall()
return data[0][0]

def uniqueUsedImages(username):
def uniqueUsedImages(userId):
with conn.cursor() as cur:
sql = 'select count(distinct gil_to) from globalimagelinks where gil_to in (select log_title from logging_userindex where log_type="upload" and log_user=(select user_id from user where user_name="%s"));' % username
sql = 'select count(distinct gil_to) from globalimagelinks where gil_to in (select log_title from logging_userindex where log_type="upload" and log_user=%d);' % userId
cur.execute(sql)
data = cur.fetchall()
return data[0][0]

def imagesEditedBySomeoneElse(username):
def imagesEditedBySomeoneElse(userId):
with conn.cursor() as cur:
sql = 'select count(*) from revision where rev_page in (select log_page from logging_userindex where log_type="upload" and log_user=(select user_id from user where user_name="%s")) and rev_user!=(select user_id from user where user_name="%s") group by rev_page having count(*)>1' % (username, username)
sql = 'select count(*) from revision where rev_page in (select log_page from logging_userindex where log_type="upload" and log_user=%d) and rev_user!=%d group by rev_page having count(*)>1' % (userId, userId)
cur.execute(sql)
data = cur.fetchall()
return len(data)
Expand All @@ -64,6 +64,13 @@ def deletedUploads(username):
data = cur.fetchall()
return data[0][0]

def getUserId(username):
with conn.cursor() as cur:
sql = 'select user_id from user where user_name="%s";' % username
cur.execute(sql)
data = cur.fetchall()
return data[0][0]

#Print header
print 'Content-type: application/json\n'

Expand Down Expand Up @@ -103,16 +110,19 @@ def deletedUploads(username):
'status': 'ok',
'user': user,
}

userid = getUserId(user)

if 'thanksReceived' in fetch:
response['thanksReceived'] = thanksReceived(user)
if 'featuredImages' in fetch:
response['featuredImages'] = featuredImages(user)
response['featuredImages'] = featuredImages(userid)
if 'articlesUsingImages' in fetch:
response['articlesUsingImages'] = articlesUsingImages(user)
response['articlesUsingImages'] = articlesUsingImages(userid)
if 'uniqueUsedImages' in fetch:
response['uniqueUsedImages'] = uniqueUsedImages(user)
response['uniqueUsedImages'] = uniqueUsedImages(userid)
if 'imagesEditedBySomeoneElse' in fetch:
response['imagesEditedBySomeoneElse'] = imagesEditedBySomeoneElse(user)
response['imagesEditedBySomeoneElse'] = imagesEditedBySomeoneElse(userid)
if 'deletedUploads' in fetch:
response['deletedUploads'] = deletedUploads(user)

Expand Down