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 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 analyse_after_import(self, doc): """ Run analysis after import. Usually only used on PDF documents. """ finder = plugins.for_document('refs', doc) if finder: finder.find_references_in_document(doc)
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 resolve_element(self, element): self.element = element # lookup TOC information builder = plugins.for_document('toc', self.document) if builder: self.toc_entry = builder.table_of_contents_entry_for_element(self.document, self.element) self.is_toc_element = self.toc_entry and self.toc_entry.element == self.element
def find_references(self, document): finder = plugins.for_document('refs', document) if finder: finder.find_references_in_document(document) finder = plugins.for_document('refs-subtypes', document) if finder: finder.find_references_in_document(document) finder = plugins.for_document('refs-cap', document) if finder: finder.find_references_in_document(document) finder = plugins.for_document('refs-act-names', document) if finder: finder.find_references_in_document(document) finder = plugins.for_document('internal-refs', document) if finder: finder.find_references_in_document(document)
def analyse_after_import(self, doc): """ Run analysis after import. Usually only used on PDF documents. """ finder = plugins.for_document('refs', doc) if finder: finder.find_references_in_document(doc) finder = plugins.for_document('refs-subtypes', doc) if finder: finder.find_references_in_document(doc) finder = plugins.for_document('refs-cap', doc) if finder: finder.find_references_in_document(doc) finder = plugins.for_document('refs-act-names', doc) if finder: finder.find_references_in_document(doc) finder = plugins.for_document('internal-refs', doc) if finder: finder.find_references_in_document(doc) italics_terms_finder = plugins.for_document('italics-terms', doc) italics_terms = doc.work.country.italics_terms if italics_terms_finder and italics_terms: italics_terms_finder.mark_up_italics_in_document( doc, italics_terms)
def commenceable_provisions(self): """ Return a list of TOCElement objects that can be commenced. """ # gather documents and sort so that we consider primary language documents first documents = self.expressions().all() documents = sorted( documents, key=lambda d: 0 if d.language == self.country.primary_language else 1) # get all the docs and combine the TOCs, based on element IDs provisions = [] id_set = set() for doc in documents: plugin = plugins.for_document('toc', doc) if plugin: plugin.insert_commenceable_provisions(doc, provisions, id_set) return provisions
def commenceable_provisions(self): """ Return a list of TOCElement objects that can be commenced. """ # gather documents and sort so that we consider primary language documents first documents = self.expressions().all() documents = sorted(documents, key=lambda d: 0 if d.language == self.country.primary_language else 1) # get all the docs and combine the TOCs, based on element IDs provisions = [] id_set = set() for doc in documents: plugin = plugins.for_document('toc', doc) if plugin: toc = plugin.table_of_contents_for_document(doc) for item in plugin.commenceable_items(toc): if item.id and item.id not in id_set: id_set.add(item.id) provisions.append(item) return provisions
def table_of_contents(self): builder = plugins.for_document('toc', self) return builder.table_of_contents_for_document(self)
def mark_up_italics(self, document): italics_terms_finder = plugins.for_document('italics-terms', document) italics_terms = document.work.country.italics_terms if italics_terms_finder and italics_terms: italics_terms_finder.mark_up_italics_in_document( document, italics_terms)
def link_terms(self, doc): finder = plugins.for_document('terms', doc) if finder: finder.find_terms_in_document(doc)
def find_references(self, document): finder = plugins.for_document('refs', document) if finder: finder.find_references_in_document(document)
def get_context_data(self, **kwargs): context = super(DocumentDetailView, self).get_context_data(**kwargs) doc = self.object context['work'] = doc.work context['work_json'] = json.dumps( WorkSerializer(instance=doc.work, context={ 'request': self.request }).data) context['document_json'] = json.dumps( DocumentSerializer(instance=doc, context={ 'request': self.request }).data) # expressions context['expressions_json'] = json.dumps( DocumentSerializer(context={ 'request': self.request }, many=True).to_representation(doc.work.expressions().all())) context['comparison_expressions'] = doc.work.expressions().filter( language=doc.language).order_by('-expression_date') context['place'] = doc.work.place context['country'] = doc.work.country context['locality'] = doc.work.locality # TODO do this in a better place context['countries'] = Country.objects.select_related( 'country').prefetch_related('localities', 'publication_set', 'country').all() context['document_content_json'] = json.dumps(doc.document_xml) # add 'numbered_title_localised' to each amendment amendments = WorkAmendmentSerializer(context={'request': self.request}, many=True)\ .to_representation(doc.work.amendments) plugin = plugins.for_document('work-detail', doc) if plugin: for a in amendments: amending_work = Work.objects.get( frbr_uri=a['amending_work']['frbr_uri']) a['amending_work'][ 'numbered_title_localised'] = plugin.work_numbered_title( amending_work) context['amendments_json'] = json.dumps(amendments) context['form'] = DocumentForm(instance=doc) context['subtypes'] = Subtype.objects.order_by('name').all() context['user_can_edit'] = ( self.request.user.is_authenticated and self.request.user.has_perm('indigo_api.change_document') and self.request.user.editor.has_country_permission(doc.work.country)) context['download_formats'] = [{ 'url': reverse('document-detail', kwargs={ 'pk': doc.id, 'format': r.format }) + getattr(r, 'suffix', ''), 'icon': r.icon, 'title': r.title } for r in DocumentViewSet.renderer_classes if hasattr(r, 'icon')] context['download_formats'].sort(key=lambda f: f['title']) return context