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

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

Issue 8793015: Reports - user profile (Closed)
Patch Set: Reports - user profile Created Nov. 7, 2012, 11:39 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sitescripts/reports/template/showUser.html ('k') | sitescripts/reports/web/showUser.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # coding: utf-8 1 # coding: utf-8
2 2
3 # This file is part of the Adblock Plus web scripts, 3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2012 Eyeo GmbH 4 # Copyright (C) 2006-2012 Eyeo GmbH
5 # 5 #
6 # Adblock Plus is free software: you can redistribute it and/or modify 6 # Adblock Plus is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 3 as 7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation. 8 # published by the Free Software Foundation.
9 # 9 #
10 # Adblock Plus is distributed in the hope that it will be useful, 10 # Adblock Plus is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details. 13 # GNU General Public License for more details.
14 # 14 #
15 # You should have received a copy of the GNU General Public License 15 # You should have received a copy of the GNU General Public License
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
17 17
18 import hashlib, hmac, base64, MySQLdb, os, re, marshal, subprocess 18 import hashlib, hmac, base64, MySQLdb, os, re, marshal, subprocess
19 from sitescripts.utils import get_config, cached, get_template, sendMail 19 from sitescripts.utils import get_config, cached, get_template, anonymizeMail, s endMail
20 20
21 def getReportSubscriptions(guid): 21 def getReportSubscriptions(guid):
22 cursor = get_db().cursor(MySQLdb.cursors.DictCursor) 22 cursor = get_db().cursor(MySQLdb.cursors.DictCursor)
23 executeQuery(cursor, 23 executeQuery(cursor,
24 '''SELECT url, hasmatches FROM #PFX#sublists INNER JOIN 24 '''SELECT url, hasmatches FROM #PFX#sublists INNER JOIN
25 #PFX#subscriptions ON (#PFX#sublists.list = #PFX#subscriptions.id) 25 #PFX#subscriptions ON (#PFX#sublists.list = #PFX#subscriptions.id)
26 WHERE report = %s''', 26 WHERE report = %s''',
27 (guid)) 27 (guid))
28 rows = cursor.fetchall() 28 rows = cursor.fetchall()
29 cursor.close() 29 cursor.close()
(...skipping 10 matching lines...) Expand all
40 FROM #PFX#reports WHERE ctime >= FROM_UNIXTIME(%s) LIMIT %s OFFS ET %s''', 40 FROM #PFX#reports WHERE ctime >= FROM_UNIXTIME(%s) LIMIT %s OFFS ET %s''',
41 (startTime, count, offset)) 41 (startTime, count, offset))
42 rows = cursor.fetchall() 42 rows = cursor.fetchall()
43 cursor.close() 43 cursor.close()
44 if len(rows) == 0: 44 if len(rows) == 0:
45 break 45 break
46 for row in rows: 46 for row in rows:
47 yield row 47 yield row
48 offset += len(rows) 48 offset += len(rows)
49 49
50 def getReportsForUser(contact):
51 cursor = get_db().cursor(MySQLdb.cursors.DictCursor)
52 executeQuery(cursor,
53 '''SELECT guid, type, UNIX_TIMESTAMP(ctime) AS ctime, status, site , contact,
54 comment, hasscreenshot, knownissues
55 FROM #PFX#reports WHERE contact = %s ORDER BY ctime DESC LIMIT 100 ''',
56 (contact))
57 rows = cursor.fetchall()
58 cursor.close()
59 return rows
60
50 def getReport(guid): 61 def getReport(guid):
51 cursor = get_db().cursor() 62 cursor = get_db().cursor()
52 executeQuery(cursor, 63 executeQuery(cursor,
53 '''SELECT dump FROM #PFX#reports WHERE guid = %s''', 64 '''SELECT dump FROM #PFX#reports WHERE guid = %s''',
54 (guid)) 65 (guid))
55 report = cursor.fetchone() 66 report = cursor.fetchone()
56 if report == None: 67 if report == None:
57 return None 68 return None
58 69
59 reportData = marshal.loads(report[0]) 70 reportData = marshal.loads(report[0])
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 if id != None: 106 if id != None:
96 filterMatch = lambda f: any(u == sn['id'] for u in f.get('subscriptions' , [])) 107 filterMatch = lambda f: any(u == sn['id'] for u in f.get('subscriptions' , []))
97 hasMatches = any(filterMatch(f) for f in reportData.get('filters', [])) 108 hasMatches = any(filterMatch(f) for f in reportData.get('filters', []))
98 executeQuery(cursor, 109 executeQuery(cursor,
99 '''INSERT IGNORE INTO #PFX#sublists (report, list, hasmatches) VA LUES (%s, %s, %s)''', 110 '''INSERT IGNORE INTO #PFX#sublists (report, list, hasmatches) VA LUES (%s, %s, %s)''',
100 (guid, id[0], hasMatches)) 111 (guid, id[0], hasMatches))
101 112
102 get_db().commit() 113 get_db().commit()
103 114
104 reportData['guid'] = guid 115 reportData['guid'] = guid
116 if contact:
117 email = reportData['email']
118 email = re.sub(r' at ', r'@', email)
119 email = re.sub(r' dot ', r'.', email)
120 reportData['email'] = anonymizeMail(email)
121 reportData['uid'] = contact
122
105 file = os.path.join(get_config().get('reports', 'dataPath'), guid[0], guid[1], guid[2], guid[3], guid + '.html') 123 file = os.path.join(get_config().get('reports', 'dataPath'), guid[0], guid[1], guid[2], guid[3], guid + '.html')
106 dir = os.path.dirname(file) 124 dir = os.path.dirname(file)
107 if not os.path.exists(dir): 125 if not os.path.exists(dir):
108 os.makedirs(dir) 126 os.makedirs(dir)
109 template = get_template(get_config().get('reports', 'webTemplate')) 127 template = get_template(get_config().get('reports', 'webTemplate'))
110 template.stream(reportData).dump(file, encoding='utf-8') 128 template.stream(reportData).dump(file, encoding='utf-8')
111 129
112 def removeReport(guid): 130 def removeReport(guid):
113 cursor = get_db().cursor() 131 cursor = get_db().cursor()
114 executeQuery(cursor, 132 executeQuery(cursor,
115 '''DELETE FROM #PFX#reports WHERE guid = %s''', 133 '''DELETE FROM #PFX#reports WHERE guid = %s''',
116 (guid)) 134 (guid))
117 get_db().commit() 135 get_db().commit()
118 file = os.path.join(get_config().get('reports', 'dataPath'), guid[0], guid[1], guid[2], guid[3], guid + '.html') 136 file = os.path.join(get_config().get('reports', 'dataPath'), guid[0], guid[1], guid[2], guid[3], guid + '.html')
119 if os.path.isfile(file): 137 if os.path.isfile(file):
120 os.remove(file) 138 os.remove(file)
121 file = os.path.join(get_config().get('reports', 'dataPath'), guid[0], guid[1], guid[2], guid[3], guid + '.png') 139 file = os.path.join(get_config().get('reports', 'dataPath'), guid[0], guid[1], guid[2], guid[3], guid + '.png')
122 if os.path.isfile(file): 140 if os.path.isfile(file):
123 os.remove(file) 141 os.remove(file)
124 142
143 def getUser(contact):
144 cursor = get_db().cursor(MySQLdb.cursors.DictCursor)
145 executeQuery(cursor,
146 '''SELECT reports, positive, negative FROM #PFX#users WHERE id = % s''',
147 (contact))
148 user = cursor.fetchone()
149 return user
150
125 @cached(3600) 151 @cached(3600)
126 def getUserUsefulnessScore(contact): 152 def getUserUsefulnessScore(contact):
127 if contact == None: 153 if contact == None:
128 return 0 154 return 0
129 155
130 cursor = get_db().cursor() 156 cursor = get_db().cursor()
131 # source from http://www.evanmiller.org/how-not-to-sort-by-average-rating.html 157 # source from http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
132 executeQuery(cursor, 158 executeQuery(cursor,
133 '''SELECT ((positive + 1.9208) / (positive + negative) 159 '''SELECT ((positive + 1.9208) / (positive + negative)
134 - 1.96 * SQRT((positive * negative) / (positive + negati ve) + 0.9604) / (positive + negative)) 160 - 1.96 * SQRT((positive * negative) / (positive + negati ve) + 0.9604) / (positive + negative))
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 dbpasswd = get_config().get('reports', 'dbpassword') 254 dbpasswd = get_config().get('reports', 'dbpassword')
229 if os.name == 'nt': 255 if os.name == 'nt':
230 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, use_unicod e=True, charset='utf8', named_pipe=True) 256 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, use_unicod e=True, charset='utf8', named_pipe=True)
231 else: 257 else:
232 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, use_unicod e=True, charset='utf8') 258 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, use_unicod e=True, charset='utf8')
233 259
234 def executeQuery(cursor, query, args=None): 260 def executeQuery(cursor, query, args=None):
235 tablePrefix = get_config().get('reports', 'dbprefix') 261 tablePrefix = get_config().get('reports', 'dbprefix')
236 query = re.sub(r'#PFX#', tablePrefix, query) 262 query = re.sub(r'#PFX#', tablePrefix, query)
237 cursor.execute(query, args) 263 cursor.execute(query, args)
OLDNEW
« no previous file with comments | « sitescripts/reports/template/showUser.html ('k') | sitescripts/reports/web/showUser.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld