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
    
  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. class CallableStorage(FileSystemStorage):
    
  31.     def __call__(self):
    
  32.         # no-op implementation.
    
  33.         return self
    
  34. 
    
  35. 
    
  36. class Storage(models.Model):
    
  37.     def custom_upload_to(self, filename):
    
  38.         return "foo"
    
  39. 
    
  40.     def random_upload_to(self, filename):
    
  41.         # This returns a different result each time,
    
  42.         # to make sure it only gets called once.
    
  43.         return "%s/%s" % (random.randint(100, 999), filename)
    
  44. 
    
  45.     def pathlib_upload_to(self, filename):
    
  46.         return Path("bar") / filename
    
  47. 
    
  48.     normal = models.FileField(storage=temp_storage, upload_to="tests")
    
  49.     custom = models.FileField(storage=temp_storage, upload_to=custom_upload_to)
    
  50.     pathlib_callable = models.FileField(
    
  51.         storage=temp_storage, upload_to=pathlib_upload_to
    
  52.     )
    
  53.     pathlib_direct = models.FileField(storage=temp_storage, upload_to=Path("bar"))
    
  54.     random = models.FileField(storage=temp_storage, upload_to=random_upload_to)
    
  55.     custom_valid_name = models.FileField(
    
  56.         storage=CustomValidNameStorage(location=temp_storage_location),
    
  57.         upload_to=random_upload_to,
    
  58.     )
    
  59.     storage_callable = models.FileField(
    
  60.         storage=callable_storage, upload_to="storage_callable"
    
  61.     )
    
  62.     storage_callable_class = models.FileField(
    
  63.         storage=CallableStorage, upload_to="storage_callable_class"
    
  64.     )
    
  65.     default = models.FileField(
    
  66.         storage=temp_storage, upload_to="tests", default="tests/default.txt"
    
  67.     )
    
  68.     empty = models.FileField(storage=temp_storage)
    
  69.     limited_length = models.FileField(
    
  70.         storage=temp_storage, upload_to="tests", max_length=20
    
  71.     )
    
  72.     extended_length = models.FileField(
    
  73.         storage=temp_storage, upload_to="tests", max_length=300
    
  74.     )