5
5
"""
6
6
7
7
from functools import wraps
8
- from flask import request , Blueprint
8
+ from flask import current_app , request , redirect , Blueprint
9
9
from service import app , normalizer , formatter
10
+ from service import names as N
10
11
11
12
12
13
def disable_cache (f ):
@@ -29,9 +30,39 @@ def decorated_func(*args, **kwargs):
29
30
return decorated_func
30
31
31
32
33
+ def add_complete_downloads (bp , urls ):
34
+ """Agrega endpoints de descarga completa de datos a un Flask Blueprint.
35
+
36
+ Args:
37
+ bp (flask.Blueprint): Objeto donde agregar los endpoints de descarga.
38
+ urls (dict): Diccionario con tipos de entidades como claves, y
39
+ diccionarios como valores. Cada subdiccionario debe contener, por
40
+ cada formato (CSV, JSON, GEOJSON), una URL a donde redirigir (o
41
+ None para no agregar el endpoint). Ver el archivo
42
+ georef.example.cfg para más detalles.
43
+
44
+ """
45
+ entities = [N .STATES , N .DEPARTMENTS , N .MUNICIPALITIES , N .LOCALITIES ,
46
+ N .STREETS ]
47
+ formats = ['json' , 'csv' , 'geojson' ]
48
+
49
+ for entity in entities :
50
+ entity_urls = urls [entity ]
51
+
52
+ for fmt in formats :
53
+ if entity_urls .get (fmt ):
54
+ url = entity_urls [fmt ]
55
+ # e.g: /provincias.csv
56
+ endpoint = '{}-{}' .format (entity , fmt )
57
+ rule = '/{}.{}' .format (entity , fmt )
58
+
59
+ bp .add_url_rule (rule , endpoint ,
60
+ lambda location = url : redirect (location ))
61
+
62
+
32
63
@app .errorhandler (404 )
33
64
def handle_404 (_ ):
34
- return formatter .create_404_error_response (app . url_map )
65
+ return formatter .create_404_error_response ()
35
66
36
67
37
68
@app .errorhandler (405 )
@@ -42,6 +73,8 @@ def handle_405(_):
42
73
# API v1.0
43
74
bp_v1_0 = Blueprint ('georef_v1.0' , __name__ )
44
75
76
+ add_complete_downloads (bp_v1_0 , current_app .config ['COMPLETE_DOWNLOAD_URLS' ])
77
+
45
78
46
79
@bp_v1_0 .route ('/provincias' , methods = ['GET' , 'POST' ])
47
80
def get_states ():
0 commit comments