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 stash_image(image): self.counter += 1 with image.open() as img: content = img.read() image_type = image.content_type file_ext = image_type.split('/')[1] cf = ContentFile(content) att = Attachment() att.filename = 'img{num}.{extension}'.format( num=self.counter, extension=file_ext) att.mime_type = image_type att.document = doc att.size = cf.size att.content = cf att.file.save(att.filename, cf) return {'src': 'media/' + att.filename}
def stash_image(image): self.counter += 1 try: with image.open() as img: content = img.read() image_type = image.content_type file_ext = image_type.split('/')[1] cf = ContentFile(content) att = Attachment() att.filename = 'img{num}.{extension}'.format( num=self.counter, extension=file_ext) att.mime_type = image_type att.document = doc att.size = cf.size att.content = cf att.file.save(att.filename, cf) except KeyError: # raised when the image can't be found in the zip file return {} return {'src': 'media/' + att.filename}