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

Side by Side Diff: sitescripts/reports/utils.py

Issue 8625042: Reports - user usefullness (Closed)
Patch Set: Reports - user usefulness Created Oct. 23, 2012, 2:15 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 # coding: utf-8 1 # coding: utf-8
2 2
3 # This Source Code is subject to the terms of the Mozilla Public License 3 # This Source Code is subject to the terms of the Mozilla Public License
4 # version 2.0 (the "License"). You can obtain a copy of the License at 4 # version 2.0 (the "License"). You can obtain a copy of the License at
5 # http://mozilla.org/MPL/2.0/. 5 # http://mozilla.org/MPL/2.0/.
6 6
7 import hashlib, hmac, base64, MySQLdb, os, re, marshal, subprocess 7 import hashlib, hmac, base64, MySQLdb, os, re, marshal, subprocess
8 from sitescripts.utils import get_config, cached, get_template, sendMail 8 from sitescripts.utils import get_config, cached, get_template, sendMail
9 9
10 def getReportSubscriptions(guid): 10 def getReportSubscriptions(guid):
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 '''DELETE FROM #PFX#reports WHERE guid = %s''', 104 '''DELETE FROM #PFX#reports WHERE guid = %s''',
105 (guid)) 105 (guid))
106 get_db().commit() 106 get_db().commit()
107 file = os.path.join(get_config().get('reports', 'dataPath'), guid[0], guid[1], guid[2], guid[3], guid + '.html') 107 file = os.path.join(get_config().get('reports', 'dataPath'), guid[0], guid[1], guid[2], guid[3], guid + '.html')
108 if os.path.isfile(file): 108 if os.path.isfile(file):
109 os.remove(file) 109 os.remove(file)
110 file = os.path.join(get_config().get('reports', 'dataPath'), guid[0], guid[1], guid[2], guid[3], guid + '.png') 110 file = os.path.join(get_config().get('reports', 'dataPath'), guid[0], guid[1], guid[2], guid[3], guid + '.png')
111 if os.path.isfile(file): 111 if os.path.isfile(file):
112 os.remove(file) 112 os.remove(file)
113 113
114 @cached(3600)
115 def getUserUsefulnessScore(contact):
116 if contact == None:
117 return 0
118
119 cursor = get_db().cursor()
120 # source from http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
121 executeQuery(cursor,
122 '''SELECT ((positive + 1.9208) / (positive + negative)
123 - 1.96 * SQRT((positive * negative) / (positive + negati ve) + 0.9604) / (positive + negative))
124 / (1 + 3.8416 / (positive + negative)) AS score FROM #PF X#users WHERE id = %s''',
125 (contact))
126 score = cursor.fetchone()
127 if score == None:
128 return 0
129
130 if score[0] == None: # no score yet
131 return 0.3
132 else:
133 return 4 * score[0]
134
135 def updateUserUsefulness(contact, newusefulness, oldusefulness):
136 new = int(newusefulness)
137 old = int(oldusefulness)
138 if new == old:
139 return
140 positive = 0
141 negative = 0
142 if old > 0:
143 positive -= 1
144 elif old < 0:
145 negative -= 1
146 if new > 0:
147 positive += 1
148 elif new < 0:
149 negative += 1
150 cursor = get_db().cursor()
151 executeQuery(cursor,
152 '''UPDATE #PFX#users SET negative = negative + %s, positive = posi tive + %s WHERE id = %s''',
153 (negative, positive, contact))
154 get_db().commit()
155
114 def saveScreenshot(guid, screenshot): 156 def saveScreenshot(guid, screenshot):
115 prefix = 'data:image/png;base64,' 157 prefix = 'data:image/png;base64,'
116 if not screenshot.startswith(prefix): 158 if not screenshot.startswith(prefix):
117 raise TypeError('Screenshot is not a PNG image') 159 raise TypeError('Screenshot is not a PNG image')
118 data = base64.b64decode(screenshot[len(prefix):]) 160 data = base64.b64decode(screenshot[len(prefix):])
119 file = os.path.join(get_config().get('reports', 'dataPath'), guid[0], guid[1], guid[2], guid[3], guid + '.png') 161 file = os.path.join(get_config().get('reports', 'dataPath'), guid[0], guid[1], guid[2], guid[3], guid + '.png')
120 dir = os.path.dirname(file) 162 dir = os.path.dirname(file)
121 if not os.path.exists(dir): 163 if not os.path.exists(dir):
122 os.makedirs(dir) 164 os.makedirs(dir)
123 f = open(file, 'wb') 165 f = open(file, 'wb')
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 dbpasswd = get_config().get('reports', 'dbpassword') 217 dbpasswd = get_config().get('reports', 'dbpassword')
176 if os.name == 'nt': 218 if os.name == 'nt':
177 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, use_unicod e=True, charset='utf8', named_pipe=True) 219 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, use_unicod e=True, charset='utf8', named_pipe=True)
178 else: 220 else:
179 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, use_unicod e=True, charset='utf8') 221 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, use_unicod e=True, charset='utf8')
180 222
181 def executeQuery(cursor, query, args=None): 223 def executeQuery(cursor, query, args=None):
182 tablePrefix = get_config().get('reports', 'dbprefix') 224 tablePrefix = get_config().get('reports', 'dbprefix')
183 query = re.sub(r'#PFX#', tablePrefix, query) 225 query = re.sub(r'#PFX#', tablePrefix, query)
184 cursor.execute(query, args) 226 cursor.execute(query, args)
OLDNEW

Powered by Google App Engine
This is Rietveld