def import_docx_file(self, user, work, date, language, docx_file, filesize): document = Document() document.work = work document.expression_date = date document.language = language document.created_by_user = user document.save() importer = plugins.for_document('importer', document) # hard-coded for Namibian docxes importer.section_number_position = 'after-title' upload = UploadedFile(file=docx_file, content_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document', size=filesize) try: importer.create_from_upload(upload, document, None) except ValueError as e: print("Error during import: %s" % e.message) raise ValidationError(e.message or "error during import") docx_file.seek(0) filename = os.path.split(docx_file.name)[1] att = Attachment() att.filename = filename att.mime_type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' att.document = document att.size = filesize att.file.save(att.filename, docx_file) document.updated_by_user = user document.save_with_revision(user) self.create_review_task(document, user, filename)
def test_get_subcomponent(self): d = Document(language=self.eng) d.work = self.work d.content = document_fixture(xml=""" <body xmlns="http://www.akomantoso.org/2.0"> <section id="section-1"> <num>1.</num> <heading>Foo</heading> <content> <p>hello</p> </content> </section> <chapter id="chapter-2"> <num>2.</num> <heading>The Chapter</heading> <content> <p>hi</p> </content> </chapter> </body> """) assert_is_not_none(d.doc.components()['main']) elem = d.get_subcomponent('main', 'chapter/2') assert_equal(elem.get('id'), "chapter-2") elem = d.get_subcomponent('main', 'section/1') assert_equal(elem.get('id'), "section-1") assert_is_none(d.get_subcomponent('main', 'chapter/99')) assert_is_none(d.get_subcomponent('main', 'section/99'))
def form_valid(self, form): data = form.cleaned_data upload = data['file'] opts = data.get('options', {}) document = Document() document.work = self.work document.expression_date = data['expression_date'] document.language = data['language'] document.created_by_user = self.request.user document.save() importer = plugins.for_document('importer', document) importer.section_number_position = opts.get('section_number_position', 'guess') importer.cropbox = opts.get('cropbox', None) try: importer.create_from_upload(upload, document, self.request) except ValueError as e: log.error("Error during import: %s" % e.message, exc_info=e) raise ValidationError(e.message or "error during import") document.updated_by_user = self.request.user document.save_with_revision(self.request.user) # add source file as an attachment AttachmentSerializer(context={ 'document': document }).create({'file': upload}) return JsonResponse( {'location': reverse('document', kwargs={'doc_id': document.id})})
def form_valid(self, form): data = form.cleaned_data upload = data['file'] opts = data.get('options', {}) document = Document() document.work = self.work document.expression_date = data['expression_date'] document.language = data['language'] document.created_by_user = self.request.user document.save() importer = plugins.for_document('importer', document) importer.section_number_position = opts.get('section_number_position', 'guess') importer.cropbox = opts.get('cropbox', None) importer.page_nums = form.cleaned_data['page_nums'] try: importer.create_from_upload(upload, document, self.request) except ValueError as e: log.error("Error during import: %s" % str(e), exc_info=e) return JsonResponse({'file': str(e) or "error during import"}, status=400) document.updated_by_user = self.request.user document.save_with_revision(self.request.user) return JsonResponse( {'location': reverse('document', kwargs={'doc_id': document.id})})
def test_preamble_and_friends_in_table_of_contents(self): d = Document() d.work = self.work d.content = document_fixture(xml=""" <coverpage> <content><p>hi</p></content> </coverpage> <preface> <content><p>hi</p></content> </preface> <preamble> <content><p>hi</p></content> </preamble> <body> <content><p>hi</p></content> </body> <conclusions> <content><p>hi></p></content> </conclusions> """) d.language = Language.objects.get(language__pk='en') toc = d.table_of_contents() toc = [t.as_dict() for t in toc] self.maxDiff = None self.assertEqual(toc, [ {'type': 'coverpage', 'component': 'main', 'subcomponent': 'coverpage', 'title': 'Coverpage'}, {'type': 'preface', 'component': 'main', 'subcomponent': 'preface', 'title': 'Preface'}, {'type': 'preamble', 'component': 'main', 'subcomponent': 'preamble', 'title': 'Preamble'}, {'type': 'conclusions', 'component': 'main', 'subcomponent': 'conclusions', 'title': 'Conclusions'}, ])
def test_subpart_without_number(self): d = Document() d.work = self.work d.content = document_fixture(xml=""" <body> <subpart eId="subpart_1"> <heading>My subpart</heading> </subpart> </body> """) d.language = Language.objects.get(language__pk='en') toc = d.table_of_contents() toc = [t.as_dict() for t in toc] self.maxDiff = None self.assertEqual([ { 'type': 'subpart', 'component': 'main', 'subcomponent': 'subpart', 'title': 'My subpart', 'heading': 'My subpart', 'id': 'subpart_1' }, ], toc)
def test_set_content(self): d = Document() d.work = self.work d.content = document_fixture(u'γνωρίζω') assert_equal(d.frbr_uri, '/za/act/1900/1') assert_equal(d.country, 'za') assert_equal(d.doc.publication_date, date(2005, 7, 24))
def test_table_of_contents_afr(self): d = Document() d.work = self.work d.content = document_fixture(xml=""" <body xmlns="http://www.akomantoso.org/2.0"> <section id="section-1"> <num>1.</num> <heading>Foo</heading> <content> <p>hello</p> </content> </section> <chapter id="chapter-1"> <num>1.</num> <heading>The Chapter</heading> <part id="part-A"> <num>A</num> <heading>The Part</heading> <section id="section-2"> <num>2.</num> <heading>Other</heading> <content> <p>hi</p> </content> </section> </part> </chapter> </body> """) d.language = Language.objects.get(language__pk='af') toc = d.table_of_contents() toc = [t.as_dict() for t in toc] self.maxDiff = None self.assertEqual(toc, [ {'id': 'section-1', 'num': '1.', 'type': 'section', 'heading': 'Foo', 'component': 'main', 'subcomponent': 'section/1', 'title': '1. Foo'}, {'id': 'chapter-1', 'num': '1.', 'type': 'chapter', 'heading': 'The Chapter', 'component': 'main', 'subcomponent': 'chapter/1', 'title': 'Hoofstuk 1. – The Chapter', 'children': [ {'id': 'part-A', 'num': 'A', 'type': 'part', 'heading': 'The Part', 'component': 'main', 'subcomponent': 'chapter/1/part/A', 'title': 'Deel A – The Part', 'children': [ {'id': 'section-2', 'num': '2.', 'type': 'section', 'heading': 'Other', 'component': 'main', 'subcomponent': 'section/2', 'title': '2. Other'}, ] }, ]}, ])
def test_component_table_of_contents(self): d = Document() d.work = self.work d.content = """<akomaNtoso xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.akomantoso.org/2.0" xsi:schemaLocation="http://www.akomantoso.org/2.0 akomantoso20.xsd"> <act contains="singleVersion"> <meta> <identification source="#openbylaws"> <FRBRWork> <FRBRthis value="/za/by-law/cape-town/2002/community-fire-safety/main"/> <FRBRuri value="/za/by-law/cape-town/2002/community-fire-safety"/> <FRBRalias value="Community Fire Safety By-law"/> <FRBRdate date="2002-02-28" name="Generation"/> <FRBRauthor href="#council" as="#author"/> <FRBRcountry value="za"/> </FRBRWork> <FRBRExpression> <FRBRthis value="/za/by-law/cape-town/2002/community-fire-safety/main/eng@"/> <FRBRuri value="/za/by-law/cape-town/2002/community-fire-safety/eng@"/> <FRBRdate date="2002-02-28" name="Generation"/> <FRBRauthor href="#council" as="#author"/> <FRBRlanguage language="eng"/> </FRBRExpression> <FRBRManifestation> <FRBRthis value="/za/by-law/cape-town/2002/community-fire-safety/main/eng@"/> <FRBRuri value="/za/by-law/cape-town/2002/community-fire-safety/eng@"/> <FRBRdate date="2014-01-14" name="Generation"/> <FRBRauthor href="#openbylaws" as="#author"/> </FRBRManifestation> </identification> </meta> <body></body> </act> <components> <component id="component-1"> <doc name="schedule1"> <meta> <identification source="#slaw"> <FRBRWork> <FRBRthis value="/za/act/1980/01/schedule1"/> <FRBRuri value="/za/act/1980/01"/> <FRBRalias value="A Title"/> <FRBRdate date="1980-01-01" name="Generation"/> <FRBRauthor href="#council" as="#author"/> <FRBRcountry value="za"/> </FRBRWork> <FRBRExpression> <FRBRthis value="/za/act/1980/01/eng@/schedule1"/> <FRBRuri value="/za/act/1980/01/eng@"/> <FRBRdate date="1980-01-01" name="Generation"/> <FRBRauthor href="#council" as="#author"/> <FRBRlanguage language="eng"/> </FRBRExpression> <FRBRManifestation> <FRBRthis value="/za/act/1980/01/eng@/schedule1"/> <FRBRuri value="/za/act/1980/01/eng@"/> <FRBRdate date="2015-04-22" name="Generation"/> <FRBRauthor href="#slaw" as="#author"/> </FRBRManifestation> </identification> </meta> <mainBody> <section id="schedule-1.section-0"> <content> <p>1. Foo</p> <p>2. Bar</p> </content> </section> </mainBody> </doc> </component> <component id="component-2"> <doc name="schedule2"> <meta> <identification source="#slaw"> <FRBRWork> <FRBRthis value="/za/act/1980/01/schedule2"/> <FRBRuri value="/za/act/1980/01"/> <FRBRdate date="1980-01-01" name="Generation"/> <FRBRauthor href="#council" as="#author"/> <FRBRcountry value="za"/> </FRBRWork> <FRBRExpression> <FRBRthis value="/za/act/1980/01/eng@/schedule2"/> <FRBRuri value="/za/act/1980/01/eng@"/> <FRBRdate date="1980-01-01" name="Generation"/> <FRBRauthor href="#council" as="#author"/> <FRBRlanguage language="eng"/> </FRBRExpression> <FRBRManifestation> <FRBRthis value="/za/act/1980/01/eng@/schedule2"/> <FRBRuri value="/za/act/1980/01/eng@"/> <FRBRdate date="2015-04-22" name="Generation"/> <FRBRauthor href="#slaw" as="#author"/> </FRBRManifestation> </identification> </meta> <mainBody> <section id="schedule-2.section-0"> <content> <p>Baz</p> <p>Boom</p> </content> </section> </mainBody> </doc> </component> </components> </akomaNtoso> """ d.language = Language.objects.get(language__pk='en') toc = d.table_of_contents() toc = [t.as_dict() for t in toc] self.maxDiff = None self.assertEqual([ { 'component': 'schedule1', 'type': 'doc', 'subcomponent': None, 'heading': 'A Title', 'title': 'A Title', 'children': [{ 'component': 'schedule1', 'type': 'section', 'id': 'schedule-1.section-0', 'subcomponent': 'section', 'title': 'Section' }] }, { 'component': 'schedule2', 'type': 'doc', 'subcomponent': None, 'heading': 'Schedule 2', 'title': 'Schedule 2', 'children': [{ 'component': 'schedule2', 'type': 'section', 'id': 'schedule-2.section-0', 'subcomponent': 'section', 'title': 'Section' }] }, ], toc)
def test_component_table_of_contents(self): d = Document() d.work = self.work d.content = """<akomaNtoso xmlns="http://docs.oasis-open.org/legaldocml/ns/akn/3.0"> <act contains="singleVersion"> <meta> <identification source="#openbylaws"> <FRBRWork> <FRBRthis value="/za/by-law/cape-town/2002/community-fire-safety/!main"/> <FRBRuri value="/za/by-law/cape-town/2002/community-fire-safety"/> <FRBRalias value="Community Fire Safety By-law"/> <FRBRdate date="2002-02-28" name="Generation"/> <FRBRauthor href="#council" as="#author"/> <FRBRcountry value="za"/> </FRBRWork> <FRBRExpression> <FRBRthis value="/za/by-law/cape-town/2002/community-fire-safety/main/eng@"/> <FRBRuri value="/za/by-law/cape-town/2002/community-fire-safety/eng@"/> <FRBRdate date="2002-02-28" name="Generation"/> <FRBRauthor href="#council" as="#author"/> <FRBRlanguage language="eng"/> </FRBRExpression> <FRBRManifestation> <FRBRthis value="/za/by-law/cape-town/2002/community-fire-safety/main/eng@"/> <FRBRuri value="/za/by-law/cape-town/2002/community-fire-safety/eng@"/> <FRBRdate date="2014-01-14" name="Generation"/> <FRBRauthor href="#openbylaws" as="#author"/> </FRBRManifestation> </identification> </meta> <body></body> <attachments> <attachment eId="att_1"> <heading>A Title</heading> <doc name="schedule"> <meta> <identification source="#slaw"> <FRBRWork> <FRBRthis value="/za/act/1980/01/!schedule1"/> <FRBRuri value="/za/act/1980/01"/> <FRBRalias value="A Title"/> <FRBRdate date="1980-01-01" name="Generation"/> <FRBRauthor href="#council" as="#author"/> <FRBRcountry value="za"/> </FRBRWork> <FRBRExpression> <FRBRthis value="/za/act/1980/01/eng@/schedule1"/> <FRBRuri value="/za/act/1980/01/eng@"/> <FRBRdate date="1980-01-01" name="Generation"/> <FRBRauthor href="#council" as="#author"/> <FRBRlanguage language="eng"/> </FRBRExpression> <FRBRManifestation> <FRBRthis value="/za/act/1980/01/eng@/schedule1"/> <FRBRuri value="/za/act/1980/01/eng@"/> <FRBRdate date="2015-04-22" name="Generation"/> <FRBRauthor href="#slaw" as="#author"/> </FRBRManifestation> </identification> </meta> <mainBody> <section eId="sec_1"> <content> <p>1. Foo</p> <p>2. Bar</p> </content> </section> </mainBody> </doc> </attachment> <attachment eId="att_2"> <heading>Schedule 2</heading> <doc name="schedule"> <meta> <identification source="#slaw"> <FRBRWork> <FRBRthis value="/za/act/1980/01/!schedule2"/> <FRBRuri value="/za/act/1980/01"/> <FRBRdate date="1980-01-01" name="Generation"/> <FRBRauthor href="#council" as="#author"/> <FRBRcountry value="za"/> </FRBRWork> <FRBRExpression> <FRBRthis value="/za/act/1980/01/eng@/schedule2"/> <FRBRuri value="/za/act/1980/01/eng@"/> <FRBRdate date="1980-01-01" name="Generation"/> <FRBRauthor href="#council" as="#author"/> <FRBRlanguage language="eng"/> </FRBRExpression> <FRBRManifestation> <FRBRthis value="/za/act/1980/01/eng@/schedule2"/> <FRBRuri value="/za/act/1980/01/eng@"/> <FRBRdate date="2015-04-22" name="Generation"/> <FRBRauthor href="#slaw" as="#author"/> </FRBRManifestation> </identification> </meta> <mainBody> <section eId="sec_1"> <content> <p>Baz</p> <p>Boom</p> </content> </section> </mainBody> </doc> </attachment> </attachments> </act> </akomaNtoso> """ d.language = Language.objects.get(language__pk='en') toc = d.table_of_contents() toc = [t.as_dict() for t in toc] self.maxDiff = None self.assertEqual([{ 'component': 'schedule1', 'type': 'attachment', 'subcomponent': None, 'id': 'att_1', 'heading': 'A Title', 'title': 'A Title', 'children': [{ 'component': 'schedule1', 'type': 'section', 'id': 'sec_1', 'subcomponent': 'section', 'title': 'Section', }] }, { 'component': 'schedule2', 'type': 'attachment', 'subcomponent': None, 'id': 'att_2', 'heading': 'Schedule 2', 'title': 'Schedule 2', 'children': [{ 'component': 'schedule2', 'type': 'section', 'id': 'sec_1', 'subcomponent': 'section', 'title': 'Section' }] }], toc)