| 1 |
""" |
| 2 |
Storing files according to a custom storage system |
| 3 |
|
| 4 |
``FileField`` and its variations can take a ``storage`` argument to specify how |
| 5 |
and where files should be stored. |
| 6 |
""" |
| 7 |
|
| 8 |
import random |
| 9 |
import tempfile |
| 10 |
from pathlib import Path |
| 11 |
|
| 12 |
from django.core.files.storage import FileSystemStorage, default_storage |
| 13 |
from django.db import models |
| 14 |
|
| 15 |
|
| 16 |
class CustomValidNameStorage(FileSystemStorage): |
| 17 |
def get_valid_name(self, name): |
| 18 |
# mark the name to show that this was called |
| 19 |
return name + "_valid" |
| 20 |
|
| 21 |
|
| 22 |
temp_storage_location = tempfile.mkdtemp() |
| 23 |
temp_storage = FileSystemStorage(location=temp_storage_location) |
| 24 |
|
| 25 |
|
| 26 |
def callable_storage(): |
| 27 |
return temp_storage |
| 28 |
|
| 29 |
|
| 30 |
def callable_default_storage(): |
| 31 |
return default_storage |
| 32 |
|
| 33 |
|
| 34 |
class CallableStorage(FileSystemStorage): |
| 35 |
def __call__(self): |
| 36 |
# no-op implementation. |
| 37 |
return self |
| 38 |
|
| 39 |
|
| 40 |
class Storage(models.Model): |
| 41 |
def custom_upload_to(self, filename): |
| 42 |
return "foo" |
| 43 |
|
| 44 |
def random_upload_to(self, filename): |
| 45 |
# This returns a different result each time, |
| 46 |
# to make sure it only gets called once. |
| 47 |
return "%s/%s" % (random.randint(100, 999), filename) |
| 48 |
|
| 49 |
def pathlib_upload_to(self, filename): |
| 50 |
return Path("bar") / filename |
| 51 |
|
| 52 |
normal = models.FileField(storage=temp_storage, upload_to="tests") |
| 53 |
custom = models.FileField(storage=temp_storage, upload_to=custom_upload_to) |
| 54 |
pathlib_callable = models.FileField( |
| 55 |
storage=temp_storage, upload_to=pathlib_upload_to |
| 56 |
) |
| 57 |
pathlib_direct = models.FileField(storage=temp_storage, upload_to=Path("bar")) |
| 58 |
random = models.FileField(storage=temp_storage, upload_to=random_upload_to) |
| 59 |
custom_valid_name = models.FileField( |
| 60 |
storage=CustomValidNameStorage(location=temp_storage_location), |
| 61 |
upload_to=random_upload_to, |
| 62 |
) |
| 63 |
storage_callable = models.FileField( |
| 64 |
storage=callable_storage, upload_to="storage_callable" |
| 65 |
) |
| 66 |
storage_callable_class = models.FileField( |
| 67 |
storage=CallableStorage, upload_to="storage_callable_class" |
| 68 |
) |
| 69 |
storage_callable_default = models.FileField( |
| 70 |
storage=callable_default_storage, upload_to="storage_callable_default" |
| 71 |
) |
| 72 |
default = models.FileField( |
| 73 |
storage=temp_storage, upload_to="tests", default="tests/default.txt" |
| 74 |
) |
| 75 |
db_default = models.FileField( |
| 76 |
storage=temp_storage, upload_to="tests", db_default="tests/db_default.txt" |
| 77 |
) |
| 78 |
empty = models.FileField(storage=temp_storage) |
| 79 |
limited_length = models.FileField( |
| 80 |
storage=temp_storage, upload_to="tests", max_length=20 |
| 81 |
) |
| 82 |
extended_length = models.FileField( |
| 83 |
storage=temp_storage, upload_to="tests", max_length=1024 |
| 84 |
) |