-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
599 lines (531 loc) · 22.5 KB
/
app.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
import yaml
import os
import json
import requests
from flask import Flask, render_template, request, redirect, session, url_for, jsonify, g, flash
from flask_babel import Babel, gettext
from oauth_wikidata import get_username, upload_file, get_token, build_text
from requests_oauthlib import OAuth1Session
from wikidata import query_monuments, query_monuments_without_coords, query_monument, get_category_info, get_article,\
get_sitelinks, api_post_request, query_monuments_selected
import gspread
from oauth2client.service_account import ServiceAccountCredentials
__dir__ = os.path.dirname(__file__)
app = Flask(__name__)
app.config.update(yaml.safe_load(open(os.path.join(__dir__, 'config.yaml'))))
BABEL = Babel(app)
##############################################################
# LOGIN
##############################################################
@app.before_request
def init_profile():
g.profiling = []
@app.before_request
def global_user():
g.user = get_username()
@app.route('/login')
def login():
next_page = request.args.get('next')
if next_page:
session['after_login'] = next_page
client_key = app.config['CONSUMER_KEY']
client_secret = app.config['CONSUMER_SECRET']
base_url = 'https://meta.wikimedia.org/w/index.php'
request_token_url = base_url + '?title=Special%3aOAuth%2finitiate'
oauth = OAuth1Session(client_key,
client_secret=client_secret,
callback_uri='oob')
fetch_response = oauth.fetch_request_token(request_token_url)
session['owner_key'] = fetch_response.get('oauth_token')
session['owner_secret'] = fetch_response.get('oauth_token_secret')
base_authorization_url = 'https://meta.wikimedia.org/wiki/Special:OAuth/authorize'
authorization_url = oauth.authorization_url(base_authorization_url,
oauth_consumer_key=client_key)
return redirect(authorization_url)
@app.route("/oauth-callback", methods=["GET"])
def oauth_callback():
base_url = 'https://meta.wikimedia.org/w/index.php'
client_key = app.config['CONSUMER_KEY']
client_secret = app.config['CONSUMER_SECRET']
oauth = OAuth1Session(client_key,
client_secret=client_secret,
resource_owner_key=session['owner_key'],
resource_owner_secret=session['owner_secret'])
oauth_response = oauth.parse_authorization_response(request.url)
verifier = oauth_response.get('oauth_verifier')
access_token_url = base_url + '?title=Special%3aOAuth%2ftoken'
oauth = OAuth1Session(client_key,
client_secret=client_secret,
resource_owner_key=session['owner_key'],
resource_owner_secret=session['owner_secret'],
verifier=verifier)
oauth_tokens = oauth.fetch_access_token(access_token_url)
session['owner_key'] = oauth_tokens.get('oauth_token')
session['owner_secret'] = oauth_tokens.get('oauth_token_secret')
next_page = session.get('after_login')
return redirect(next_page)
##############################################################
# LOCALIZAÇÃO
##############################################################
# Função para pegar a língua de preferência do usuário
@BABEL.localeselector
def get_locale():
if request.args.get('lang'):
session['lang'] = request.args.get('lang')
return session.get('lang', 'pt')
# Função para mudar a língua de exibição do conteúdo
@app.route('/set_locale')
def set_locale():
next_page = request.args.get('return_to')
lang = request.args.get('lang')
session["lang"] = lang.replace("-", "_")
redirected = redirect(next_page)
redirected.delete_cookie('session', '/item')
return redirected
@app.context_processor
def inject_language():
possible_languages = {
"aa": "afar",
"ab": "Аҧсуа",
"ae": "avesta",
"af": "Afrikaans",
"ak": "akana",
"am": "አማርኛ",
"an": "aragonés",
"ar": "العربية",
"as": "অসমীয়া",
"av": "авар",
"ay": "aymar",
"az": "Azərbaycan",
"ba": "Башҡорт",
"be": "беларуская",
"bg": "български",
"bi": "Bislama",
"bih": "भोजपुरी",
"bm": "bamanankan",
"bn": "বাংলা",
"bo": "བོད་ཡིག",
"br": "brezhoneg",
"bs": "bosanski",
"ca": "català",
"ce": "нохчийн",
"ch": "chamoru",
"co": "corsu",
"cr": "ᓀᐦᐃᔭᐤ",
"cs": "česky",
"cu": "ѩзыкъ словѣньскъ",
"cv": "Чӑваш",
"cy": "Cymraeg",
"da": "dansk",
"de": "Deutsch",
"dv": "ދިވެހިބަސް",
"dz": "ཇོང་ཁ",
"ee": "Ɛʋɛ",
"el": "Ελληνικά",
"en": "English",
"eo": "Esperanto",
"es": "español",
"et": "eesti",
"eu": "euskara",
"fa": "فارسی",
"ff": "Fulfulde",
"fi": "suomi",
"fj": "na vosa Vakaviti",
"fo": "føroyskt",
"fr": "Français",
"fy": "Frysk",
"ga": "Gaeilge",
"gd": "Gàidhlig",
"gl": "galego",
"gn": "Avañe'ẽ",
"gu": "ગુજરાતી",
"gv": "Gaelg",
"ha": "حَوْسََ",
"he": "עִבְרִית",
"hi": "हिन्दी",
"ho": "Hiri Motu",
"hr": "hrvatski",
"ht": "kreyòl ayisyen",
"hu": "magyar",
"hy": "Հայերեն",
"hz": "Otjiherero",
"ia": "Interlingua",
"id": "bahasa Indonesia",
"ie": "Interlingue",
"ig": "Ibo",
"ii": "ꆇꉙ",
"ik": "Iñupiaq",
"io": "Ido",
"is": "íslenska",
"it": "italiano",
"iu": "ᐃᓄᒃᑎᑐᑦ",
"ja": "日本語",
"jv": "Basa Jawa",
"ka": "ქართული",
"kg": "Kikongo",
"ki": "Gĩkũyũ",
"kj": "kuanyama",
"kk": "қазақша",
"kl": "kalaallisut",
"km": "ភាសាខ្មែរ",
"kn": "ಕನ್ನಡ",
"ko": "한국어",
"kr": "kanuri",
"ks": "कश्मीरी - (كشميري)",
"ku": "كوردي",
"kv": "коми",
"kw": "Kernewek",
"ky": "Кыргызча",
"la": "latine",
"lb": "Lëtzebuergesch",
"lg": "Luganda",
"li": "Lèmburgs",
"ln": "Lingala",
"lo": "ລາວ",
"lt": "lietuvių",
"lu": " ",
"lv": "latviešu",
"mai": "मैथिली",
"mg": "Malagasy fiteny",
"mh": "Kajin M̧ajeļ; Ebon",
"mi": "te reo Māori",
"mk": "македонски",
"ml": "മലയാളം",
"mn": "монгол",
"mr": "मराठी",
"ms": "بهاس ملاي",
"mt": "bil-Malta",
"my": "မန္မာစာ",
"na": "ekakairũ naoero",
"nb": "Bokmål",
"nd": "isiNdebele",
"ne": "नेपाली",
"ng": "O(shi)wambo",
"nl": "Nederlands",
"nn": "Norsk Nynorsk",
"no": "norsk",
"nr": "Ndébélé",
"nv": "Diné bizaad",
"ny": "chiCheŵa",
"oc": "occitan",
"oj": "ᐊᓂᔑᓇᐯ",
"om": "Oromoo",
"or": "ଓଡ଼ିଆ",
"os": "иронау",
"pa": "ਪੰਜਾਬੀ",
"pi": "पािऴ",
"pl": "polski",
"ps": "پښتو",
"pt": "português",
"pt_br": "português brasileiro",
"qu": "Runa Simi",
"rm": "rumantsch",
"rn": "kiRundi",
"ro": "română",
"ru": "русский",
"rw": "kinyaRwanda",
"sa": "संस्कृत",
"sc": "sardu",
"sd": "सिनधि",
"se": "sámegiella",
"sg": "sängö",
"sh": "хрватскосрпск",
"si": "සිංහල",
"sk": "slovenčina",
"sl": "slovenščina",
"sm": "Gagana Samoa",
"sn": "chiShona",
"so": "Soomaaliga",
"sq": "Shqip",
"sr": "српски",
"ss": "SiSwati",
"st": "seSotho",
"su": "basa Sunda",
"sv": "svenska",
"sw": "Kiswahili",
"ta": "தமிழ்",
"te": "తెలుగు",
"tg": "Тоҷикӣ",
"th": "ภาษาไทย",
"ti": "ትግርኛ",
"tk": "Түркмен",
"tl": "Tagalog",
"tn": "seTswana",
"to": "faka-Tonga",
"tr": "Türkçe",
"ts": "xiTsonga",
"tt": "Tatarça",
"tw": "Twi",
"ty": "reo Tahiti",
"ug": "Уйғурче",
"uk": "українська",
"ur": "اردو",
"uz": "Ўзбек",
"ve": "tshiVenḓa",
"vi": "tiếng Việt",
"vo": "Volapük",
"wa": "walon",
"wo": "Wolof",
"xh": "isiXhosa",
"yi": "ייִדיש",
"yo": "Yorùbá",
"za": "Saɯ cueŋƅ",
"zh": "漢語; 汉语; 中文",
"zu": "isiZulu",
}
translations_folders = os.listdir(os.path.join(__dir__, "translations"))
return dict(AVAILABLE_LANGUAGES={x: possible_languages[x].capitalize() if x in possible_languages else x for x in
translations_folders},
CURRENT_LANGUAGE=session.get('lang', request.accept_languages.best_match(app.config['LANGUAGES'])))
##############################################################
# PÁGINAS
##############################################################
# Página de erro
@app.errorhandler(400)
@app.errorhandler(401)
@app.errorhandler(403)
@app.errorhandler(404)
@app.errorhandler(405)
@app.errorhandler(406)
@app.errorhandler(408)
@app.errorhandler(409)
@app.errorhandler(410)
@app.errorhandler(411)
@app.errorhandler(412)
@app.errorhandler(413)
@app.errorhandler(414)
@app.errorhandler(415)
@app.errorhandler(416)
@app.errorhandler(417)
@app.errorhandler(418)
@app.errorhandler(422)
@app.errorhandler(423)
@app.errorhandler(424)
@app.errorhandler(429)
@app.errorhandler(500)
@app.errorhandler(501)
@app.errorhandler(502)
@app.errorhandler(503)
@app.errorhandler(504)
@app.errorhandler(505)
def page_not_found(e):
username = get_username()
lang = get_locale()
return render_template('error.html',
username=username,
lang=lang,
error=e.original_exception.args[0])
# Função para exibir a tela de descrição do aplicativo
@app.route('/about')
@app.route('/sobre')
def about():
username = get_username()
lang = get_locale()
return render_template('sobre.html',
username=username,
lang=lang)
# Página inicial
@app.route('/')
@app.route('/home')
@app.route('/inicio')
@app.route('/mapa')
@app.route('/map')
def mapa():
username = get_username()
lang = get_locale()
return render_template("map.html",
username=username,
lang=lang)
@app.route('/mapa/<uf>')
@app.route('/map/<uf>')
def mapa_uf(uf):
username = get_username()
lang = get_locale()
states_qids = {"ac": "Q25263", "av": "Q210527", "ba": "Q326203", "be": "Q321455", "br": "Q373528", "cb": "Q273529",
"co": "Q244517", "ev": "Q274118", "fa": "Q244521", "gu": "Q273533", "le": "Q244512", "li": "Q207199",
"ma": "Q26253", "pa": "Q225189", "po": "Q322792", "sa": "Q244510", "se": "Q274109", "vc": "Q326214",
"vi": "Q273525", "vr": "Q379372"}
monuments = query_monuments(states_qids[uf.lower()], lang)
qids_with_image = []
qids_without_image = []
comandos = "var "
for item in monuments:
tooltip = item["label"]
tooltip_style = "{direction:'top', offset: [0, -37]}"
popup = "<span style='text-align:center'><b>" + item["label"] + "</b></span><br><br>" + "<a class='custom-link' target='_self' href='" + url_for("monumento", qid=item['item']) + "'><button class='send_button'><i class='fa-solid fa-arrow-up-from-bracket'></i> " + gettext("Ver mais informações e enviar fotografias") + "</div>"
popup_style = "{closeButton: false}"
if "imagem" in item and item["imagem"] != "No-image.png":
if "types" in item and item["types"]:
comandos += item["item"] + " = L.marker({lon: " + item["coord"][0] + ", lat: " + item["coord"][1] + "}, {icon: greenIcon})" + ".bindTooltip(\"" + tooltip + "\", " + tooltip_style + ").bindPopup(\"" + popup + "\", " + popup_style + ").on('click', markerOnClick)" + "".join(item["types"]) + ",\n"
qids_with_image.append(item["item"])
else:
comandos += item["item"] + " = L.marker({lon: " + item["coord"][0] + ", lat: " + item["coord"][1] + "}, {icon: redIcon})" + ".bindTooltip(\"" + tooltip + "\", " + tooltip_style + ").bindPopup(\"" + popup + "\", " + popup_style + ").on('click', markerOnClick).addTo(markers_without_image),\n"
qids_without_image.append(item["item"])
comandos = comandos[:-2] + ";\n"
return render_template("map_uf.html",
markers=comandos,
markers_list="[" + ",".join(list(set(qids_without_image+qids_with_image))) + "]",
bounds=uf_bounds(uf),
username=username,
lang=lang,
uf=uf)
@app.route('/mapa/<uf>/geolocalizar')
@app.route('/map/<uf>/geolocate')
def geolocate(uf):
username = get_username()
lang = get_locale()
states_qids = {"ac": "Q25263", "av": "Q210527", "ba": "Q326203", "be": "Q321455", "br": "Q373528", "cb": "Q273529",
"co": "Q244517", "ev": "Q274118", "fa": "Q244521", "gu": "Q273533", "le": "Q244512", "li": "Q207199",
"ma": "Q26253", "pa": "Q225189", "po": "Q322792", "sa": "Q244510", "se": "Q274109", "vc": "Q326214",
"vi": "Q273525", "vr": "Q379372"}
monuments, locais = query_monuments_without_coords(states_qids[uf.lower()], lang)
return render_template("geolocate.html",
bounds=uf_bounds(uf),
monuments=monuments,
locais=locais,
username=username,
lang=lang,
uf=uf)
@app.route('/mapa/sugerir', methods=['GET', 'POST'])
@app.route('/map/suggest', methods=['GET', 'POST'])
def suggest():
username = get_username()
lang = get_locale()
if request.method == "POST":
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name(os.path.join(__dir__, 'credentials.json'), scope)
client = gspread.authorize(creds)
spreadsheet = app.config["SUGGESTIONS_SPREADSHEET"]
gsheet = client.open(spreadsheet).sheet1
name = request.form.get("inputName") or ""
state = request.form.get("inputState") or ""
local = request.form.get("inputLocal") or ""
address = request.form.get("inputAddress") or ""
url = request.form.get("inputURL") or ""
comments = request.form.get("inputComments") or ""
gsheet.append_row([name, state, local, address, url, comments])
flash(gettext(u'Sua sugestão para adicionar %(val)s à lista de monumentos foi enviada com sucesso!', val=name))
uf = request.args["uf"] if "uf" in request.args else ""
return render_template("suggest.html",
username=username,
lang=lang,
uf=uf)
@app.route('/monumento/<qid>', methods=['GET', 'POST'])
@app.route('/monument/<qid>', methods=['GET', 'POST'])
def monumento(qid):
username = get_username()
lang = get_locale()
if request.method == "POST":
return send_file()
else:
metadata = query_monument(qid, lang)
if "commons_cat" in metadata and metadata["commons_cat"]:
metadata["cat_info"] = get_category_info(metadata["commons_cat"][0])
metadata["sitelinks"] = get_sitelinks(qid)
aux_lang = "pt" if lang == "pt-br" else lang
if aux_lang in metadata["sitelinks"]:
metadata["article"] = get_article(aux_lang, metadata["sitelinks"][aux_lang])
metadata["article_wiki"] = aux_lang
metadata["article_name"] = metadata["sitelinks"][aux_lang]
return render_template("item.html",
metadata=metadata,
username=username,
lang=lang)
##############################################################
# CONSULTAS E REQUISIÇÕES
##############################################################
@app.route('/postCoordinates', methods=['GET', 'POST'])
def post_coordinates():
if request.method == "POST":
jsondata = request.get_json()
item = jsondata['item']
lat = jsondata['lat']
lon = jsondata['lon']
username = get_username()
token = get_token()
params = {
"action": "wbcreateclaim",
"format": "json",
"entity": item,
"property": "P625",
"snaktype": "value",
"value": "{\"latitude\":" + str(lat) + ",\"longitude\":" + str(lon) + ",\"globe\":\"http://www.wikidata.org/entity/Q2\",\"precision\":0.000001}",
"token": token,
}
result = api_post_request(params).json()
message = gettext(r"Coordenadas inseridas com sucesso!") if result["success"] else gettext(r"Algo deu errado, atualize esta página e tente novamente. Caso o erro persista, entre em contato na seção \"Sobre\".")
answer = {"qid": item, "message": message}
return json.dumps(answer), 200
def uf_bounds(uf):
bounds = {
"ac": [[39.7260317, -25.0132019], [36.9286243, -31.2683074]],
"av": [[41.0801257, -8.0891722], [40.2789474, -8.7840289]],
"ba": [[41.8194701, -7.8106767], [41.3187983, -8.8115744]],
"be": [[38.3311027, -6.931521], [37.3189368, -8.8192763]],
"br": [[41.9925213, -6.1891593], [41.024635, -7.4318921]],
"cb": [[40.4161318, -6.8637851], [39.5373649, -8.2929448]],
"co": [[40.5203648, -7.7334139], [39.9239427, -8.9092375]],
"ev": [[39.0269315, -7.106938], [38.1546502, -8.6580211]],
"fa": [[37.5290921, -7.3948663], [36.9599682, -8.994702]],
"gu": [[41.1793283, -6.781215], [40.2299187, -7.8492267]],
"le": [[40.0897361, -8.108319], [39.2115773, -9.5162186]],
"li": [[39.3163481, -8.781861], [38.6735207, -9.5005266]],
"ma": [[33.1042108, -15.8553168], [30.1378213, -17.2659245]],
"pa": [[39.6636796, -6.9511303], [38.7514974, -8.3435606]],
"po": [[41.4719715, -7.8755793], [41.0036531, -8.7889073]],
"sa": [[39.837655, -7.8103377], [38.7314943, -9.0015496]],
"se": [[38.8430403, -8.1296963], [37.7494971, -9.2590435]],
"vc": [[42.152724, -8.0827652], [41.6064808, -8.8809556]],
"vi": [[41.2144114, -7.304573], [40.3226383, -8.3591746]],
"vr": [[41.9270958, -7.1712179], [41.1187616, -8.1185336]]
}
return bounds[uf.lower()]
@app.route('/send_file', methods=["POST"])
def send_file():
username = get_username()
status_code = "ERROR"
if request.method == "POST":
uploaded_file = request.files.getlist('uploaded_file')[0]
form = request.form
# Enviar imagem
if username:
text = build_text(form)
data = upload_file(uploaded_file, form, text)
if "error" in data and data["error"]["code"] == "fileexists-shared-forbidden":
message = gettext(u"Uma imagem com este exato título já existe. Por favor, reformule o título.")
elif "upload" in data and "warnings" in data["upload"] and "duplicate" in data["upload"]["warnings"]:
message = gettext(u"Esta imagem é uma duplicata exata da imagem https://commons.wikimedia.org/wiki/File:%(file_)s",
file_=data["upload"]["warnings"]["duplicate"][0])
elif "upload" in data and "warnings" in data["upload"] and "duplicate-archive" in data["upload"]["warnings"]:
message = gettext(u"Esta imagem é uma duplicata exata de uma outra imagem que foi deletada da base.")
elif "upload" in data and "warnings" in data["upload"] and "was-deleted" in data["upload"]["warnings"]:
message = gettext(u"Uma outra imagem costumava utilizar este mesmo título. Por favor, reformule o título.")
elif "upload" in data and "warnings" in data["upload"] and "exists" in data["upload"]["warnings"]:
message = gettext(u"Uma imagem com este exato título já existe. Por favor, reformule o título.")
#TODO:lockmanager-fail-conflict is an error that does not impact in sending the files. For now, treat as success
elif "error" in data and "code" in data["error"] and data["error"]["code"] == "lockmanager-fail-conflict":
message = gettext(u"Imagem enviada com sucesso! Verifique suas contribuições clicando em seu nome de usuário(a).") + " (lockmanager-fail-conflict)"
status_code = "SUCCESS"
elif "error" in data:
message = data["error"]["code"]
elif "upload" in data and "result" in data["upload"] and data["upload"]["result"] == "Success":
message = gettext(u"Imagem enviada com sucesso! Verifique suas contribuições clicando em seu nome de usuário(a).")
status_code = "SUCCESS"
else:
message = gettext(u"Error.")
else:
message = gettext(u'Ocorreu algum erro! Verifique o formulário e tente novamente. Caso o erro persista, '
u'por favor, reporte em https://github.com/WikiMovimentoBrasil/wlmpt/issues')
return jsonify({"message": message, "status": status_code, "filename": form["filename"]})
@app.route('/print_selection', methods=["POST"])
def print_selection():
if request.method == "POST":
jsondata = request.get_json()
items = jsondata['items']
results = query_monuments_selected(items, get_locale())
return jsonify(results), 200
##############################################################
# MAIN
##############################################################
if __name__ == '__main__':
app.run()