2013-10-27 21:26:51 +01:00
|
|
|
"""
|
2014-09-24 07:13:13 +02:00
|
|
|
Storing files according to a custom storage system
|
2013-10-27 21:26:51 +01:00
|
|
|
|
|
|
|
``FileField`` and its variations can take a ``storage`` argument to specify how
|
|
|
|
and where files should be stored.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import random
|
|
|
|
import tempfile
|
2019-08-18 11:10:49 +02:00
|
|
|
from pathlib import Path
|
2013-10-27 21:26:51 +01:00
|
|
|
|
2023-01-20 19:10:29 +01:00
|
|
|
from django.core.files.storage import FileSystemStorage, default_storage
|
2015-01-28 13:35:27 +01:00
|
|
|
from django.db import models
|
2013-10-27 21:26:51 +01:00
|
|
|
|
|
|
|
|
2015-05-03 06:10:24 +02:00
|
|
|
class CustomValidNameStorage(FileSystemStorage):
|
|
|
|
def get_valid_name(self, name):
|
|
|
|
# mark the name to show that this was called
|
|
|
|
return name + "_valid"
|
|
|
|
|
|
|
|
|
2015-02-21 18:56:36 +01:00
|
|
|
temp_storage_location = tempfile.mkdtemp()
|
2013-10-27 21:26:51 +01:00
|
|
|
temp_storage = FileSystemStorage(location=temp_storage_location)
|
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2020-03-31 12:12:39 +02:00
|
|
|
def callable_storage():
|
|
|
|
return temp_storage
|
|
|
|
|
|
|
|
|
2023-01-20 19:10:29 +01:00
|
|
|
def callable_default_storage():
|
|
|
|
return default_storage
|
|
|
|
|
|
|
|
|
2020-03-31 12:12:39 +02:00
|
|
|
class CallableStorage(FileSystemStorage):
|
|
|
|
def __call__(self):
|
|
|
|
# no-op implementation.
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
2013-10-27 21:26:51 +01:00
|
|
|
class Storage(models.Model):
|
|
|
|
def custom_upload_to(self, filename):
|
|
|
|
return "foo"
|
|
|
|
|
|
|
|
def random_upload_to(self, filename):
|
|
|
|
# This returns a different result each time,
|
|
|
|
# to make sure it only gets called once.
|
|
|
|
return "%s/%s" % (random.randint(100, 999), filename)
|
|
|
|
|
2019-08-18 11:10:49 +02:00
|
|
|
def pathlib_upload_to(self, filename):
|
|
|
|
return Path("bar") / filename
|
|
|
|
|
2013-10-27 21:26:51 +01:00
|
|
|
normal = models.FileField(storage=temp_storage, upload_to="tests")
|
|
|
|
custom = models.FileField(storage=temp_storage, upload_to=custom_upload_to)
|
2019-08-18 11:10:49 +02:00
|
|
|
pathlib_callable = models.FileField(
|
|
|
|
storage=temp_storage, upload_to=pathlib_upload_to
|
|
|
|
)
|
2019-08-18 11:40:11 +02:00
|
|
|
pathlib_direct = models.FileField(storage=temp_storage, upload_to=Path("bar"))
|
2013-10-27 21:26:51 +01:00
|
|
|
random = models.FileField(storage=temp_storage, upload_to=random_upload_to)
|
2015-05-03 06:10:24 +02:00
|
|
|
custom_valid_name = models.FileField(
|
|
|
|
storage=CustomValidNameStorage(location=temp_storage_location),
|
|
|
|
upload_to=random_upload_to,
|
|
|
|
)
|
2020-03-31 12:12:39 +02:00
|
|
|
storage_callable = models.FileField(
|
|
|
|
storage=callable_storage, upload_to="storage_callable"
|
|
|
|
)
|
|
|
|
storage_callable_class = models.FileField(
|
|
|
|
storage=CallableStorage, upload_to="storage_callable_class"
|
|
|
|
)
|
2023-01-20 19:10:29 +01:00
|
|
|
storage_callable_default = models.FileField(
|
|
|
|
storage=callable_default_storage, upload_to="storage_callable_default"
|
|
|
|
)
|
2013-10-27 21:26:51 +01:00
|
|
|
default = models.FileField(
|
|
|
|
storage=temp_storage, upload_to="tests", default="tests/default.txt"
|
|
|
|
)
|
2024-08-05 21:36:49 +02:00
|
|
|
db_default = models.FileField(
|
|
|
|
storage=temp_storage, upload_to="tests", db_default="tests/db_default.txt"
|
|
|
|
)
|
2013-10-27 21:26:51 +01:00
|
|
|
empty = models.FileField(storage=temp_storage)
|
2014-10-15 09:42:06 +02:00
|
|
|
limited_length = models.FileField(
|
|
|
|
storage=temp_storage, upload_to="tests", max_length=20
|
|
|
|
)
|
|
|
|
extended_length = models.FileField(
|
|
|
|
storage=temp_storage, upload_to="tests", max_length=300
|
|
|
|
)
|