From f802e30297e8901df67b06bfa9aba7fa49bceae2 Mon Sep 17 00:00:00 2001 From: GabrielTaufer Date: Tue, 22 Sep 2020 21:41:58 -0300 Subject: [PATCH 1/8] Fixed datetime initial value verification --- django_remote_forms/fields.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/django_remote_forms/fields.py b/django_remote_forms/fields.py index 4e0d33a..e27170e 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: @@ -104,7 +104,7 @@ def as_dict(self): # If initial value is datetime then convert it using first available input format if (isinstance(field_dict['initial'], (datetime.datetime, datetime.time, datetime.date))): - if not len(field_dict['input_formats']): + if not getattr(field_dict['input_formats'], 'len', None): if isinstance(field_dict['initial'], datetime.date): field_dict['input_formats'] = settings.DATE_INPUT_FORMATS elif isinstance(field_dict['initial'], datetime.time): From 1ce0a49da9e70d236ebae72c902f8afab64ffb48 Mon Sep 17 00:00:00 2001 From: GabrielTaufer Date: Tue, 22 Sep 2020 21:43:05 -0300 Subject: [PATCH 2/8] Fix exception error syntax --- django_remote_forms/forms.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/django_remote_forms/forms.py b/django_remote_forms/forms.py index bebb1b7..ee736dd 100644 --- a/django_remote_forms/forms.py +++ b/django_remote_forms/forms.py @@ -51,7 +51,7 @@ def __init__(self, form, *args, **kwargs): self.excluded_fields |= (self.included_fields - self.all_fields) if not self.ordered_fields: - if self.form.fields.keyOrder: + if hasattr(self.form.fields, 'keyOrder'): self.ordered_fields = self.form.fields.keyOrder else: self.ordered_fields = self.form.fields.keys() @@ -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 d31fd851e40a08c1a23b4a75197bcd2181824a4f Mon Sep 17 00:00:00 2001 From: GabrielTaufer Date: Tue, 22 Sep 2020 21:43:49 -0300 Subject: [PATCH 3/8] Change force_unicode to force_text import from django.utils.encoding --- django_remote_forms/utils.py | 4 ++-- django_remote_forms/widgets.py | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/django_remote_forms/utils.py b/django_remote_forms/utils.py index 0391fa0..cc47c2a 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): @@ -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: diff --git a/django_remote_forms/widgets.py b/django_remote_forms/widgets.py index 3ca30e6..d66a3fb 100644 --- a/django_remote_forms/widgets.py +++ b/django_remote_forms/widgets.py @@ -112,8 +112,6 @@ def as_dict(self): widget_dict = super(RemoteTimeInput, self).as_dict() widget_dict['format'] = self.widget.format - widget_dict['manual_format'] = self.widget.manual_format - widget_dict['date'] = self.widget.manual_format widget_dict['input_type'] = 'time' return widget_dict From bf56787d6f92116146fa209f1aa678c7f2fc4486 Mon Sep 17 00:00:00 2001 From: GabrielTaufer Date: Mon, 12 Oct 2020 10:41:06 -0300 Subject: [PATCH 4/8] Form as dict hotfix --- django_remote_forms/forms.py | 12 +++++++++--- django_remote_forms/utils.py | 3 +++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/django_remote_forms/forms.py b/django_remote_forms/forms.py index ee736dd..6087373 100644 --- a/django_remote_forms/forms.py +++ b/django_remote_forms/forms.py @@ -10,6 +10,7 @@ def __init__(self, form, *args, **kwargs): self.all_fields = set(self.form.fields.keys()) + self.title = kwargs.pop('title', None) self.excluded_fields = set(kwargs.pop('exclude', [])) self.included_fields = set(kwargs.pop('include', [])) self.readonly_fields = set(kwargs.pop('readonly', [])) @@ -108,7 +109,8 @@ def as_dict(self): } """ form_dict = OrderedDict() - form_dict['title'] = self.form.__class__.__name__ + + form_dict['title'] = self.title if self.title else self.form.__class__.__name__ form_dict['non_field_errors'] = self.form.non_field_errors() form_dict['label_suffix'] = self.form.label_suffix form_dict['is_bound'] = self.form.is_bound @@ -123,6 +125,7 @@ def as_dict(self): initial_data = {} for name, field in [(x, self.form.fields[x]) for x in self.fields]: + print(' ', name, field) # 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. @@ -153,11 +156,14 @@ def as_dict(self): if 'initial' not in form_dict['fields'][name]: form_dict['fields'][name]['initial'] = None - initial_data[name] = form_dict['fields'][name]['initial'] + # Remove the coerce key from the field, it will not be used + if form_dict['fields'][name].get('coerce'): + form_dict['fields'][name].pop('coerce') + initial_data[name] = form_dict['fields'][name]['initial'] if self.form.data: form_dict['data'] = self.form.data else: form_dict['data'] = initial_data - + print(form_dict) return resolve_promise(form_dict) diff --git a/django_remote_forms/utils.py b/django_remote_forms/utils.py index cc47c2a..b380bfd 100644 --- a/django_remote_forms/utils.py +++ b/django_remote_forms/utils.py @@ -1,11 +1,14 @@ from django.utils.functional import Promise from django.utils.encoding import force_text +from django.forms.models import ModelChoiceIteratorValue def resolve_promise(o): if isinstance(o, dict): for k, v in o.items(): o[k] = resolve_promise(v) + if isinstance(o[k], ModelChoiceIteratorValue): + o[k] = getattr(o[k], 'value') elif isinstance(o, (list, tuple)): o = [resolve_promise(x) for x in o] elif isinstance(o, Promise): From c6f0cabbb4f6d3fe1c7db036060e8344fbf5a6da Mon Sep 17 00:00:00 2001 From: GabrielTaufer Date: Mon, 12 Oct 2020 13:52:40 -0300 Subject: [PATCH 5/8] Add model and APP name to RemoteModelChoiceField --- django_remote_forms/fields.py | 17 +++++++++++++++-- django_remote_forms/forms.py | 5 ++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/django_remote_forms/fields.py b/django_remote_forms/fields.py index e27170e..ad6ac74 100644 --- a/django_remote_forms/fields.py +++ b/django_remote_forms/fields.py @@ -175,7 +175,6 @@ def as_dict(self): class RemoteChoiceField(RemoteField): def as_dict(self): field_dict = super(RemoteChoiceField, self).as_dict() - field_dict['choices'] = [] for key, value in self.field.choices: field_dict['choices'].append({ @@ -188,7 +187,21 @@ def as_dict(self): class RemoteModelChoiceField(RemoteChoiceField): def as_dict(self): - return super(RemoteModelChoiceField, self).as_dict() + form_as_dict = super(RemoteModelChoiceField, self).as_dict() + + field = self.__dict__.get('field', {}) + if hasattr(field, '_queryset'): + queryset = self.__dict__['field'].__dict__['_queryset'] + model = queryset.model() + app_name = model.app_name if hasattr(model, 'app_name') else None + model_name = model.model_name if hasattr(model, 'model_name') else None + + form_as_dict.update({ + 'app_name' : app_name, + 'model_name' : model_name, + }) + + return form_as_dict class RemoteTypedChoiceField(RemoteChoiceField): diff --git a/django_remote_forms/forms.py b/django_remote_forms/forms.py index 6087373..ae5a796 100644 --- a/django_remote_forms/forms.py +++ b/django_remote_forms/forms.py @@ -124,8 +124,7 @@ def as_dict(self): initial_data = {} - for name, field in [(x, self.form.fields[x]) for x in self.fields]: - print(' ', name, field) + 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. @@ -165,5 +164,5 @@ def as_dict(self): form_dict['data'] = self.form.data else: form_dict['data'] = initial_data - print(form_dict) + return resolve_promise(form_dict) From 9470a8c62e74a35c64efe14e9746bb4d67340a9e Mon Sep 17 00:00:00 2001 From: GabrielTaufer Date: Wed, 4 Nov 2020 22:47:13 -0300 Subject: [PATCH 6/8] Fix form FKs translation --- django_remote_forms/fields.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/django_remote_forms/fields.py b/django_remote_forms/fields.py index ad6ac74..ce6595f 100644 --- a/django_remote_forms/fields.py +++ b/django_remote_forms/fields.py @@ -6,6 +6,7 @@ from django_remote_forms import logger, widgets +from django.utils.translation import gettext as _ class RemoteField(object): """ @@ -28,7 +29,7 @@ def as_dict(self): field_dict = OrderedDict() field_dict['title'] = self.field.__class__.__name__ field_dict['required'] = self.field.required - field_dict['label'] = self.field.label + field_dict['label'] = _(' '.join(list(map(lambda x: x.capitalize(), self.field.label.split(' '))))) field_dict['initial'] = self.form_initial_data or self.field.initial field_dict['help_text'] = self.field.help_text @@ -125,6 +126,7 @@ def as_dict(self): class RemoteDateTimeField(RemoteTimeField): def as_dict(self): + print('RemoteDateTimeField') return super(RemoteDateTimeField, self).as_dict() From 12154301e85c88d1177bdce9a00f6b7500b7b34e Mon Sep 17 00:00:00 2001 From: GabrielTaufer Date: Thu, 5 Nov 2020 22:00:31 -0300 Subject: [PATCH 7/8] Add new model instance treatment on resolve_promise function --- django_remote_forms/utils.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/django_remote_forms/utils.py b/django_remote_forms/utils.py index b380bfd..af30401 100644 --- a/django_remote_forms/utils.py +++ b/django_remote_forms/utils.py @@ -1,7 +1,7 @@ from django.utils.functional import Promise from django.utils.encoding import force_text from django.forms.models import ModelChoiceIteratorValue - +from django.db import models def resolve_promise(o): if isinstance(o, dict): @@ -22,5 +22,9 @@ def resolve_promise(o): raise Exception('Unable to resolve lazy object %s' % o) elif callable(o): o = o() - + elif isinstance(o, models.Model): # New treatment for models instances, return the model instance id + try: + o = o.id + except Exception as e: + print(e) return o From c18c28902d6df94744026f20c202b2c4bf769d76 Mon Sep 17 00:00:00 2001 From: Lucas Soranzo Date: Tue, 15 Dec 2020 21:53:23 -0300 Subject: [PATCH 8/8] Implement field subclass possibility --- django_remote_forms/fields.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/django_remote_forms/fields.py b/django_remote_forms/fields.py index ad6ac74..572de8e 100644 --- a/django_remote_forms/fields.py +++ b/django_remote_forms/fields.py @@ -37,6 +37,9 @@ def as_dict(self): # Instantiate the Remote Forms equivalent of the widget if possible # in order to retrieve the widget contents as a dictionary. remote_widget_class_name = 'Remote%s' % self.field.widget.__class__.__name__ + if hasattr(self.field.widget, 'parent_class'): + remote_widget_class_name = 'Remote%s' % self.field.widget.parent_class + try: remote_widget_class = getattr(widgets, remote_widget_class_name) remote_widget = remote_widget_class(self.field.widget, field_name=self.field_name) @@ -48,6 +51,9 @@ def as_dict(self): field_dict['widget'] = widget_dict + if hasattr(self.field.widget, 'input_type'): + field_dict['widget']['input_type'] = self.field.widget.input_type + return field_dict @@ -223,7 +229,21 @@ def as_dict(self): class RemoteModelMultipleChoiceField(RemoteMultipleChoiceField): def as_dict(self): - return super(RemoteModelMultipleChoiceField, self).as_dict() + form_as_dict = super(RemoteModelMultipleChoiceField, self).as_dict() + + field = self.__dict__.get('field', {}) + if hasattr(field, '_queryset'): + queryset = self.__dict__['field'].__dict__['_queryset'] + model = queryset.model() + app_name = model.app_name if hasattr(model, 'app_name') else None + model_name = model.model_name if hasattr(model, 'model_name') else None + + form_as_dict.update({ + 'app_name' : app_name, + 'model_name' : model_name, + }) + + return form_as_dict class RemoteTypedMultipleChoiceField(RemoteMultipleChoiceField):