From 8332a39defaf21e701ddb47ebc399a291e7582f1 Mon Sep 17 00:00:00 2001 From: Mike Date: Sun, 12 Jun 2022 21:58:44 -0700 Subject: [PATCH] fix: #187 provide eav value during field cleaning --- eav/forms.py | 7 ++++--- eav/widgets.py | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/eav/forms.py b/eav/forms.py index 0e3e00d..b6117d7 100644 --- a/eav/forms.py +++ b/eav/forms.py @@ -32,6 +32,7 @@ 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): @@ -39,10 +40,10 @@ class CSVFormField(forms.Field): 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) diff --git a/eav/widgets.py b/eav/widgets.py index e68887e..30d862e 100644 --- a/eav/widgets.py +++ b/eav/widgets.py @@ -21,3 +21,21 @@ class CSVWidget(Textarea): 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