@@ -32,3 +32,35 @@ def task_process_xml_document(self, xml_id, user_id=None, username=None):
3232 task_generate_html_file .delay (xml_id , user_id = user_id , username = username )
3333
3434 return True
35+
36+
37+ @celery_app .task (bind = True , name = _ ('Validate XML Document' ), timelimit = - 1 )
38+ def task_validate_xml_file (self , xml_id , user_id = None , username = None ):
39+ user = _get_user (self .request , username = username , user_id = user_id )
40+
41+ try :
42+ xml_file = XMLDocument .objects .get (id = xml_id )
43+ except XMLDocument .DoesNotExist :
44+ logging .error (f'XML file with ID { xml_id } does not exist.' )
45+ return False
46+
47+ logging .info (f'Starting XML validation for XML file { xml_file .xml_file .name } .' )
48+ params = {}
49+
50+ temp_dir = tempfile .gettempdir ()
51+ base_filename = os .path .splitext (os .path .basename (xml_file .xml_file .name ))[0 ]
52+
53+ path_csv = os .path .join (temp_dir , f"{ base_filename } .validation.csv" )
54+ path_exceptions = os .path .join (temp_dir , f"{ base_filename } .exceptions.json" )
55+
56+ try :
57+ validator = data_checker .XMLDataChecker (path_csv , path_exceptions , xml_file .xml_file .path )
58+ validator .validate (params = params , csv_per_xml = False )
59+ except Exception as e :
60+ logging .error (f'Error during XML validation: { e } ' )
61+ return False
62+
63+ xml_file .validation_file = path_csv
64+ xml_file .exceptions_file = path_exceptions
65+ xml_file .save ()
66+ logging .info (f'XML validation completed successfully for { xml_file .xml_file .name } .' )
0 commit comments