def parse_submit_description_from_ip(ip): rootdir = os.path.dirname(ip.object_path) if os.path.isfile( ip.object_path) else ip.object_path xml = ip.package_mets_path parsed = parse_submit_description(xml, rootdir) ip.label = parsed.get('label') ip.entry_date = parsed.get('entry_date') ip.start_date = parsed.get('start_date') ip.end_date = parsed.get('end_date') if ip.policy is not None: try: parsed_policy = parsed.get('altrecordids', {}).get('POLICYID', [])[0] if ip.policy.policy_id != parsed_policy: raise ValueError( 'Policy in submit description ({}) and submission agreement ({}) does not match' .format(parsed_policy, ip.policy.policy_id)) except IndexError: pass ip.information_class = parsed.get( 'information_class') or ip.policy.information_class if ip.information_class != ip.policy.information_class: raise ValueError( 'Information class in submit description ({}) and policy ({}) does not match' .format(ip.information_class, ip.policy.information_class)) add_agents_from_xml(ip, xml) ip.save()
def run(self): ip = self.get_information_package() rootdir = os.path.dirname(ip.object_path) if os.path.isfile( ip.object_path) else ip.object_path xml = ip.package_mets_path parsed = parse_submit_description(xml, rootdir) ip.label = parsed.get('label') ip.entry_date = parsed.get('entry_date') ip.start_date = parsed.get('start_date') ip.end_date = parsed.get('end_date') if ip.policy is None: parsed_policy = parsed.get('altrecordids', {}).get('POLICYID')[0] ip.policy = ArchivePolicy.objects.get(policy_id=parsed_policy) ip.information_class = parsed.get( 'information_class') or ip.policy.information_class if ip.information_class != ip.policy.information_class: raise ValueError( 'Information class in submit description ({}) and policy ({}) does not match' .format(ip.information_class, ip.policy.information_class)) for agent_el in get_agents(etree.parse(xml)): agent = Agent.objects.from_mets_element(agent_el) ip.agents.add(agent) ip.save()
def test_information_class_with_multiple_numbers(self): self.xmlfile.write(b''' <mets INFORMATIONCLASS="123 456"> <metsHdr CREATEDATE="456"></metsHdr> </mets> ''') self.xmlfile.close() ip = parse_submit_description(self.xmlfile.name) self.assertEqual(ip['information_class'], 123)
def test_no_objid(self): self.xmlfile.write(b''' <mets> <metsHdr CREATEDATE="456"></metsHdr> </mets> ''') self.xmlfile.close() ip = parse_submit_description(self.xmlfile.name) self.assertEqual(ip['id'], os.path.splitext(os.path.basename(self.xmlfile.name))[0])
def test_objid_with_prefix(self): self.xmlfile.write(b''' <mets OBJID="ID:123"> <metsHdr CREATEDATE="456"></metsHdr> </mets> ''') self.xmlfile.close() ip = parse_submit_description(self.xmlfile.name) self.assertEqual(ip['id'], '123')
def test_information_class_in_root_and_altrecordid(self): self.xmlfile.write(b''' <mets INFORMATIONCLASS="123"> <metsHdr CREATEDATE="456"></metsHdr> <altRecordID TYPE="INFORMATIONCLASS">456</altRecordID> </mets> ''') self.xmlfile.close() ip = parse_submit_description(self.xmlfile.name) self.assertEqual(ip['information_class'], 123)
def test_objid_and_create_date(self): self.xmlfile.write(b''' <mets OBJID="123"> <metsHdr CREATEDATE="456"></metsHdr> </mets> ''') self.xmlfile.close() ip = parse_submit_description(self.xmlfile.name) self.assertEqual(ip['id'], '123') self.assertEqual(ip['create_date'], '456')
def test_information_class_with_letters(self): self.xmlfile.write(''' <root INFORMATIONCLASS="class 123"> <metsHdr CREATEDATE="456"></metsHdr> </root> ''') self.xmlfile.close() ip = parse_submit_description(self.xmlfile.name) self.assertEqual(ip['information_class'], 123)
def test_objpath(self, mock_objectpath, mock_os_stat): mock_objectpath.return_value = 'foo' mock_os_stat.return_value = mock.Mock(**{'st_size': 24}) self.xmlfile.write(b''' <mets OBJID="123"> <metsHdr CREATEDATE="456"></metsHdr> </mets> ''') self.xmlfile.close() ip = parse_submit_description(self.xmlfile.name) self.assertEqual(ip['id'], '123') self.assertEqual(ip['create_date'], '456') self.assertEqual(ip['object_path'], 'foo') self.assertEqual(ip['object_size'], 24)
def run(self, sip_path): sip_path, = self.parse_params(sip_path) sip_path = pathlib.Path(sip_path) user = User.objects.get(pk=self.responsible) perms = copy.deepcopy(getattr(settings, 'IP_CREATION_PERMS_MAP', {})) organization = user.user_profile.current_organization object_identifier_value = sip_path.stem existing_sip = InformationPackage.objects.filter( Q( Q(object_path=sip_path) | Q(object_identifier_value=object_identifier_value), ), package_type=InformationPackage.SIP).first() xmlfile = sip_path.with_suffix('.xml') if existing_sip is None: parsed = parse_submit_description(xmlfile.as_posix(), srcdir=sip_path.parent) parsed_sa = parsed.get('altrecordids', {}).get('SUBMISSIONAGREEMENT', [None])[0] if parsed_sa is not None: raise ValueError('No submission agreement found in xml') sa = SubmissionAgreement.objects.get(pk=parsed_sa) with transaction.atomic(): ip = InformationPackage.objects.create( object_identifier_value=object_identifier_value, sip_objid=object_identifier_value, sip_path=sip_path.as_posix(), package_type=InformationPackage.AIP, state='Prepared', responsible=user, submission_agreement=sa, submission_agreement_locked=True, object_path=sip_path.as_posix(), package_mets_path=xmlfile.as_posix(), ) member = Member.objects.get(django_user=user) user_perms = perms.pop('owner', []) organization.assign_object(ip, custom_permissions=perms) organization.add_object(ip) for perm in user_perms: perm_name = get_permission_name(perm, ip) assign_perm(perm_name, member.django_user, ip) # refresh date fields to convert them to datetime instances instead of # strings to allow further datetime manipulation ip.refresh_from_db( fields=['entry_date', 'start_date', 'end_date']) ip.create_profile_rels( [x.lower().replace(' ', '_') for x in profile_types], user) else: with transaction.atomic(): ip = existing_sip ip.sip_objid = object_identifier_value ip.sip_path = sip_path.as_posix() ip.package_type = InformationPackage.AIP ip.responsible = user ip.state = 'Prepared' ip.object_path = sip_path.as_posix() ip.package_mets_path = xmlfile.as_posix() ip.save() return str(ip.pk)