forked from osm-fr/osmose-frontend
-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors.py
199 lines (166 loc) · 7.38 KB
/
errors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#! /usr/bin/env python
#-*- coding: utf-8 -*-
###########################################################################
## ##
## Copyrights Etienne Chové <[email protected]> 2009 ##
## ##
## This program is free software: you can redistribute it and/or modify ##
## it under the terms of the GNU General Public License as published by ##
## the Free Software Foundation, either version 3 of the License, or ##
## (at your option) any later version. ##
## ##
## This program is distributed in the hope that it will be useful, ##
## but WITHOUT ANY WARRANTY; without even the implied warranty of ##
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ##
## GNU General Public License for more details. ##
## ##
## You should have received a copy of the GNU General Public License ##
## along with this program. If not, see <http://www.gnu.org/licenses/>. ##
## ##
###########################################################################
from bottle import route, request, template, response, abort, redirect
from tools import utils
from tools import query
from tools import query_meta
from tools.OrderedDict import OrderedDict
import StringIO, re
import errors_graph
def _errors_geo(db, lang, params):
results = query._gets(db, params)
translate = utils.translator(lang)
features = []
for res in results:
properties = {"error_id": res["id"], "item": res["item"] or 0}
features.append({"type": "Feature", "geometry": {"type": "Point", "coordinates": [float(res["lon"]), float(res["lat"])]}, "properties": properties})
return {"type": "FeatureCollection", "features": features}
def _errors(db, lang, params):
results = query._gets(db, params)
out = OrderedDict()
if not params.full:
out["description"] = ["lat", "lon", "error_id", "item"]
else:
out["description"] = ["lat", "lon", "error_id", "item", "source", "class", "elems", "subclass", "subtitle", "title", "level", "update", "username"]
out["errors"] = []
translate = utils.translator(lang)
for res in results:
lat = res["lat"]
lon = res["lon"]
error_id = res["id"]
item = res["item"] or 0
if not params.full:
out["errors"].append([str(lat), str(lon), str(error_id), str(item)])
else:
source = res["source"]
classs = res["class"]
elems = res["elems"]
subclass = res["subclass"]
subtitle = translate.select(res["subtitle"])
title = translate.select(res["title"])
level = res["level"]
update = res["timestamp"]
username = res["username"] or ""
out["errors"].append([str(lat), str(lon), str(error_id), str(item), str(source), str(classs), str(elems), str(subclass), subtitle, title, str(level), str(update), username])
return out
@route('/api/0.2/errors')
def errors(db, lang):
params = query._params()
return _errors(db, lang, params)
def int_list(s):
return map(lambda x: int(x), filter(lambda x: x and x!='',s).split(','))
@route('/errors/graph.<format:ext>')
def graph(db, format='png'):
class options:
sources = request.params.get('source', type=int_list, default=[])
classes = request.params.get('class', type=int_list, default=[])
items = request.params.get('item', type=int_list, default=[])
levels = request.params.get('level', type=int_list, default=[])
country = request.params.get('country')
if country <> None and not re.match(r"^([a-z_]+(\*|))$", country):
country = None
try:
data = errors_graph.make_plt(db, options, format)
response.content_type = {'png':'image/png', 'svg':'image/svg+xml', 'pdf':'application/pdf'}[format]
return data
except Exception, e:
response.content_type = "text/plain"
import traceback
out = StringIO.StringIO()
traceback.print_exc(file=out)
return out.getvalue() + "\n"
@route('/errors')
@route('/errors.<format:ext>')
@route('/errors/')
@route('/errors/done')
@route('/errors/done.<format:ext>')
@route('/errors/false-positive')
@route('/errors/false-positive.<format:ext>')
def index(db, lang, format=None):
if request.path.endswith("false-positive"):
title = _("False positives")
gen = "false-positive"
elif request.path.endswith("done"):
title = _("Fixed issues")
gen = "done"
else:
title = _("Informations")
gen = "error"
if not format in ('rss', 'gpx', 'josm'):
format = None
countries = query_meta._countries(db, lang) if format == None else None
items = query_meta._items(db, lang)
params = query._params()
params.status = {"error":"open", "false-positive": "false", "done":"done"}[gen]
params.limit = None
if format == None and params.item:
errors_groups = query._count(db, params, [
"dynpoi_class.item",
"dynpoi_class.source",
"dynpoi_class.class",
"source.country",
"source.analyser"],
["dynpoi_item"], [
"min(dynpoi_item.menu::text)::hstore AS menu",
"min(dynpoi_class.title::text)::hstore AS title"],
orderBy = True)
total = 0
for res in errors_groups:
if res["count"] != -1:
total += res["count"]
else:
errors_groups = []
total = 0
params.limit = request.params.get('limit', type=int, default=100)
if params.limit > 10000:
params.limit = 10000
if (total > 0 and total < 1000) or params.limit:
params.full = True
errors = query._gets(db, params)
if gen in ("false-positive", "done"):
opt_date = "date"
else:
opt_date = "-1"
else:
opt_date = None
errors = None
if format == 'rss':
response.content_type = 'application/rss+xml'
tpl = 'errors/list.rss'
elif format == 'gpx':
response.content_type = 'application/gpx+xml'
tpl = 'errors/list.gpx'
elif format == 'josm':
objects = []
for res in errors:
if res["elems"]:
elems = res["elems"].split("_")
for e in elems:
m = re.match(r"([a-z]+)([0-9]+)", e)
if m:
cur_type = m.group(1)
objects.append(cur_type[0] + m.group(2))
response.status = 302
response.set_header('Location', 'http://localhost:8111/load_object?objects=%s' % ','.join(objects))
return
else:
tpl = 'errors/index'
return template(tpl, countries=countries, items=items, errors_groups=errors_groups, total=total, errors=errors, query=request.query_string, country=params.country, item=params.item, translate=utils.translator(lang), gen=gen, opt_date=opt_date, title=title, website=utils.website, main_website=utils.main_website, remote_url_read=utils.remote_url_read)