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, 9:38 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 count = 1000
Wladimir Palant 2012/11/07 09:46:18 We don't need this complicated setup for user repo
Andrey Novikov 2012/11/07 11:39:38 Done.
52 offset = 0
53 while True:
54 cursor = get_db().cursor(MySQLdb.cursors.DictCursor)
55 executeQuery(cursor,
56 '''SELECT guid, type, UNIX_TIMESTAMP(ctime) AS ctime, status, si te, contact,
57 comment, hasscreenshot, knownissues
58 FROM #PFX#reports WHERE contact = %s LIMIT %s OFFSET %s''',
59 (contact, count, offset))
60 rows = cursor.fetchall()
61 cursor.close()
62 if len(rows) == 0:
63 break
64 for row in rows:
65 yield row
66 offset += len(rows)
67
50 def getReport(guid): 68 def getReport(guid):
51 cursor = get_db().cursor() 69 cursor = get_db().cursor()
52 executeQuery(cursor, 70 executeQuery(cursor,
53 '''SELECT dump FROM #PFX#reports WHERE guid = %s''', 71 '''SELECT dump FROM #PFX#reports WHERE guid = %s''',
54 (guid)) 72 (guid))
55 report = cursor.fetchone() 73 report = cursor.fetchone()
56 if report == None: 74 if report == None:
57 return None 75 return None
58 76
59 reportData = marshal.loads(report[0]) 77 reportData = marshal.loads(report[0])
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 if id != None: 113 if id != None:
96 filterMatch = lambda f: any(u == sn['id'] for u in f.get('subscriptions' , [])) 114 filterMatch = lambda f: any(u == sn['id'] for u in f.get('subscriptions' , []))
97 hasMatches = any(filterMatch(f) for f in reportData.get('filters', [])) 115 hasMatches = any(filterMatch(f) for f in reportData.get('filters', []))
98 executeQuery(cursor, 116 executeQuery(cursor,
99 '''INSERT IGNORE INTO #PFX#sublists (report, list, hasmatches) VA LUES (%s, %s, %s)''', 117 '''INSERT IGNORE INTO #PFX#sublists (report, list, hasmatches) VA LUES (%s, %s, %s)''',
100 (guid, id[0], hasMatches)) 118 (guid, id[0], hasMatches))
101 119
102 get_db().commit() 120 get_db().commit()
103 121
104 reportData['guid'] = guid 122 reportData['guid'] = guid
123 if contact:
124 email = reportData['email']
125 email = re.sub(r' at ', r'@', email)
126 email = re.sub(r' dot ', r'.', email)
127 reportData['email'] = anonymizeMail(email)
128 reportData['uid'] = contact
129
105 file = os.path.join(get_config().get('reports', 'dataPath'), guid[0], guid[1], guid[2], guid[3], guid + '.html') 130 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) 131 dir = os.path.dirname(file)
107 if not os.path.exists(dir): 132 if not os.path.exists(dir):
108 os.makedirs(dir) 133 os.makedirs(dir)
109 template = get_template(get_config().get('reports', 'webTemplate')) 134 template = get_template(get_config().get('reports', 'webTemplate'))
110 template.stream(reportData).dump(file, encoding='utf-8') 135 template.stream(reportData).dump(file, encoding='utf-8')
111 136
112 def removeReport(guid): 137 def removeReport(guid):
113 cursor = get_db().cursor() 138 cursor = get_db().cursor()
114 executeQuery(cursor, 139 executeQuery(cursor,
115 '''DELETE FROM #PFX#reports WHERE guid = %s''', 140 '''DELETE FROM #PFX#reports WHERE guid = %s''',
116 (guid)) 141 (guid))
117 get_db().commit() 142 get_db().commit()
118 file = os.path.join(get_config().get('reports', 'dataPath'), guid[0], guid[1], guid[2], guid[3], guid + '.html') 143 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): 144 if os.path.isfile(file):
120 os.remove(file) 145 os.remove(file)
121 file = os.path.join(get_config().get('reports', 'dataPath'), guid[0], guid[1], guid[2], guid[3], guid + '.png') 146 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): 147 if os.path.isfile(file):
123 os.remove(file) 148 os.remove(file)
124 149
150 def getUser(contact):
151 cursor = get_db().cursor(MySQLdb.cursors.DictCursor)
152 executeQuery(cursor,
153 '''SELECT reports, positive, negative FROM #PFX#users WHERE id = % s''',
154 (contact))
155 user = cursor.fetchone()
156 return user
157
125 @cached(3600) 158 @cached(3600)
126 def getUserUsefulnessScore(contact): 159 def getUserUsefulnessScore(contact):
127 if contact == None: 160 if contact == None:
128 return 0 161 return 0
129 162
130 cursor = get_db().cursor() 163 cursor = get_db().cursor()
131 # source from http://www.evanmiller.org/how-not-to-sort-by-average-rating.html 164 # source from http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
132 executeQuery(cursor, 165 executeQuery(cursor,
133 '''SELECT ((positive + 1.9208) / (positive + negative) 166 '''SELECT ((positive + 1.9208) / (positive + negative)
134 - 1.96 * SQRT((positive * negative) / (positive + negati ve) + 0.9604) / (positive + negative)) 167 - 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') 261 dbpasswd = get_config().get('reports', 'dbpassword')
229 if os.name == 'nt': 262 if os.name == 'nt':
230 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, use_unicod e=True, charset='utf8', named_pipe=True) 263 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, use_unicod e=True, charset='utf8', named_pipe=True)
231 else: 264 else:
232 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, use_unicod e=True, charset='utf8') 265 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, use_unicod e=True, charset='utf8')
233 266
234 def executeQuery(cursor, query, args=None): 267 def executeQuery(cursor, query, args=None):
235 tablePrefix = get_config().get('reports', 'dbprefix') 268 tablePrefix = get_config().get('reports', 'dbprefix')
236 query = re.sub(r'#PFX#', tablePrefix, query) 269 query = re.sub(r'#PFX#', tablePrefix, query)
237 cursor.execute(query, args) 270 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