diff --git a/README.md b/README.md index 71947c7..38b9b9b 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Syntax](https://docs.python.org/3/library/string.html#format-string-syntax). ### Installation -``` bash +```bash pip install django-dynamic-filenames ``` @@ -15,16 +15,17 @@ pip install django-dynamic-filenames Basic example: -``` python +```python from django.db import models from dynamic_filenames import FilePattern upload_to_pattern = FilePattern( - filename_pattern='{app_label:.25}/{model_name:.30}/{uuid:base32}{ext}' + filename_pattern='{app_label:.25}/{model_name:.30}/{instance.created:%Y-%m-%d}/{uuid:base32}{ext}' ) class FileModel(models.Model): my_file = models.FileField(upload_to=upload_to_pattern) + created = models.DateTimeField(auto_now_add=True) ``` Auto slug example: @@ -109,7 +110,7 @@ You can also use a special slug type specifier, that slugifies strings. Example: -``` python +```python from django.db import models from dynamic_filenames import FilePattern diff --git a/pyproject.toml b/pyproject.toml index aa24c6a..12454e2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -68,7 +68,7 @@ testpaths = ["tests"] DJANGO_SETTINGS_MODULE = "tests.testapp.settings" [tool.coverage.run] -source = ["django_esm"] +source = ["dynamic_filenames"] [tool.coverage.report] show_missing = true diff --git a/tests/test_dynamic_filenames.py b/tests/test_dynamic_filenames.py index a58b984..0c21048 100644 --- a/tests/test_dynamic_filenames.py +++ b/tests/test_dynamic_filenames.py @@ -1,3 +1,4 @@ +import datetime import uuid import pytest @@ -178,6 +179,17 @@ def test_uuid(self): == "522d6f3519204b0fb82ae8f558af2749.txt" ) + def test_date(self): + assert ( + FilePattern(filename_pattern="{instance.created:%Y-%m-%d}{ext}")( + instance=DefaultModel( + title="best model", created=datetime.date(2021, 9, 10) + ), + filename="some_file.txt", + ) + == "2021-09-10.txt" + ) + class TestExtendedUUID: def test_uuid_2_base10(self): diff --git a/tests/testapp/models.py b/tests/testapp/models.py index ad18c77..159623f 100644 --- a/tests/testapp/models.py +++ b/tests/testapp/models.py @@ -10,3 +10,4 @@ class DefaultModel(models.Model): title = models.CharField(max_length=100, default="hello goodby") file_field = models.FileField(upload_to=upload_to_pattern) + created = models.DateTimeField(auto_now_add=True)