Skip to content

Commit

Permalink
Ref #46 -- Document and test datetime support
Browse files Browse the repository at this point in the history
  • Loading branch information
codingjoe committed Oct 18, 2024
1 parent 4aae8a9 commit 7e29084
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,25 @@ Syntax](https://docs.python.org/3/library/string.html#format-string-syntax).

### Installation

``` bash
```bash
pip install django-dynamic-filenames
```

### Samples

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:
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/test_dynamic_filenames.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import uuid

import pytest
Expand Down Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions tests/testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 7e29084

Please sign in to comment.