def save_uploaded_file(self, collection, formfile, filename_set):
     filename = formfile.name
     if collection.unique_filenames:
         filename = make_unique_filename(
             filename_set=filename_set,
             wanted_filename=filename,
             max_filename_length=collection.max_filename_length)
         filename_set.add(filename)
     temporaryfile = TemporaryFile(collection=collection, filename=filename)
     temporaryfile.file.save(formfile.name, formfile, save=False)
     temporaryfile.full_clean()
     temporaryfile.save()
     return temporaryfile
    def test_singlemode_keeps_only_last_file_do_not_delete_last(self):
        collection = TemporaryFileCollection.objects.create(
            user=create_user('testuser'), singlemode=True)

        last_added_temporary_file = TemporaryFile(filename='test2.txt',
                                                  collection=collection)
        last_added_temporary_file.file.save('test1.txt',
                                            ContentFile('Testdata'),
                                            save=False)
        last_added_temporary_file.clean()
        last_added_temporary_file.save()
        last_added_temporary_file.clean()
        self.assertEqual(collection.files.count(), 1)
        self.assertEqual(collection.files.first(), last_added_temporary_file)
    def test_singlemode_keeps_only_last_file_delete_physical_file(self):
        collection = TemporaryFileCollection.objects.create(
            user=create_user('testuser'), singlemode=True)

        first_added_temporary_file = TemporaryFile(filename='test1.txt',
                                                   collection=collection)
        first_added_temporary_file.file.save('test1.txt',
                                             ContentFile('Testdata'),
                                             save=False)
        first_added_temporary_file.clean()
        first_added_temporary_file.save()
        first_added_physical_file_path = first_added_temporary_file.file.path
        self.assertTrue(os.path.exists(first_added_physical_file_path))

        last_added_temporary_file = TemporaryFile(filename='test2.txt',
                                                  collection=collection)
        last_added_temporary_file.file.save('test1.txt',
                                            ContentFile('Testdata'),
                                            save=False)
        last_added_temporary_file.clean()
        self.assertFalse(os.path.exists(first_added_physical_file_path))
Beispiel #4
0
    def test_post_accept_invalid_existing_collection(self):
        collection = TemporaryFileCollection.objects.create(user=self.testuser)
        temporaryfile = TemporaryFile(
            collection=collection,
            filename='testfile.txt')
        temporaryfile.save()

        request = self.factory.post('/test', {
            'collectionid': collection.id,
            'file': SimpleUploadedFile('testfile1.txt', b'Test1'),
            'accept': 'application/pdf'
        })
        request.user = self.testuser
        self.assertEqual(TemporaryFileCollection.objects.count(), 1)
        self.assertEqual(collection.files.count(), 1)
        response = UploadTemporaryFilesView.as_view()(request)
        self.assertEqual(response.status_code, 400)
        responsedata = json.loads(response.content.decode('utf-8'))
        self.assertEqual(TemporaryFileCollection.objects.count(), 1)
        self.assertEqual(collection.files.count(), 1)
        self.assertEqual(responsedata['file'][0]['code'], 'unsupported_mimetype')
        self.assertEqual(responsedata['file'][0]['message'], u'testfile1.txt: Unsupported filetype.')