Skip to content

Commit

Permalink
Merge pull request #205 from Dresdn/Dresdn/issue-187
Browse files Browse the repository at this point in the history
fix: #187 return csv field value in widget
  • Loading branch information
Dresdn committed Jun 13, 2022
2 parents 5a6cc40 + 361eebf commit b5702fb
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ We follow [Semantic Versions](https://semver.org/) starting at the `0.14.0` rele

### Bug Fixes

- Fixes AttributeError when using CSVFormField [[#187](https://github.com/jazzband/django-eav2/issues/187)]

## 1.2.1 (2021-02-08)
### Bug Fixes

Expand Down
7 changes: 4 additions & 3 deletions eav/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@ class CSVFormField(forms.Field):

def __init__(self, *args, **kwargs):
kwargs.pop('max_length', None)
self.separator = kwargs.pop('separator', self.default_separator)
super().__init__(*args, **kwargs)

def to_python(self, value):
if not value:
return []
return [v.strip() for v in value.split(self.separator) if v]

def validate(self, value):
super().validate(value)
def validate(self, field_value):
super().validate(field_value)
try:
isinstance(value.split(self.separator), list)
isinstance(field_value, list)
except ValidationError:
raise ValidationError(self.message, code=self.code)

Expand Down
18 changes: 18 additions & 0 deletions eav/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,21 @@ def prep_value(self, value):
def render(self, name, value, **kwargs):
value = self.prep_value(value)
return super().render(name, value, **kwargs)

def value_from_datadict(self, data, files, name):
"""
Return the value of this widget or None.
Since we're only given the value of the entity name and the data dict
contains the '_eav_config_cls' (which we don't have access to) as the
key, we need to loop through each field checking if the eav attribute
exists with the given 'name'.
"""
widget_value = None
for data_value in data:
try:
widget_value = getattr(data.get(data_value), name)
except AttributeError:
pass # noqa: WPS420

return widget_value
56 changes: 55 additions & 1 deletion tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys

import pytest
from django.contrib.admin.sites import AdminSite
from django.core.handlers.base import BaseHandler
from django.forms import ModelForm
Expand Down Expand Up @@ -50,6 +50,12 @@ class Meta:
fields = '__all__'


class PatientDynamicForm(BaseDynamicEntityForm):
class Meta:
model = Patient
fields = '__all__'


class M2MModelForm(ModelForm):
class Meta:
model = M2MModel
Expand Down Expand Up @@ -111,3 +117,51 @@ def test_m2m(self):
form = M2MModelForm(dict(name='Lorem', models=[model.pk]), instance=m2mmodel)
form.save()
self.assertEqual(len(m2mmodel.models.all()), 1)


@pytest.fixture()
def patient() -> Patient:
"""Return an eav enabled Patient instance."""
eav.register(Patient)
return Patient.objects.create(name='Jim Morrison')


@pytest.mark.django_db()
@pytest.mark.parametrize(
'csv_data, separator',
[
('', ';'),
('justone', ','),
('one;two;three', ';'),
('alpha,beta,gamma', ','),
(None, ','),
],
)
def test_csvdynamicform(patient, csv_data, separator) -> None:
"""Ensure that a TYPE_CSV field works correctly with forms."""
Attribute.objects.create(name='csv', datatype=Attribute.TYPE_CSV)
patient.eav.csv = csv_data
patient.save()
patient.refresh_from_db()

form = PatientDynamicForm(
patient.__dict__,
instance=patient,
)
form.fields['csv'].separator = separator
assert form.is_valid()
jim = form.save()

expected_result = str(csv_data).split(separator) if csv_data else []
assert jim.eav.csv == expected_result


@pytest.mark.django_db()
def test_csvdynamicform_empty(patient) -> None:
"""Test to ensure an instance with no eav values is correct."""
form = PatientDynamicForm(
patient.__dict__,
instance=patient,
)
assert form.is_valid()
assert form.save()

0 comments on commit b5702fb

Please sign in to comment.