-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Population strategies and settings (#30) πͺ²
* Population strategies and pluggable parsers π§π»ββοΈ * Fixed Python 3.10 tests * Disable Python 3.10 tests * Fixing code-style issues * Coverage settings * Docs * Code-style changes * Release date
- Loading branch information
Showing
24 changed files
with
561 additions
and
341 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
coverage: | ||
precision: 2 | ||
round: down | ||
range: "70...100" | ||
|
||
status: | ||
project: false | ||
patch: true | ||
changes: false | ||
|
||
comment: | ||
layout: "header, diff, changes, tree" | ||
behavior: default | ||
|
||
ignore: | ||
- "docs/**" | ||
- ".github/**" | ||
- "tests/**" | ||
- "setup.py" | ||
- "runtests.py" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
[flake8] | ||
ignore = E203,W503,E704 | ||
exclude = .git,.mypy_cache,.pytest_cache,.tox,.venv,__pycache__,build,dist,docs,venv | ||
max-line-length = 119 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class BaseStrategy: | ||
def __call__(self, field, obj, key: str, value): | ||
setattr(obj, key, value) | ||
|
||
|
||
class IgnoreStrategy(BaseStrategy): | ||
def __call__(self, field, obj, key: str, value): | ||
pass | ||
|
||
|
||
class ModelChoiceFieldStrategy(BaseStrategy): | ||
|
||
""" | ||
We need to changes key postfix if there is ModelChoiceField (because of _id etc.) | ||
We always try to assign whole object instance, for example: | ||
artis_id is normalized as Artist model, but it have to be assigned to artist model property | ||
because artist_id in model has different type (for example int if your are using int primary keys) | ||
If you are still confused (sorry), try to check docs | ||
""" | ||
def __call__(self, field, obj, key: str, value): | ||
model_key = key | ||
if field.to_field_name: | ||
postfix_to_remove = f"_{field.to_field_name}" | ||
else: | ||
postfix_to_remove = "_id" | ||
if key.endswith(postfix_to_remove): | ||
model_key = key[:-len(postfix_to_remove)] | ||
setattr(obj, model_key, value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from django.conf import settings | ||
|
||
DEFAULTS = { | ||
'POPULATION_STRATEGIES': { | ||
'django_api_forms.fields.FormFieldList': 'django_api_forms.population_strategies.IgnoreStrategy', | ||
'django_api_forms.fields.FileField': 'django_api_forms.population_strategies.IgnoreStrategy', | ||
'django_api_forms.fields.ImageField': 'django_api_forms.population_strategies.IgnoreStrategy', | ||
'django_api_forms.fields.FormField': 'django_api_forms.population_strategies.IgnoreStrategy', | ||
'django.forms.models.ModelMultipleChoiceField': 'django_api_forms.population_strategies.IgnoreStrategy', | ||
'django.forms.models.ModelChoiceField': 'django_api_forms.population_strategies.ModelChoiceFieldStrategy' | ||
}, | ||
'DEFAULT_POPULATION_STRATEGY': 'django_api_forms.population_strategies.BaseStrategy', | ||
'PARSERS': { | ||
'application/json': 'json.loads', | ||
'application/x-msgpack': 'msgpack.loads' | ||
} | ||
} | ||
|
||
|
||
class Settings: | ||
def __getattr__(self, item): | ||
if item not in DEFAULTS: | ||
raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{item}'") | ||
|
||
django_setting = f"DJANGO_API_FORMS_{item}" | ||
default = DEFAULTS[item] | ||
|
||
if hasattr(settings, django_setting): | ||
customized_value = getattr(settings, django_setting) | ||
if isinstance(default, dict): | ||
value = {**default, **customized_value} | ||
else: | ||
value = customized_value | ||
else: | ||
value = default | ||
|
||
setattr(self, item, value) | ||
return value |
Oops, something went wrong.