def test_first_metadata_value_in_ancestry_path(self): """ Test get closest unittitle element value (c04) """ pead = ParsedEad( TestParsedEad.test_dir, TestParsedEad.test_dir + 'metadata/descriptive/EAD-example1.xml') dao_elements = pead.get_dao_elements() for dao_elm in dao_elements: self.assertEqual("Record - Adams-Ayers", pead._first_md_val_ancpath(dao_elm, "unittitle"))
def test_get_dao_elements(self): """ Test get dao elements """ pead = ParsedEad( TestParsedEad.test_dir, TestParsedEad.test_dir + 'metadata/descriptive/EAD-example1.xml') dao_elements = pead.get_dao_elements() self.assertIsNotNone(dao_elements, "DAO elements must not be None") self.assertEqual(len(dao_elements), 2, "DAO elements list must have two DAO element")
def test_first_metadata_value_in_ancestry_path_c01(self): """ Test get closest unittitle element value (c01) """ pead = ParsedEad( TestParsedEad.test_dir, TestParsedEad.test_dir + 'metadata/descriptive/EAD-example4.xml') dao_elements = pead.get_dao_elements() for dao_elm in dao_elements: self.assertEqual("Correspondence", pead._first_md_val_ancpath(dao_elm, "unittitle"))
def test_first_metadata_value_in_ancestry_path_origination(self): """ Test get closest unittitle element value (c04) """ pead = ParsedEad( TestParsedEad.test_dir, TestParsedEad.test_dir + 'metadata/descriptive/EAD-example5.xml') dao_elements = pead.get_dao_elements() for dao_elm in dao_elements: self.assertEqual( "Test Agency", pead._first_md_val_ancpath(dao_elm, "origination", "ead:corpname/ead:part"))
def test_first_metadata_value_in_ancestry_path_unitdatestructured(self): """ Test get closest unittitle element value (c04) """ pead = ParsedEad( TestParsedEad.test_dir, TestParsedEad.test_dir + 'metadata/descriptive/EAD-example1.xml') dao_elements = pead.get_dao_elements() for dao_elm in dao_elements: self.assertEqual( "22.04.2016", pead._first_md_val_ancpath(dao_elm, "unitdatestructured", "ead:datesingle"))
def test_c_level(self): """ Test get closest unittitle element value (c01) """ pead = ParsedEad( TestParsedEad.test_dir, TestParsedEad.test_dir + 'metadata/descriptive/EAD-example1.xml') dao_elements = pead.get_dao_elements() for dao_elm in dao_elements: self.assertEqual( "item", pead._first_md_val_ancpath(dao_elm, "[Cc][0,1][0-9]", "level", True))
def get_basic_metadata(request, file_path): user_data_path = os.path.join(ip_data_path, request.user.username) vars = environment_variables(request) if not vars['selected_ip']: return {} selected_ip = vars['selected_ip'] print(file_path) archive_file_path = os.path.join(user_data_path, selected_ip.ip_filename) t = tarfile.open(archive_file_path, 'r') file_content = read_textfile_from_tar(t, file_path) tmp_file_path = "/tmp/%s" % randomword(10) res_events = [] try: title = "" date = "" with open(tmp_file_path, 'w') as tmp_file: tmp_file.write(file_content) if fnmatch.fnmatch(file_path, metadata_file_pattern_ead): pead = ParsedEad("/tmp", tmp_file_path) dao_elements = pead.get_dao_elements() actual = pead.ead_tree.getroot().tag unit_titles = [] unit_dates = [] for dao_elm in dao_elements: unit_titles.append( pead._first_md_val_ancpath(dao_elm, "unittitle")) unit_dates.append( pead._first_md_val_ancpath(dao_elm, "unitdate")) title = unit_titles[0] date = unit_dates[0] events = "" elif fnmatch.fnmatch(file_path, metadata_file_pattern_premis): structure = get_ip_structure(request) logical_view = search(structure, "logical_view_data") events = search(logical_view, "events") for event in events: if len(event): res_events.append({ 'type': event[0]['type'], 'datetime': event[0]['datetime'], 'agent': event[0]['linking_agent_id']['value'] }) title = "Root PREMIS" date = "20.09.2017" md_type = ead_md_type if fnmatch.fnmatch(file_path, metadata_file_pattern_ead) \ else premis_md_type if fnmatch.fnmatch(file_path, metadata_file_pattern_premis) else "Other" return JsonResponse( { 'success': True, 'type': md_type, 'title': title, 'date': date, 'events': res_events, 'file_path': file_path }, status=200) except Exception as error: logger.exception(error) return JsonResponse({ 'success': False, 'error': str(error) }, status=500)