-
Notifications
You must be signed in to change notification settings - Fork 0
Microtask 2 #2
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
base: master
Are you sure you want to change the base?
Microtask 2 #2
Changes from 5 commits
d7b2d0e
5ad9407
cc7ea89
8133e9a
6dd4900
ef6bda9
8c861d6
7bbf540
db47853
97357e5
3553348
863165f
2f5e74d
efcde18
a613a35
f5c41a7
8969f0c
fee5636
c9e8057
7659411
66399e5
1581136
8df73a2
ed0cb90
4c4a59f
a7fea46
f3d082f
f166090
0edfcac
355f556
6e607d7
acb8ea9
8bda4b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <!DOCTYPE HTML> | ||
| <html> | ||
| <head> | ||
| <title>Home Page</title> | ||
| </head> | ||
| <body> | ||
| <center> | ||
| <a href="{% url 'Microtask1:main' %}">Microtask1</a> <br> | ||
| <a href="{% url 'Microtask1:main2' %}">Microtask2</a> <br> | ||
| </center> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <title>User Percentile</title> | ||
| </head> | ||
| <body> | ||
| <center> | ||
| <h2>Tool to display user Percentile and Rank in Georgian wiki</h2> | ||
| <p>Enter initial letter of username in capital</p> <br> | ||
| <form method="post"> | ||
| {% csrf_token %} | ||
| Enter username: <input type="text" name="username"> | ||
| <br> | ||
| <br> | ||
| <button type='submit'> Submit </button> | ||
| <a href="https://stats.wikimedia.org/EN/TablesWikipediaKA.htm">Click to see the list of users</a> | ||
| </form> | ||
| </center> | ||
| <br> | ||
| <br> | ||
| <br> | ||
|
|
||
| {% if false_query %} | ||
| <h2>No user found</h2> <br> | ||
| {% endif %} | ||
|
|
||
| {% if name %} | ||
| Name = {{name}} <br> | ||
| Useredits = {{edits}} <br> | ||
| Total_edits = {{total_edits}} <br> | ||
| Rank = {{rank}} <br> | ||
| Total_users = {{total_users}} <br> | ||
| Percentile = {{Percentile}} <br> | ||
| User rating = {{user_rating}} <br> | ||
| {% endif %} | ||
|
|
||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| from django.http import HttpResponse | ||
| from django.shortcuts import render | ||
| import math | ||
| import json | ||
| import requests | ||
| import toolforge | ||
|
|
||
| def main2(request): | ||
| if(request.method=='POST'): | ||
| flag = 1 | ||
| usr = request.POST['username'] | ||
| a = 1 if re.match("^[a-zA-Z0-9_ ]*$", s) else 0 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would leave users with a non-ASCII username stranded. You should accept anything that's a valid MediaWiki username and escape it. Usually in SQL that means using parametrized queries. (Also, where does the
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
| if(!a): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't look like valid Python. (Now that I look at it, all your ifs are in C-style. In Python you can just wrote Also, what's the point of the variable? You can always just inline it.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
| flag=0 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not much point in setting this as you are exiting the function anyway.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
| return(render(request, 'Microtask1/task2.html',{"false_query":1})) | ||
| SqlQuery1 = "SELECT user_editcount FROM user WHERE user_name='" + usr + "';" | ||
| SqlQuery2 = "SELECT ss_total_edits FROM site_stats" | ||
| #SqlQuery3 = "select rank from (select user_name, @curRank := @curRank + 1 as rank from user p, ( select @curRank := 0 ) q order by user_editcount DESC) q where user_name='"+usr+"';" | ||
| SqlQuery3 = "SELECT 1+(SELECT count(*) from user a WHERE a.user_editcount > b.user_editcount) as RNK FROM user b WHERE user_name='"+usr+"';" | ||
| SqlQuery4 = "select count(*) from user;" | ||
| conn = toolforge.connect('kawiki_p') | ||
| with conn.cursor() as curr: | ||
| s1 = curr.execute(SqlQuery1) | ||
| useredits_tup = curr.fetchall() | ||
| s2 = curr.execute(SqlQuery2) | ||
| totaledits_tup =curr.fetchall() | ||
| s3 = curr.execute(SqlQuery3) | ||
| userrank_tup = curr.fetchall() | ||
| s4 = curr.execute(SqlQuery4) | ||
| totalusers_tup = curr.fetchall() | ||
|
|
||
| conn.close() | ||
| try: | ||
| useredits = useredits_tup[0][0] | ||
| totaledits = totaledits_tup[0][0] | ||
| userrank = userrank_tup[0][0] | ||
| totalusers = totalusers_tup[0][0] | ||
| except: | ||
| flag = 0 | ||
|
|
||
| if(flag): | ||
| percentile = 100-((userrank*100)/(totalusers+1)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand the logic here. In practice this will be the same as
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was calculating percentile on the basis no of editcounts , so the user with highest editcounts gets 99 percentile (since 99% of users lie below him) and not the basis of userrank. In which case, the percentile of the user with highest no of editcounts would we 1, so he would be in top 1 percentile. |
||
| rating = math.ceil((userrank/totalusers)*100) | ||
| userrating = "User lies in top "+str(rating) + "%" | ||
| return(render(request, 'Microtask1/task2.html',{'edits':useredits, 'name':usr, 'total_edits':totaledits, 'rank':userrank, 'total_users':totalusers, 'Percentile':percentile, 'user_rating':userrating})) | ||
| return(render(request, 'Microtask1/task2.html',{"false_query":1})) | ||
| else: | ||
| return(render(request, 'Microtask1/task2.html',{})) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works, but if you want a nicer approach, you can wrap everything in a try...catch and throw an exception when the user is not found etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.