Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: sitescripts/reports/utils.py

Issue 8625042: Reports - user usefullness (Closed)
Patch Set: Reports - user usefulness Created Oct. 23, 2012, 2:15 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sitescripts/reports/utils.py
===================================================================
--- a/sitescripts/reports/utils.py
+++ b/sitescripts/reports/utils.py
@@ -111,6 +111,48 @@
if os.path.isfile(file):
os.remove(file)
+@cached(3600)
+def getUserUsefulnessScore(contact):
+ if contact == None:
+ return 0
+
+ cursor = get_db().cursor()
+ # source from http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
+ executeQuery(cursor,
+ '''SELECT ((positive + 1.9208) / (positive + negative)
+ - 1.96 * SQRT((positive * negative) / (positive + negative) + 0.9604) / (positive + negative))
+ / (1 + 3.8416 / (positive + negative)) AS score FROM #PFX#users WHERE id = %s''',
+ (contact))
+ score = cursor.fetchone()
+ if score == None:
+ return 0
+
+ if score[0] == None: # no score yet
+ return 0.3
+ else:
+ return 4 * score[0]
+
+def updateUserUsefulness(contact, newusefulness, oldusefulness):
+ new = int(newusefulness)
+ old = int(oldusefulness)
+ if new == old:
+ return
+ positive = 0
+ negative = 0
+ if old > 0:
+ positive -= 1
+ elif old < 0:
+ negative -= 1
+ if new > 0:
+ positive += 1
+ elif new < 0:
+ negative += 1
+ cursor = get_db().cursor()
+ executeQuery(cursor,
+ '''UPDATE #PFX#users SET negative = negative + %s, positive = positive + %s WHERE id = %s''',
+ (negative, positive, contact))
+ get_db().commit()
+
def saveScreenshot(guid, screenshot):
prefix = 'data:image/png;base64,'
if not screenshot.startswith(prefix):

Powered by Google App Engine
This is Rietveld