Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed calling population methods when declared in form #66

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.0.0-rc.10 : 02.08.2024

- **Fixed**: Fixed calling population methods when declared in form

## 1.0.0-rc.9 : 24.03.2023

- **Added**: Introduced extra optional arguments when creating `From` instance
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Jakub Dubec
Copyright (c) 2024 Jakub Dubec

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 4 additions & 0 deletions django_api_forms/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,17 @@ def populate(self, obj, exclude: List[str] = None):
field_class, "django_api_forms.population_strategies.BaseStrategy"
)
)

if isinstance(self.Meta, type):
if hasattr(self.Meta, 'field_strategy'):
if key in self.Meta.field_strategy.keys():
strategy = resolve_from_path(
self.Meta.field_strategy[key]
)

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])

return obj
Expand Down
2 changes: 1 addition & 1 deletion django_api_forms/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.0.0-rc.9'
__version__ = '1.0.0-rc.10'
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "django-api-forms"
version = "1.0.0-rc.9"
version = "1.0.0-rc.10"
description = "Declarative Django request validation for RESTful APIs"
authors = [
"Jakub Dubec <[email protected]>",
Expand Down
52 changes: 51 additions & 1 deletion tests/test_population.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from django.forms import fields
from django.test import TestCase
from django.test.client import RequestFactory

from django_api_forms import Form, EnumField, FormField
from django_api_forms.exceptions import ApiFormException
from tests import settings
from tests.testapp.forms import AlbumForm, BandForm
from tests.testapp.forms import AlbumForm, BandForm, ArtistForm
from tests.testapp.models import Album, Artist, Band


Expand Down Expand Up @@ -73,3 +75,51 @@ def test_invalid_populate(self):
album = Album()
with self.assertRaisesMessage(ApiFormException, str('No clean data provided! Try to call is_valid() first.')):
form.populate(album)

def test_form_method_populate(self):
class MyAlbumForm(Form):
title = fields.CharField(max_length=100)
year = fields.IntegerField()
artist = FormField(form=ArtistForm)
type = EnumField(enum=Album.AlbumType, required=True)

def populate_year(self, obj, value: int) -> int:
return 2020

def populate_artist(self, obj, value: dict) -> Artist:
artist = Artist()

artist.name = value['name']
artist.genres = value['genres']
artist.members = value['members']

obj.artist = artist

return artist

request_factory = RequestFactory()
data = {
'title': 'Unknown Pleasures',
'year': 1979,
'artist': {
"name": "Punk Pineapples",
"genres": ["Punk", "Tropical Rock"],
"members": 5
},
'type': 'vinyl'
}

request = request_factory.post(
'/test/',
data=data,
content_type='application/json'
)

my_model = Album()
form = MyAlbumForm.create_from_request(request)
self.assertTrue(form.is_valid())

form.populate(my_model)
self.assertIsInstance(my_model.artist, Artist)
self.assertEqual(my_model.year, 2020)
self.assertEqual(my_model.artist.name, 'Punk Pineapples')
Loading