Skip to content

Commit

Permalink
Fix validators setup in S3FileFieldMixin
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSuperiorStanislav committed Oct 4, 2024
1 parent 817bb74 commit af86b9c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ repos:
types: [ file ]
stages: [ push ]

- id: check_new_migrations
name: check for new migrations
entry: inv django.check-new-migrations
language: system
pass_filenames: false
types: [ file ]
stages: [ push ]

- id: pytest
name: Run pytest
entry: inv pytest.run
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ We follow [Semantic Versions](https://semver.org/).

## Unreleased

- Fix validators setup in `S3FileFieldMixin`

## 0.2.0

- Refactor and improve validation on keys in django module
Expand Down
26 changes: 17 additions & 9 deletions saritasa_s3_tools/django/model_fields.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import collections.abc

from django.core import exceptions
from django.core.files import utils
from django.core.files.storage import default_storage
from django.db import models
from django.db.models.fields import files
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _

import botocore.exceptions
Expand All @@ -26,20 +29,25 @@ def __init__(
**kwargs,
) -> None:
self.s3_config = s3_config
validators = [
self._validate_file_existence,
]
if validate_key_pattern:
validators.append(self._validate_key)
kwargs["validators"] = (
*validators,
*kwargs.get("validators", ()),
)
self.validate_key_pattern = validate_key_pattern
super().__init__(
verbose_name=verbose_name, # type: ignore
**kwargs,
)

@cached_property
def validators(
self,
) -> collections.abc.Sequence[
collections.abc.Callable[[files.FieldFile | str], None],
]:
"""Get validators."""
validators = super().validators # type: ignore
validators.append(self._validate_file_existence)
if self.validate_key_pattern:
validators.append(self._validate_key)
return validators

def generate_filename(
self,
instance: models.Model,
Expand Down

0 comments on commit af86b9c

Please sign in to comment.