Skip to content

Commit

Permalink
Update packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
codingjoe committed Oct 9, 2024
1 parent 259716d commit 9a0a3fd
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 212 deletions.
18 changes: 8 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.10"
python-version: "3.x"
cache: 'pip'
cache-dependency-path: 'requirements.txt'
- run: python -m pip install -r requirements.txt
cache-dependency-path: 'pyproject.toml'
- run: python -m pip install -e .[lint]
- run: ${{ matrix.lint-command }}

readme:
Expand All @@ -37,10 +37,10 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.10"
python-version: "3.x"
- name: Install Python dependencies
run: python -m pip install --upgrade pip setuptools wheel twine readme-renderer
- run: python setup.py sdist bdist_wheel
run: python -m pip install --upgrade pip build wheel twine readme-renderer
- run: python -m build --sdist --wheel
- run: python -m twine check dist/*
- uses: actions/upload-artifact@v4
with:
Expand Down Expand Up @@ -68,13 +68,11 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Upgrade Python setuptools
run: python -m pip install --upgrade pip setuptools wheel codecov
- run: python setup.py develop
- run: python -m pip install .[test]
- name: Install Django ${{ matrix.django-version }}
run: python -m pip install "django~=${{ matrix.django-version }}.0"
- name: Run tests
run: python setup.py test
run: python -m pytest
- run: codecov

analyze:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ jobs:
with:
python-version: "3.10"
- name: Install Python dependencies
run: python -m pip install --upgrade pip setuptools wheel twine
run: python -m pip install --upgrade pip build wheel twine
- name: Build dist packages
run: python setup.py sdist bdist_wheel
run: python -m build --sdist --wheel
- name: Upload packages
run: python -m twine upload dist/*
env:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ venv.bak/

# mypy
.mypy_cache/

# setuptools_scm
_version.py
127 changes: 127 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# Django Dynamic Filenames

Write advanced filename patterns using the [Format String
Syntax](https://docs.python.org/3/library/string.html#format-string-syntax).

## Getting Started

### Installation

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

### Samples

Basic example:

``` 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}'
)

class FileModel(models.Model):
my_file = models.FileField(upload_to=upload_to_pattern)
```

Auto slug example:

## Features

### Field names

`ext`

: File extension including the dot.

`name`

: Filename excluding the folders.

`model_name`

: Name of the Django model.

`app_label`

: App label of the Django model.

`instance`

: Instance of the model before it has been saved. You may not have a
primary key at this point.

`uuid`

: UUID version 4 that supports multiple type specifiers. The UUID will
be the same should you use it twice in the same string, but
different on each invocation of the `upload_to` callable.

The type specifiers allow you to format the UUID in different ways,
e.g. `{uuid:x}` will give you a with a hexadecimal UUID.

The supported type specifiers are:

`s`

: String representation of a UUID including dashes.

`i`

: Integer representation of a UUID. Like to `UUID.int`.

`x`

: Hexadecimal (Base16) representation of a UUID. Like to
`UUID.hex`.

`X`

: Upper case hexadecimal representation of a UUID. Like to
`UUID.hex`.

`base32`

: Base32 representation of a UUID without padding.

`base64`

: Base64 representation of a UUID without padding.

:::: warning
::: title
Warning
:::

Not all file systems support Base64 file names.
::::

All type specifiers also support precisions to cut the string, e.g.
`{{uuid:.2base32}}` would only return the first 2 characters of a
Base32 encoded UUID.

### Type specifiers

You can also use a special slug type specifier, that slugifies strings.

Example:

``` python
from django.db import models
from dynamic_filenames import FilePattern

upload_to_pattern = FilePattern(
filename_pattern='{app_label:.25}/{model_name:.30}/{instance.title:.40slug}{ext}'
)

class FileModel(models.Model):
title = models.CharField(max_length=100)
my_file = models.FileField(upload_to=upload_to_pattern)
```

Slug type specifiers also support precisions to cut the string. In the
example above the slug of the instance title will be cut at 40
characters.
118 changes: 0 additions & 118 deletions README.rst

This file was deleted.

6 changes: 6 additions & 0 deletions dynamic_filenames.py → dynamic_filenames/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Write advanced filename patterns using the Format String Syntax."""
import base64
import os
import re
Expand All @@ -6,6 +7,11 @@

from django.utils.text import slugify

from . import _version # noqa

__version__ = _version.__version__
VERSION = _version.VERSION_TUPLE


class SlugFormatter(Formatter):
format_spec_pattern = re.compile(r"(\.\d+)?([\d\w]+)?")
Expand Down
Loading

0 comments on commit 9a0a3fd

Please sign in to comment.