예제 #1
0
 def handle(self, *args, **options):
     File.dump_files(verbose=True)
예제 #2
0
    def test_adding_file(self):

        # Create default thing storing reference to file
        # in the local media directory.
        test_fqfn = os.path.join(self.media_dir, "test.txt")
        open(test_fqfn, "w").write("hello there")
        o1 = o = Thing()
        test_fn = "i/special/test.txt"
        o.upload = test_fn
        o.save()
        obj_id = o.id

        # Confirm thing was saved.
        Thing.objects.update()
        q = Thing.objects.all()
        self.assertEqual(q.count(), 1)
        self.assertEqual(q[0].upload.name, test_fn)

        # Confirm the file only exists on the file system
        # and hasn't been loaded into the database.
        q = File.objects.all()
        self.assertEqual(q.count(), 0)

        # Verify we can read the contents of thing.
        o = Thing.objects.get(id=obj_id)
        self.assertEqual(o.upload.read(), b"hello there")

        # Verify that by attempting to read the file, we've automatically
        # loaded it into the database.
        File.objects.update()
        q = File.objects.all()
        self.assertEqual(q.count(), 1)
        self.assertEqual(q[0].content, b"hello there")

        # Load a dynamically created file outside /media.
        test_file = files.temp.NamedTemporaryFile(
            suffix=".txt",
            # Django>=1.10 no longer allows accessing files outside of MEDIA_ROOT...
            # dir=files.temp.gettempdir()
            dir=os.path.join(settings.PROJECT_DIR, "media"),
        )
        data0 = b"1234567890"
        test_file.write(data0)
        test_file.seek(0)
        t = Thing.objects.create(upload=files.File(test_file))
        self.assertEqual(File.objects.count(), 2)
        t = Thing.objects.get(pk=t.pk)
        self.assertEqual(t.upload.file.size, 10)
        self.assertEqual(t.upload.file.name[-4:], ".txt")
        self.assertEqual(t.upload.file.read(), data0)
        t.upload.delete()
        self.assertEqual(File.objects.count(), 1)

        # Delete file from local filesystem and re-export it from the database.
        self.assertEqual(os.path.isfile(test_fqfn), True)
        os.remove(test_fqfn)
        self.assertEqual(os.path.isfile(test_fqfn), False)
        o1.upload.read()  # This forces the re-export to the filesystem.
        self.assertEqual(os.path.isfile(test_fqfn), True)

        # This dumps all files to the filesystem.
        File.dump_files()

        # Confirm when delete a file from the database, we also delete it from
        # the filesystem.
        self.assertEqual(default_storage.exists("i/special/test.txt"), True)
        default_storage.delete("i/special/test.txt")
        self.assertEqual(default_storage.exists("i/special/test.txt"), False)
        self.assertEqual(os.path.isfile(test_fqfn), False)
예제 #3
0
 def test_adding_file(self):
     
     # Create default thing storing reference to file
     # in the local media directory.
     test_fqfn = os.path.join(self.media_dir, 'test.txt')
     open(test_fqfn,'w').write('hello there')
     o1 = o = Thing()
     test_fn = 'i/special/test.txt'
     o.upload = test_fn
     o.save()
     id = o.id
     
     # Confirm thing was saved.
     Thing.objects.update()
     q = Thing.objects.all()
     self.assertEqual(q.count(), 1)
     self.assertEqual(q[0].upload.name, test_fn)
     
     # Confirm the file only exists on the file system
     # and hasn't been loaded into the database.
     q = File.objects.all()
     self.assertEqual(q.count(), 0)
     
     # Verify we can read the contents of thing.
     o = Thing.objects.get(id=id)
     self.assertEqual(o.upload.read(), b"hello there")
     
     # Verify that by attempting to read the file, we've automatically
     # loaded it into the database.
     File.objects.update()
     q = File.objects.all()
     self.assertEqual(q.count(), 1)
     self.assertEqual(q[0].content, b"hello there")
     
     # Load a dynamically created file outside /media.
     test_file = files.temp.NamedTemporaryFile(
         suffix='.txt',
         dir=files.temp.gettempdir()
     )
     test_file.write(b'1234567890')
     test_file.seek(0)
     t = Thing.objects.create(
         upload=files.File(test_file),
     )
     self.assertEqual(File.objects.count(), 2)
     t = Thing.objects.get(pk=t.pk)
     self.assertEqual(t.upload.file.size, 10)
     self.assertEqual(t.upload.file.name[-4:], '.txt')
     self.assertEqual(t.upload.file.read(), b'1234567890')
     t.upload.delete()
     self.assertEqual(File.objects.count(), 1)
     
     # Delete file from local filesystem and re-export it from the database.
     self.assertEqual(os.path.isfile(test_fqfn), True)
     os.remove(test_fqfn)
     self.assertEqual(os.path.isfile(test_fqfn), False)
     o1.upload.read() # This forces the re-export to the filesystem.
     self.assertEqual(os.path.isfile(test_fqfn), True)
     
     # This dumps all files to the filesystem.
     File.dump_files()
     
     # Confirm when delete a file from the database, we also delete it from
     # the filesystem.
     self.assertEqual(default_storage.exists('i/special/test.txt'), True)
     default_storage.delete('i/special/test.txt')
     self.assertEqual(default_storage.exists('i/special/test.txt'), False)
     self.assertEqual(os.path.isfile(test_fqfn), False)
    def test_adding_file(self):
        # Create default thing storing reference to file
        # in the local media directory.
        test_fqfn = os.path.join(self.media_dir, 'test.txt')
        open(test_fqfn, 'w').write('hello there')
        o1 = o = Thing()
        test_fn = 'i/special/test.txt'
        o.upload = test_fn
        o.save()
        obj_id = o.id

        # Confirm thing was saved.
        Thing.objects.update()
        q = Thing.objects.all()
        self.assertEqual(q.count(), 1)
        self.assertEqual(q[0].upload.name, test_fn)

        # Confirm the file only exists on the file system
        # and hasn't been loaded into the database.
        q = File.objects.all()
        self.assertEqual(q.count(), 0)

        # Verify we can read the contents of thing.
        o = Thing.objects.get(id=obj_id)
        self.assertEqual(o.upload.read(), b"hello there")

        # Verify that by attempting to read the file, we've automatically
        # loaded it into the database.
        File.objects.update()
        q = File.objects.all()
        self.assertEqual(q.count(), 1)
        self.assertEqual(q[0].content, b"hello there")

        # Load a dynamically created file outside /media.
        test_file = files.temp.NamedTemporaryFile(
            suffix='.txt',
            dir=os.path.join(settings.PROJECT_DIR, 'media'),
        )
        data0 = b'1234567890'
        test_file.write(data0)
        test_file.seek(0)
        t = Thing.objects.create(upload=files.File(test_file), )
        self.assertEqual(File.objects.count(), 2)
        t = Thing.objects.get(pk=t.pk)
        self.assertEqual(t.upload.file.size, 10)
        self.assertEqual(t.upload.file.name[-4:], '.txt')
        self.assertEqual(t.upload.file.read(), data0)
        t.upload.delete()
        self.assertEqual(File.objects.count(), 1)

        # Delete file from local filesystem and re-export it from the database.
        self.assertEqual(os.path.isfile(test_fqfn), True)
        os.remove(test_fqfn)
        self.assertEqual(os.path.isfile(test_fqfn), False)
        o1.upload.read()  # This forces the re-export to the filesystem.
        self.assertEqual(os.path.isfile(test_fqfn), True)

        # This dumps all files to the filesystem.
        File.dump_files()

        # Confirm when delete a file from the database, we also delete it from
        # the filesystem.
        self.assertEqual(default_storage.exists('i/special/test.txt'), True)
        default_storage.delete('i/special/test.txt')
        self.assertEqual(default_storage.exists('i/special/test.txt'), False)
        self.assertEqual(os.path.isfile(test_fqfn), False)