Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Imports
3from django.core.files.storage import FileSystemStorage
5# Exports
7__all__ = (
8 "OverwriteStorage",
9)
12# Classes
15class OverwriteStorage(FileSystemStorage):
16 """Upload a file, overwriting an existing file."""
18 # TODO: See Django file_storage tests for guidance on how to test OverwriteStorage
19 # https://github.com/django/django/blob/master/tests/file_storage/tests.py
20 # See also: https://stackoverflow.com/a/11170472/241720
22 def get_available_name(self, name, max_length=None):
23 """Since the purpose of the storage class is to overwrite, the name is returned as is."""
24 return name
26 def save(self, name, content, max_length=None):
27 """Delete the existing file if it exists before saving the new file."""
28 if self.exists(name):
29 self.delete(name)
31 return super().save(name, content, max_length=max_length)