storage.py 630 B

12345678910111213141516171819202122232425262728293031
  1. from uuid import uuid4
  2. from functools import partial
  3. from typing import Callable, Union
  4. from django.db import models
  5. def dynamic_upload(
  6. path: str,
  7. instance: Union[models.Model, None] = None,
  8. filename: Union[str, None] = None,
  9. ) -> Union[
  10. Callable[
  11. [
  12. Union[models.Model, None],
  13. Union[str, None]
  14. ],
  15. Union[str, Callable]
  16. ],
  17. str
  18. ]:
  19. if not path.endswith('/'):
  20. path += '/'
  21. if not filename:
  22. return partial(dynamic_upload, path)
  23. filename = f'{uuid4().hex}-{filename}'
  24. return f'{path}{filename}'