From c03d395a91a13ccb175ce4aae387accef4f7a0f0 Mon Sep 17 00:00:00 2001 From: Carlos Rodriguez Garcia Date: Thu, 1 Jul 2021 11:03:26 +0200 Subject: [PATCH 1/7] First commit. Add vscode/settings.json to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 2803757..0dd5b16 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.*~ *.egg-info *.pyc +.vscode/settings.json From a2c9c363972e4fe87994b7161812356af59358c5 Mon Sep 17 00:00:00 2001 From: Carlos Rodriguez Garcia Date: Mon, 12 Jul 2021 12:51:35 +0200 Subject: [PATCH 2/7] Add support por python3 --- django_remote_forms/fields.py | 2 +- django_remote_forms/forms.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/django_remote_forms/fields.py b/django_remote_forms/fields.py index 4e0d33a..b51b5a3 100644 --- a/django_remote_forms/fields.py +++ b/django_remote_forms/fields.py @@ -40,7 +40,7 @@ def as_dict(self): try: remote_widget_class = getattr(widgets, remote_widget_class_name) remote_widget = remote_widget_class(self.field.widget, field_name=self.field_name) - except Exception, e: + except Exception as e: logger.warning('Error serializing %s: %s', remote_widget_class_name, str(e)) widget_dict = {} else: diff --git a/django_remote_forms/forms.py b/django_remote_forms/forms.py index 26b38fb..ee736dd 100644 --- a/django_remote_forms/forms.py +++ b/django_remote_forms/forms.py @@ -138,7 +138,7 @@ def as_dict(self): try: remote_field_class = getattr(fields, remote_field_class_name) remote_field = remote_field_class(field, form_initial_field_data, field_name=name) - except Exception, e: + except Exception as e: logger.warning('Error serializing field %s: %s', remote_field_class_name, str(e)) field_dict = {} else: From 79c91363dd1bac009afdb67afbcd49b5d7f52935 Mon Sep 17 00:00:00 2001 From: Carlos Rodriguez Garcia Date: Mon, 12 Jul 2021 13:34:24 +0200 Subject: [PATCH 3/7] Support for foreign fields --- django_remote_forms/forms.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/django_remote_forms/forms.py b/django_remote_forms/forms.py index ee736dd..9395144 100644 --- a/django_remote_forms/forms.py +++ b/django_remote_forms/forms.py @@ -3,6 +3,7 @@ from django_remote_forms import fields, logger from django_remote_forms.utils import resolve_promise +from django.forms import ModelMultipleChoiceField class RemoteForm(object): def __init__(self, form, *args, **kwargs): @@ -121,12 +122,15 @@ def as_dict(self): form_dict['ordered_fields'] = self.fields initial_data = {} + foreign_key_fields = [] for name, field in [(x, self.form.fields[x]) for x in self.fields]: # Retrieve the initial data from the form itself if it exists so # that we properly handle which initial data should be returned in # the dictionary. + if type(field) in [ModelMultipleChoiceField]: + foreign_key_fields.append(name) # Please refer to the Django Form API documentation for details on # why this is necessary: # https://docs.djangoproject.com/en/dev/ref/forms/api/#dynamic-initial-values @@ -160,4 +164,9 @@ def as_dict(self): else: form_dict['data'] = initial_data + for field_name in foreign_key_fields: + obj_list = form_dict['data'].get(field_name, []) + if obj_list: + form_dict['data'][field_name] = [obj.pk for obj in obj_list] + return resolve_promise(form_dict) From a36683c25c3f0d3592f06e627fe93ec2866b46dc Mon Sep 17 00:00:00 2001 From: Carlos Rodriguez Garcia Date: Mon, 12 Jul 2021 13:39:19 +0200 Subject: [PATCH 4/7] Support comma separated field --- django_remote_forms/fields.py | 18 ++++++++++++++++++ django_remote_forms/forms.py | 11 +++++++++++ django_remote_forms/widgets.py | 4 ++++ 3 files changed, 33 insertions(+) diff --git a/django_remote_forms/fields.py b/django_remote_forms/fields.py index b51b5a3..400bd40 100644 --- a/django_remote_forms/fields.py +++ b/django_remote_forms/fields.py @@ -5,6 +5,7 @@ from django.conf import settings from django_remote_forms import logger, widgets +from django import forms class RemoteField(object): @@ -208,6 +209,10 @@ def as_dict(self): return super(RemoteMultipleChoiceField, self).as_dict() +class RemoteCommaSeparatedField(RemoteMultipleChoiceField): + pass + + class RemoteModelMultipleChoiceField(RemoteMultipleChoiceField): def as_dict(self): return super(RemoteModelMultipleChoiceField, self).as_dict() @@ -276,3 +281,16 @@ def as_dict(self): class RemoteSlugField(RemoteCharField): def as_dict(self): return super(RemoteSlugField, self).as_dict() + + +class CommaSeparatedField(forms.MultipleChoiceField): + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def prepare_value(self, value): + return value.split(',') if value else value + + def clean(self, value): + value = super().clean(value) + return ','.join(value) diff --git a/django_remote_forms/forms.py b/django_remote_forms/forms.py index 9395144..ca78f6c 100644 --- a/django_remote_forms/forms.py +++ b/django_remote_forms/forms.py @@ -123,6 +123,7 @@ def as_dict(self): initial_data = {} foreign_key_fields = [] + comma_separated_fields = [] for name, field in [(x, self.form.fields[x]) for x in self.fields]: # Retrieve the initial data from the form itself if it exists so @@ -131,6 +132,9 @@ def as_dict(self): if type(field) in [ModelMultipleChoiceField]: foreign_key_fields.append(name) + elif type(field) in [fields.CommaSeparatedField]: + comma_separated_fields.append(name) + # Please refer to the Django Form API documentation for details on # why this is necessary: # https://docs.djangoproject.com/en/dev/ref/forms/api/#dynamic-initial-values @@ -169,4 +173,11 @@ def as_dict(self): if obj_list: form_dict['data'][field_name] = [obj.pk for obj in obj_list] + for field_name in comma_separated_fields: + obj = form_dict['data'].get(field_name, '') + if obj: + form_dict['data'][field_name] = obj.split(',') + else: + form_dict['data'][field_name] = [] + return resolve_promise(form_dict) diff --git a/django_remote_forms/widgets.py b/django_remote_forms/widgets.py index d66a3fb..ac0c7af 100644 --- a/django_remote_forms/widgets.py +++ b/django_remote_forms/widgets.py @@ -193,6 +193,10 @@ def as_dict(self): return widget_dict +class RemoteFilteredSelectMultiple(RemoteSelectMultiple): + pass + + class RemoteRadioInput(RemoteWidget): def as_dict(self): widget_dict = OrderedDict() From 04d29cc5387f5ebcaaf6049c56133a5880cf2021 Mon Sep 17 00:00:00 2001 From: Carlos Rodriguez Garcia Date: Tue, 13 Jul 2021 10:15:16 +0200 Subject: [PATCH 5/7] Fix error with deprecated force_unicode function --- django_remote_forms/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_remote_forms/utils.py b/django_remote_forms/utils.py index 0391fa0..ce9db32 100644 --- a/django_remote_forms/utils.py +++ b/django_remote_forms/utils.py @@ -1,5 +1,5 @@ from django.utils.functional import Promise -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text def resolve_promise(o): From 065cc277e6950579ee08c445b64d8a401726dd72 Mon Sep 17 00:00:00 2001 From: Carlos Rodriguez Garcia Date: Wed, 21 Jul 2021 13:01:23 +0200 Subject: [PATCH 6/7] Filter form data to include only form fields data --- django_remote_forms/forms.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/django_remote_forms/forms.py b/django_remote_forms/forms.py index ca78f6c..b21793f 100644 --- a/django_remote_forms/forms.py +++ b/django_remote_forms/forms.py @@ -163,8 +163,13 @@ def as_dict(self): initial_data[name] = form_dict['fields'][name]['initial'] + form_data = self.form.data.copy() + + # Filter data to include only form fields data + form_data = {k: v for k, v in self.form.data.items() if k in list(self.fields)} + if self.form.data: - form_dict['data'] = self.form.data + form_dict['data'] = form_data else: form_dict['data'] = initial_data From a8e890389bc6defc1b451682844c25ee0b25cd42 Mon Sep 17 00:00:00 2001 From: Carlos Rodriguez Garcia Date: Mon, 13 Sep 2021 13:14:09 +0200 Subject: [PATCH 7/7] Fixed splited message errors --- django_remote_forms/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_remote_forms/utils.py b/django_remote_forms/utils.py index ce9db32..cc47c2a 100644 --- a/django_remote_forms/utils.py +++ b/django_remote_forms/utils.py @@ -10,7 +10,7 @@ def resolve_promise(o): o = [resolve_promise(x) for x in o] elif isinstance(o, Promise): try: - o = force_unicode(o) + o = force_text(o) except: # Item could be a lazy tuple or list try: