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 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) 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})})