def set_publication_details(apps, schema_editor): Document = apps.get_model("indigo_api", "Document") db_alias = schema_editor.connection.alias for doc in Document.objects.using(db_alias).all(): a = Act(doc.document_xml) doc.publication_number = a.publication_number doc.publication_name = a.publication_name doc.publication_date = a.publication_date doc.save()
def migrate_xml(self, document_xml, frbr_uri, forward): # Create a cobalt StructuredDocument from the document's existing XML cobalt_doc = Act(document_xml) # Update the document's FRBR URI in the XML (meta/identification block) cobalt_doc.frbr_uri = frbr_uri # Add `/akn` prefix (if forward=True; remove if forward=False) # to hrefs in <ref> or <passiveRef> AKN elements, e.g. # href="/za/act/2012/22" <--> href="/akn/za/act/2012/22" for node in cobalt_doc.root.xpath( "//a:*[self::a:ref or self::a:passiveRef][starts-with(@href, '/')]", namespaces={'a': cobalt_doc.namespace}): ref = node.get('href') if FRBR_URI_RE.match(ref): ref = self.new_frbr_uri(ref, forward) node.set('href', ref) return cobalt_doc.to_xml().decode('utf-8')
def test_migration_without_heading(self): migration = ScheduleArticleToHcontainer() xml = OLD_SCHEDULE % (""" <paragraph id="schedule2.paragraph-0"> <content> <p>None</p> </content> </paragraph>""") act = Act(xml) migration.migrate_act(act) expected = NEW_SCHEDULE % """ <heading>Schedule</heading><paragraph id="schedule2.paragraph-0"> <content> <p>None</p> </content> </paragraph>""" self.assertMultiLineEqual( expected, etree.tostring(act.root, encoding='utf-8', pretty_print=True).decode('utf-8'))
def validate_content(self, value): try: Act(value) except LxmlError as e: raise ValidationError("Invalid XML: %s" % str(e)) return value