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

Side by Side Diff: sitescripts/filterhits/web/query.py

Issue 29344572: Noissue - Removed obsolete sitescripts.filterhits package (Closed)
Patch Set: Created May 26, 2016, 12:05 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/filterhits/web/common.py ('k') | sitescripts/filterhits/web/submit.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 file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2016 Eyeo GmbH
5 #
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
8 # published by the Free Software Foundation.
9 #
10 # Adblock Plus is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
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/>.
17
18 import json
19 import os
20 import traceback
21 from urlparse import parse_qsl
22
23 import MySQLdb
24
25 from sitescripts.web import url_handler
26 from sitescripts.utils import cached, setupStderr
27 from sitescripts.filterhits import db
28 from sitescripts.filterhits.web import common
29
30
31 def query(domain=None, filter=None, skip=0, take=20, order="DESC", order_by="fre quency", **_):
32 """
33 Returns the SQL and parameters needed to perform a query of the filterhits d ata.
34 """
35 sql = """SELECT SQL_CALC_FOUND_ROWS domain, filter, frequency
36 FROM frequencies as freq
37 LEFT JOIN filters as f ON f.sha1=freq.filter_sha1
38 %s
39 ORDER BY %s
40 LIMIT %%s, %%s"""
41
42 where = zip(*[("%s LIKE %%s" % s, "%%%s%%" % p) for s, p in (("domain", doma in),
43 ("filter", filt er)) if p])
44 if where:
45 where_fields, params = where
46 where_sql = "WHERE " + " AND ".join(where_fields)
47 else:
48 where_sql = ""
49 params = []
50
51 order = order.upper() if order.upper() in ("ASC", "DESC") else "ASC"
52 if order_by not in ["filter", "domain", "frequency"]:
53 order_by = "frequency"
54 order_by_sql = "`%s` %s" % (order_by, order)
55
56 params = list(params) + [int(skip), int(take)]
57 return [sql % (where_sql, order_by_sql)] + params
58
59
60 @url_handler("/query")
61 def query_handler(environ, start_response):
62 setupStderr(environ["wsgi.errors"])
63 params = dict(parse_qsl(environ.get("QUERY_STRING", "")))
64
65 try:
66 db_connection = db.connect()
67 try:
68 results = db.query(db_connection, *query(**params), dict_result=True )
69 total = db.query(db_connection, "SELECT FOUND_ROWS()")[0][0]
70 finally:
71 db_connection.close()
72 except MySQLdb.Error:
73 traceback.print_exc()
74 return common.show_error("Failed to query database!", start_response,
75 "500 Database error")
76
77 try:
78 echo = int(params["echo"])
79 except (ValueError, KeyError):
80 echo = 0
81
82 response_headers = [("Content-type", "application/json; charset=utf-8")]
83 start_response("200 OK", response_headers)
84 return [json.dumps({"results": results, "echo": echo,
85 "total": total, "count": len(results)},
86 ensure_ascii=False).encode("utf-8")]
OLDNEW
« no previous file with comments | « sitescripts/filterhits/web/common.py ('k') | sitescripts/filterhits/web/submit.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld