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

Side by Side Diff: sitescripts/urlfixer/web/submitData.py

Issue 8771229: Removed database connection caching from /submitData (Closed)
Patch Set: Created Nov. 16, 2012, 9:56 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 | « no previous file | no next file » | 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 os, MySQLdb, simplejson as json 18 import os, MySQLdb, simplejson as json
19 from urlparse import parse_qs 19 from urlparse import parse_qs
20 from sitescripts.web import url_handler 20 from sitescripts.web import url_handler
21 from sitescripts.utils import cached, get_config, setupStderr 21 from sitescripts.utils import cached, get_config, setupStderr
22 22
23 @url_handler("/submitData") 23 @url_handler("/submitData")
24 def submit_data(environ, start_response): 24 def submit_data(environ, start_response):
25 setupStderr(environ['wsgi.errors']) 25 setupStderr(environ["wsgi.errors"])
26 26
27 if environ["REQUEST_METHOD"].upper() != "POST": 27 if environ["REQUEST_METHOD"].upper() != "POST":
28 return showError("Unsupported request method", start_response) 28 return showError("Unsupported request method", start_response)
29 29
30 params = parse_qs(environ.get("QUERY_STRING", "")) 30 params = parse_qs(environ.get("QUERY_STRING", ""))
31 requestVersion = params.get("version", ["0"])[0] 31 requestVersion = params.get("version", ["0"])[0]
32 data = "{}" 32 data = "{}"
33 try: 33 try:
34 data_length = int(environ.get("CONTENT_LENGTH", "0")) 34 data_length = int(environ.get("CONTENT_LENGTH", "0"))
35 except ValueError: 35 except ValueError:
(...skipping 17 matching lines...) Expand all
53 return [] 53 return []
54 54
55 def process_domain(db, domain, status): 55 def process_domain(db, domain, status):
56 domain_id = _get_domain_id(db, domain) 56 domain_id = _get_domain_id(db, domain)
57 _increment_entry(db, domain_id, status) 57 _increment_entry(db, domain_id, status)
58 58
59 def showError(message, start_response): 59 def showError(message, start_response):
60 start_response("400 Processing Error", [("Content-Type", "text/plain; charset= utf-8")]) 60 start_response("400 Processing Error", [("Content-Type", "text/plain; charset= utf-8")])
61 return [message.encode("utf-8")] 61 return [message.encode("utf-8")]
62 62
63 @cached(600)
64 def _get_db(): 63 def _get_db():
65 database = get_config().get("urlfixer", "database") 64 database = get_config().get("urlfixer", "database")
66 dbuser = get_config().get("urlfixer", "dbuser") 65 dbuser = get_config().get("urlfixer", "dbuser")
67 dbpasswd = get_config().get("urlfixer", "dbpassword") 66 dbpasswd = get_config().get("urlfixer", "dbpassword")
68 if os.name == "nt": 67 if os.name == "nt":
69 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, 68 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database,
70 use_unicode=True, charset="utf8", named_pipe=True) 69 use_unicode=True, charset="utf8", named_pipe=True)
71 else: 70 else:
72 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, 71 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database,
73 use_unicode=True, charset="utf8") 72 use_unicode=True, charset="utf8")
74 73
75 def _get_domain_id(db, domain): 74 def _get_domain_id(db, domain):
76 cursor = db.cursor(MySQLdb.cursors.DictCursor) 75 cursor = db.cursor(MySQLdb.cursors.DictCursor)
77 cursor.execute("SELECT id FROM domains WHERE domain = %s", (domain)) 76 cursor.execute("SELECT id FROM domains WHERE domain = %s", (domain))
78 result = cursor.fetchone() 77 result = cursor.fetchone()
79 if result == None: 78 if result == None:
80 cursor.execute("INSERT INTO domains(domain) VALUES (%s)", (domain)) 79 cursor.execute("INSERT INTO domains(domain) VALUES (%s)", (domain))
81 return db.insert_id() 80 return db.insert_id()
82 else: 81 else:
83 return result["id"] 82 return result["id"]
84 83
85 def _increment_entry(db, domain_id, status): 84 def _increment_entry(db, domain_id, status):
86 cursor = db.cursor(MySQLdb.cursors.DictCursor) 85 cursor = db.cursor(MySQLdb.cursors.DictCursor)
87 cursor.execute("INSERT INTO corrections(domain, status, curr_month, prev_month , curr_year, prev_year) VALUES (%s, %s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE curr_month = curr_month + 1, curr_year = curr_year + 1", (domain_id, status, 1, 0, 1, 0)) 86 cursor.execute("INSERT INTO corrections(domain, status, curr_month, prev_month , curr_year, prev_year) VALUES (%s, %s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE curr_month = curr_month + 1, curr_year = curr_year + 1", (domain_id, status, 1, 0, 1, 0))
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld