Skip to content

Commit 7e29084

Browse files
committed
Ref #46 -- Document and test datetime support
1 parent 4aae8a9 commit 7e29084

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,25 @@ Syntax](https://docs.python.org/3/library/string.html#format-string-syntax).
77

88
### Installation
99

10-
``` bash
10+
```bash
1111
pip install django-dynamic-filenames
1212
```
1313

1414
### Samples
1515

1616
Basic example:
1717

18-
``` python
18+
```python
1919
from django.db import models
2020
from dynamic_filenames import FilePattern
2121

2222
upload_to_pattern = FilePattern(
23-
filename_pattern='{app_label:.25}/{model_name:.30}/{uuid:base32}{ext}'
23+
filename_pattern='{app_label:.25}/{model_name:.30}/{instance.created:%Y-%m-%d}/{uuid:base32}{ext}'
2424
)
2525

2626
class FileModel(models.Model):
2727
my_file = models.FileField(upload_to=upload_to_pattern)
28+
created = models.DateTimeField(auto_now_add=True)
2829
```
2930

3031
Auto slug example:
@@ -109,7 +110,7 @@ You can also use a special slug type specifier, that slugifies strings.
109110

110111
Example:
111112

112-
``` python
113+
```python
113114
from django.db import models
114115
from dynamic_filenames import FilePattern
115116

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ testpaths = ["tests"]
6868
DJANGO_SETTINGS_MODULE = "tests.testapp.settings"
6969

7070
[tool.coverage.run]
71-
source = ["django_esm"]
71+
source = ["dynamic_filenames"]
7272

7373
[tool.coverage.report]
7474
show_missing = true

tests/test_dynamic_filenames.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime
12
import uuid
23

34
import pytest
@@ -178,6 +179,17 @@ def test_uuid(self):
178179
== "522d6f3519204b0fb82ae8f558af2749.txt"
179180
)
180181

182+
def test_date(self):
183+
assert (
184+
FilePattern(filename_pattern="{instance.created:%Y-%m-%d}{ext}")(
185+
instance=DefaultModel(
186+
title="best model", created=datetime.date(2021, 9, 10)
187+
),
188+
filename="some_file.txt",
189+
)
190+
== "2021-09-10.txt"
191+
)
192+
181193

182194
class TestExtendedUUID:
183195
def test_uuid_2_base10(self):

tests/testapp/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
class DefaultModel(models.Model):
1111
title = models.CharField(max_length=100, default="hello goodby")
1212
file_field = models.FileField(upload_to=upload_to_pattern)
13+
created = models.DateTimeField(auto_now_add=True)

0 commit comments

Comments
 (0)