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

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

Issue 8483155: Added typo data collection (/submitData) (Closed)
Patch Set: Created Oct. 9, 2012, 1:46 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
« no previous file with comments | « sitescripts/urlfixer/web/__init__.py ('k') | sitescripts/web.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # coding: utf-8
2
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
5 # http://mozilla.org/MPL/2.0/.
6
7 import os, MySQLdb, simplejson as json
8 from urlparse import parse_qs
9 from sitescripts.web import url_handler
10 from sitescripts.utils import cached, get_config, setupStderr
11
12 @url_handler("/submitData")
13 def submit_data(environ, start_response):
14 setupStderr(environ['wsgi.errors'])
15
16 if environ["REQUEST_METHOD"].upper() != "POST":
17 return showError("Unsupported request method", start_response)
18
19 params = parse_qs(environ.get("QUERY_STRING", ""))
20 requestVersion = params.get("version", ["0"])[0]
21 data = "{}"
22 try:
23 data_length = int(environ.get("CONTENT_LENGTH", "0"))
24 except ValueError:
25 data_length = 0
26 if data_length != 0:
27 data = environ["wsgi.input"].read(data_length)
28 try:
29 data = json.loads(data)
30 except json.decoder.JSONDecodeError:
31 return showError("Error while parsing JSON data.", start_response)
32
33 db = _get_db()
34
35 for domain, status in data.iteritems():
36 process_domain(db, domain, status)
37
38 db.commit()
39
40 response_headers = [("Content-type", "text/plain")]
41 start_response("200 OK", response_headers)
42 return []
43
44 def process_domain(db, domain, status):
45 domain_id = _get_domain_id(db, domain)
46 _increment_entry(db, domain_id, status)
47
48 def showError(message, start_response):
49 start_response("400 Processing Error", [("Content-Type", "text/plain; charset= utf-8")])
50 return [message.encode("utf-8")]
51
52 @cached(600)
53 def _get_db():
54 database = get_config().get("urlfixer", "database")
55 dbuser = get_config().get("urlfixer", "dbuser")
56 dbpasswd = get_config().get("urlfixer", "dbpassword")
57 if os.name == "nt":
58 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database,
59 use_unicode=True, charset="utf8", named_pipe=True)
60 else:
61 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database,
62 use_unicode=True, charset="utf8")
63
64 def _get_domain_id(db, domain):
65 cursor = db.cursor(MySQLdb.cursors.DictCursor)
66 cursor.execute("SELECT id FROM domains WHERE domain=%s", (domain))
67 result = cursor.fetchone()
68 if result == None:
69 cursor.execute("INSERT INTO domains(domain) VALUES (%s)", (domain))
70 return db.insert_id()
71 else:
72 return result["id"]
73
74 def _increment_entry(db, domain_id, status):
75 cursor = db.cursor(MySQLdb.cursors.DictCursor)
76 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) )
Wladimir Palant 2012/10/09 14:17:08 Style nit: spaces around binary operators
OLDNEW
« no previous file with comments | « sitescripts/urlfixer/web/__init__.py ('k') | sitescripts/web.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld