11import os
22import shutil
33import tempfile
4+ import shutil
45import urllib .request
56
67import connexion
7- from flask import current_app as app , request , g
8+ from flask import current_app as app , request , g , jsonify
89
910from schematic import CONFIG
1011
1112from schematic .manifest .generator import ManifestGenerator
13+ from schematic .models .metadata import MetadataModel
1214
1315
1416# def before_request(var1, var2):
1820# # Do stuff after your route executes
1921# pass
2022
21- # @before_request
22- def get_manifest_route (schema_url , title , oauth , use_annotations ):
23- # check if file exists at the path created, i.e., app.config['SCHEMATIC_CONFIG']
23+
24+ def config_handler ():
2425 path_to_config = app .config ["SCHEMATIC_CONFIG" ]
2526
27+ # check if file exists at the path created, i.e., app.config['SCHEMATIC_CONFIG']
2628 if os .path .isfile (path_to_config ):
2729 CONFIG .load_config (path_to_config )
2830 else :
2931 raise FileNotFoundError (
3032 f"No configuration file was found at this path: { path_to_config } "
3133 )
3234
35+
36+ def get_temp_jsonld (schema_url ):
3337 # retrieve a JSON-LD via URL and store it in a temporary location
3438 with urllib .request .urlopen (schema_url ) as response :
3539 with tempfile .NamedTemporaryFile (delete = False , suffix = ".jsonld" ) as tmp_file :
3640 shutil .copyfileobj (response , tmp_file )
3741
3842 # get path to temporary JSON-LD file
39- jsonld = tmp_file .name
43+ return tmp_file .name
44+
45+
46+ # @before_request
47+ def get_manifest_route (schema_url , title , oauth , use_annotations ):
48+ # call config_handler()
49+ config_handler ()
50+
51+ # get path to temporary JSON-LD file
52+ jsonld = get_temp_jsonld (schema_url )
4053
4154 # request.data[]
4255 data_type = connexion .request .args ["data_type" ]
@@ -56,3 +69,63 @@ def get_manifest_route(schema_url, title, oauth, use_annotations):
5669 result = manifest_generator .get_manifest (sheet_url = True , dataset_id = dataset_id )
5770
5871 return result
72+
73+
74+ def validate_manifest_route (schema_url , data_type ):
75+ # call config_handler()
76+ config_handler ()
77+
78+ manifest_file = connexion .request .files ["csv_file" ]
79+
80+ # save contents of incoming manifest CSV file to temp file
81+ temp_dir = tempfile .gettempdir ()
82+ # path to temp file where manifest file contents will be saved
83+ temp_path = os .path .join (temp_dir , manifest_file .filename )
84+ # save content
85+ manifest_file .save (temp_path )
86+
87+ # get path to temporary JSON-LD file
88+ jsonld = get_temp_jsonld (schema_url )
89+
90+ metadata_model = MetadataModel (
91+ inputMModelLocation = jsonld , inputMModelLocationType = "local"
92+ )
93+
94+ errors = metadata_model .validateModelManifest (
95+ manifestPath = temp_path , rootNode = data_type
96+ )
97+
98+ return errors
99+
100+
101+ def submit_manifest_route (schema_url ):
102+ # call config_handler()
103+ config_handler ()
104+
105+ manifest_file = connexion .request .files ["csv_file" ]
106+
107+ # save contents of incoming manifest CSV file to temp file
108+ temp_dir = tempfile .gettempdir ()
109+ # path to temp file where manifest file contents will be saved
110+ temp_path = os .path .join (temp_dir , manifest_file .filename )
111+ # save content
112+ manifest_file .save (temp_path )
113+
114+ # get path to temporary JSON-LD file
115+ jsonld = get_temp_jsonld (schema_url )
116+
117+ dataset_id = connexion .request .args ["dataset_id" ]
118+
119+ data_type = connexion .request .args ["data_type" ]
120+
121+ metadata_model = MetadataModel (
122+ inputMModelLocation = jsonld , inputMModelLocationType = "local"
123+ )
124+
125+ success = metadata_model .submit_metadata_manifest (
126+ manifest_path = temp_path ,
127+ dataset_id = dataset_id ,
128+ validate_component = data_type ,
129+ )
130+
131+ return success
0 commit comments