55from flask import Flask , jsonify , request , escape
66from pyxform import xls2xform
77
8- app = Flask (__name__ )
9- logger = logging .getLogger (__name__ )
10-
11-
12- @app .route ("/" )
13- def index ():
14- return "Welcome to the pyxform-http! Make a POST request to '/api/v1/convert' to convert an XLSForm to an ODK XForm."
15-
16-
17- @app .route ("/api/v1/convert" , methods = ["POST" ])
18- def post ():
19-
20- xlsform_formid_fallback = sanitize (request .headers .get ("X-XlsForm-FormId-Fallback" ))
21- if xlsform_formid_fallback is None :
22- xlsform_formid_fallback = "tmp"
23-
24- with TemporaryDirectory () as temp_dir_name :
25- try :
26- with open (
27- os .path .join (temp_dir_name , xlsform_formid_fallback + ".xml" ), "w+"
28- ) as xform , open (
29- os .path .join (temp_dir_name , xlsform_formid_fallback + ".xlsx" ), "wb"
30- ) as xlsform :
31- xlsform .write (request .get_data ())
32- convert_status = xls2xform .xls2xform_convert (
33- xlsform_path = str (xlsform .name ),
34- xform_path = str (xform .name ),
35- validate = True ,
36- pretty_print = False ,
37- )
38-
39- if convert_status :
40- logger .warning (convert_status )
41-
42- if os .path .isfile (xform .name ):
43- return response (
44- status = 200 , result = xform .read (), warnings = convert_status
8+
9+ def app ():
10+ app = Flask (__name__ )
11+ logger = logging .getLogger (__name__ )
12+
13+ @app .route ("/" )
14+ def index ():
15+ return "Welcome to the pyxform-http! Make a POST request to '/api/v1/convert' to convert an XLSForm to an ODK XForm."
16+
17+ @app .route ("/api/v1/convert" , methods = ["POST" ])
18+ def post ():
19+
20+ xlsform_formid_fallback = sanitize (
21+ request .headers .get ("X-XlsForm-FormId-Fallback" )
22+ )
23+ if xlsform_formid_fallback is None :
24+ xlsform_formid_fallback = "tmp"
25+
26+ with TemporaryDirectory () as temp_dir_name :
27+ try :
28+ with open (
29+ os .path .join (temp_dir_name , xlsform_formid_fallback + ".xml" ), "w+"
30+ ) as xform , open (
31+ os .path .join (temp_dir_name , xlsform_formid_fallback + ".xlsx" ), "wb"
32+ ) as xlsform :
33+ xlsform .write (request .get_data ())
34+ convert_status = xls2xform .xls2xform_convert (
35+ xlsform_path = str (xlsform .name ),
36+ xform_path = str (xform .name ),
37+ validate = True ,
38+ pretty_print = False ,
4539 )
46- else :
47- return response (error = convert_status )
4840
49- except Exception as e :
50- logger .error (e )
51- return response (error = str (e ))
41+ if convert_status :
42+ logger .warning (convert_status )
43+
44+ if os .path .isfile (xform .name ):
45+ return response (
46+ status = 200 , result = xform .read (), warnings = convert_status
47+ )
48+ else :
49+ return response (error = convert_status )
50+
51+ except Exception as e :
52+ logger .error (e )
53+ return response (error = str (e ))
54+
55+ return app
5256
5357
5458def sanitize (string ):
@@ -60,5 +64,5 @@ def response(status=400, result=None, warnings=None, error=None):
6064
6165
6266if __name__ == "__main__" :
63- # Only for debugging while developing
64- app .run (host = "0.0.0.0" , debug = True , port = 80 )
67+ app = app ()
68+ app .run ()
0 commit comments