Esempio n. 1
0
    def test_update_attachment(self):
        # create an attachment for a doc
        work = Work.objects.get_for_frbr_uri('/akn/za/act/2014/10')
        doc = work.expressions().first()

        attachment = Attachment(document=doc)
        attachment.filename = "foo.txt"
        attachment.size = 100
        attachment.mime_type = "text/plain"
        attachment.file.save("foo.txt", ContentFile("foo"))
        attachment.save()

        # check the attachment
        response = self.client.get('/api/documents/%s/attachments' % doc.id)
        assert_equal(response.status_code, 200)
        data = response.data['results'][0]
        assert_equal(data['mime_type'], 'text/plain')

        # test patch
        data['filename'] = 'new.txt'
        response = self.client.patch(data['url'], data)
        assert_equal(response.status_code, 200)
        assert_equal(response.data['filename'], 'new.txt')

        # test put
        response = self.client.put(data['url'],
                                   {'filename': 'new-from-patch.txt'})
        assert_equal(response.status_code, 200)
        assert_equal(response.data['filename'], 'new-from-patch.txt')

        # test put with slashes in filename
        response = self.client.put(data['url'],
                                   {'filename': '/with/slashes.txt'})
        assert_equal(response.status_code, 200)
        assert_equal(response.data['filename'], 'withslashes.txt')
Esempio n. 2
0
    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)
Esempio n. 3
0
        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}
Esempio n. 4
0
File: base.py Progetto: ipbes/indigo
        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}