Skip to content

Commit

Permalink
AliasStrategy 🍩
Browse files Browse the repository at this point in the history
  • Loading branch information
Sibyx committed Aug 15, 2024
1 parent c1bfb8b commit 9294463
Show file tree
Hide file tree
Showing 6 changed files with 777 additions and 614 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 1.0.0-rc.10 : 02.08.2024

- **Added**: `AliasStrategy` for overriding property name on target object during `setattr()`
- **Changed**:`field_strategy` now can be also an instance of `BaseStrategy`
- **Fixed**: Fixed calling population methods when declared in form

## 1.0.0-rc.9 : 24.03.2023
Expand Down
6 changes: 5 additions & 1 deletion django_api_forms/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.utils.translation import gettext as _

from .exceptions import UnsupportedMediaType, ApiFormException, DetailValidationError
from .population_strategies import BaseStrategy
from .settings import Settings
from .utils import resolve_from_path

Expand Down Expand Up @@ -197,7 +198,10 @@ def populate(self, obj, exclude: List[str] = None):
if hasattr(self, f'populate_{key}'):
self.cleaned_data[key] = getattr(self, f'populate_{key}')(obj, self.cleaned_data[key])

strategy()(field, obj, key, self.cleaned_data[key])
if isinstance(strategy, BaseStrategy):
strategy(field, obj, key, self.cleaned_data[key])
else:
strategy()(field, obj, key, self.cleaned_data[key])

return obj

Expand Down
8 changes: 8 additions & 0 deletions django_api_forms/population_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ def __call__(self, field, obj, key: str, value):
setattr(obj, key, value)


class AliasStrategy(BaseStrategy):
def __init__(self, property_name: str):
self._property_name = property_name

def __call__(self, field, obj, key: str, value):
setattr(obj, self._property_name, value)


class IgnoreStrategy(BaseStrategy):
def __call__(self, field, obj, key: str, value):
pass
Expand Down
Loading

0 comments on commit 9294463

Please sign in to comment.