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['initial'] = self.form_initial_data or self.field.initial field_dict['help_text'] = self.field.help_text field_dict['error_messages'] = self.field.error_messages # 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__ 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 as e: logger.warning('Error serializing %s: %s', remote_widget_class_name, str(e)) widget_dict = {} else: widget_dict = remote_widget.as_dict() field_dict['widget'] = widget_dict return field_dict
def as_dict(self): field_dict = SortedDict() field_dict['title'] = self.field.__class__.__name__ field_dict['required'] = self.field.required field_dict['label'] = self.field.label field_dict['initial'] = self.form_initial_data or self.field.initial field_dict['help_text'] = self.field.help_text field_dict['error_messages'] = self.field.error_messages # 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__ 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 as e: logger.warning('Error serializing %s: %s', remote_widget_class_name, str(e)) widget_dict = {} else: widget_dict = remote_widget.as_dict() field_dict['widget'] = widget_dict return field_dict
def as_dict(self): field_dict = OrderedDict() field_dict["type"] = self.field.__class__.__name__ field_dict["required"] = self.field.required field_dict["label"] = self.field.label field_dict[ "initial"] = self.form_initial_data if self.form_initial_data is not None else self.field.initial field_dict["help_text"] = self.field.help_text field_dict["error_messages"] = self.field.error_messages # 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__ 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 as e: logger.warning("Error serializing %s: %s", remote_widget_class_name, str(e)) widget_dict = {} else: widget_dict = remote_widget.as_dict() field_dict["widget"] = widget_dict return field_dict
def __init__(self, form, *args, **kwargs): self.form = form self._config = kwargs.pop('config', {}) self.all_fields = set(self.form.fields.keys()) self.excluded_fields = set(kwargs.pop('exclude', [])) self.included_fields = set(kwargs.pop('include', [])) self.readonly_fields = set(kwargs.pop('readonly', [])) self.ordered_fields = kwargs.pop('ordering', []) self.fieldsets = [] if(hasattr(self.form, 'Meta')): self.fieldsets = getattr(self.form.Meta, 'fieldsets', []) # Make sure all passed field lists are valid if self.excluded_fields and not (self.all_fields >= self.excluded_fields): logger.warning('Excluded fields %s are not present in form fields' % (self.excluded_fields - self.all_fields)) self.excluded_fields = set() if self.included_fields and not (self.all_fields >= self.included_fields): logger.warning('Included fields %s are not present in form fields' % (self.included_fields - self.all_fields)) self.included_fields = set() if self.readonly_fields and not (self.all_fields >= self.readonly_fields): logger.warning('Readonly fields %s are not present in form fields' % (self.readonly_fields - self.all_fields)) self.readonly_fields = set() if self.ordered_fields and not (self.all_fields >= set(self.ordered_fields)): logger.warning('Readonly fields %s are not present in form fields' % (set(self.ordered_fields) - self.all_fields)) self.ordered_fields = [] if self.included_fields | self.excluded_fields: logger.warning('Included and excluded fields have following fields %s in common' % (set(self.ordered_fields) - self.all_fields)) self.excluded_fields = set() self.included_fields = set() # Extend exclude list from include list self.excluded_fields |= (self.included_fields - self.all_fields) if not self.ordered_fields: try: self.ordered_fields = self.form.fields.keyOrder except AttributeError: self.ordered_fields = self.form.fields.keys() self.fields = [] # Construct ordered field list considering exclusions for field_name in self.ordered_fields: if field_name in self.excluded_fields: continue self.fields.append(field_name)
def as_dict(self): """ Returns a form as a dictionary that looks like the following: form = { 'non_field_errors': [], 'label_suffix': ':', 'is_bound': False, 'prefix': 'text'. 'fields': { 'name': { 'type': 'type', 'errors': {}, 'help_text': 'text', 'label': 'text', 'initial': 'data', 'max_length': 'number', 'min_length: 'number', 'required': False, 'bound_data': 'data' 'widget': { 'attr': 'value' } } } } """ form_dict = SortedDict() form_dict['title'] = 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 form_dict['prefix'] = self.form.prefix form_dict['fields'] = SortedDict() form_dict['errors'] = self.form.errors fieldset_list = [] for fieldset_name, fieldset_data in self.fieldsets: fieldset_data.update({ 'key': fieldset_name, }) fieldset_list.append(fieldset_data) form_dict['fieldsets'] = fieldset_list initial_data = {} for bound_field in (x for x in self.form if x.name 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. # 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 form_initial_field_data = self.form.initial.get(bound_field.name) # Instantiate the Remote Forms equivalent of the field if possible # in order to retrieve the field contents as a dictionary. # Use config to to check for any serializer overrides. field_class_name = bound_field.field.__class__.__name__ if self._config.get('fields', {}).get(field_class_name): remote_field_class = self._config['fields'][field_class_name] else: remote_field_class = getattr(fields, 'Remote%s' % field_class_name) try: remote_field = remote_field_class(bound_field, form_initial_field_data) except Exception, e: logger.warning('Error serializing field %s: %s', remote_field_class, str(e)) field_dict = {} else: field_dict = remote_field.as_dict() if bound_field.name in self.readonly_fields: field_dict['readonly'] = True form_dict['fields'][bound_field.name] = field_dict widget_class_name = bound_field.field.widget.__class__.__name__ if self._config.get('widgets', {}).get(widget_class_name): remote_widget_class = self._config['widgets'][widget_class_name] else: remote_widget_class = getattr(widgets, 'Remote%s' % widget_class_name) try: remote_widget = remote_widget_class(remote_field.widget, name=remote_field.name, required=remote_field.required) except Exception, e: logger.error('Error serializing %s: %s', remote_widget_class, str(e)) widget_dict = {}
def as_dict(self): """ Returns a form as a dictionary that looks like the following: form = { 'non_field_errors': [], 'label_suffix': ':', 'is_bound': False, 'prefix': 'text'. 'fields': { 'name': { 'type': 'type', 'errors': {}, 'help_text': 'text', 'label': 'text', 'initial': 'data', 'max_length': 'number', 'min_length: 'number', 'required': False, 'bound_data': 'data' 'widget': { 'attr': 'value' } } } } """ form_dict = OrderedDict() form_dict['title'] = self.form.__class__.__name__ form_dict['errors'] = self.form.errors 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 form_dict['prefix'] = self.form.prefix form_dict['fields'] = OrderedDict() form_dict['fieldsets'] = getattr(self.form, 'fieldsets', []) # If there are no fieldsets, specify order form_dict['ordered_fields'] = self.fields initial_data = {} 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. # 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 form_initial_field_data = self.form.initial.get(name) # Instantiate the Remote Forms equivalent of the field if possible # in order to retrieve the field contents as a dictionary. remote_field_class_name = 'Remote%s' % field.__class__.__name__ 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 as e: logger.warning('Error serializing field %s: %s', remote_field_class_name, str(e)) field_dict = {} else: field_dict = remote_field.as_dict() if name in self.readonly_fields: field_dict['readonly'] = True form_dict['fields'][name] = field_dict # Load the initial data, which is a conglomerate of form initial and field initial if 'initial' not in form_dict['fields'][name]: form_dict['fields'][name]['initial'] = None initial_data[name] = form_dict['fields'][name]['initial'] if self.form.data: form_dict['data'] = self.form.data else: form_dict['data'] = initial_data gaura = resolve_promise(form_dict) #print(gaura) return resolve_promise(form_dict)
def __init__(self, form, *args, **kwargs): self.form = form self.all_fields = set(self.form.fields.keys()) self.excluded_fields = set(kwargs.pop('exclude', [])) self.included_fields = set(kwargs.pop('include', [])) self.readonly_fields = set(kwargs.pop('readonly', [])) self.ordered_fields = kwargs.pop('ordering', []) self.fieldsets = kwargs.pop('fieldsets', {}) # Make sure all passed field lists are valid if self.excluded_fields and not (self.all_fields >= self.excluded_fields): logger.warning( 'Excluded fields %s are not present in form fields' % (self.excluded_fields - self.all_fields)) self.excluded_fields = set() if self.included_fields and not (self.all_fields >= self.included_fields): logger.warning( 'Included fields %s are not present in form fields' % (self.included_fields - self.all_fields)) self.included_fields = set() if self.readonly_fields and not (self.all_fields >= self.readonly_fields): logger.warning( 'Readonly fields %s are not present in form fields' % (self.readonly_fields - self.all_fields)) self.readonly_fields = set() if self.ordered_fields and not (self.all_fields >= set( self.ordered_fields)): logger.warning( 'Readonly fields %s are not present in form fields' % (set(self.ordered_fields) - self.all_fields)) self.ordered_fields = [] if self.included_fields | self.excluded_fields: logger.warning( 'Included and excluded fields have following fields %s in common' % (set(self.ordered_fields) - self.all_fields)) self.excluded_fields = set() self.included_fields = set() # Extend exclude list from include list self.excluded_fields |= (self.included_fields - self.all_fields) if not self.ordered_fields: if hasattr(self.form.fields, 'keyOrder'): self.ordered_fields = self.form.fields.keyOrder else: self.ordered_fields = self.form.fields.keys() self.fields = [] # Construct ordered field list considering exclusions for field_name in self.ordered_fields: if field_name in self.excluded_fields: continue self.fields.append(field_name) # Validate fieldset fieldset_fields = set() if self.fieldsets: for fieldset_name, fieldsets_data in self.fieldsets: if 'fields' in fieldsets_data: fieldset_fields |= set(fieldsets_data['fields']) if not (self.all_fields >= fieldset_fields): logger.warning('Following fieldset fields are invalid %s' % (fieldset_fields - self.all_fields)) self.fieldsets = {} if not (set(self.fields) >= fieldset_fields): logger.warning('Following fieldset fields are excluded %s' % (fieldset_fields - set(self.fields))) self.fieldsets = {}
def as_dict(self): """ Returns a form as a dictionary that looks like the following: form = { 'non_field_errors': [], 'label_suffix': ':', 'is_bound': False, 'prefix': 'text'. 'fields': { 'name': { 'type': 'type', 'errors': {}, 'help_text': 'text', 'label': 'text', 'initial': 'data', 'max_length': 'number', 'min_length: 'number', 'required': False, 'bound_data': 'data' 'widget': { 'attr': 'value' } } } } """ form_dict = SortedDict() form_dict['title'] = 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 form_dict['prefix'] = self.form.prefix form_dict['fields'] = SortedDict() form_dict['errors'] = self.form.errors form_dict['fieldsets'] = getattr(self.form, 'fieldsets', []) # If there are no fieldsets, specify order form_dict['ordered_fields'] = self.fields initial_data = {} 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. # 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 form_initial_field_data = self.form.initial.get(name) # Instantiate the Remote Forms equivalent of the field if possible # in order to retrieve the field contents as a dictionary. remote_field_class_name = 'Remote%s' % field.__class__.__name__ 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: logger.warning('Error serializing field %s: %s', remote_field_class_name, str(e)) field_dict = {} else: field_dict = remote_field.as_dict() if name in self.readonly_fields: field_dict['readonly'] = True form_dict['fields'][name] = field_dict # Load the initial data, which is a conglomerate of form initial and field initial if 'initial' not in form_dict['fields'][name]: form_dict['fields'][name]['initial'] = None initial_data[name] = form_dict['fields'][name]['initial']
def __init__(self, form, *args, **kwargs): self.form = form self.all_fields = set(self.form.fields.keys()) self.excluded_fields = set(kwargs.pop('exclude', [])) self.included_fields = set(kwargs.pop('include', [])) self.readonly_fields = set(kwargs.pop('readonly', [])) self.ordered_fields = kwargs.pop('ordering', []) self.fieldsets = kwargs.pop('fieldsets', {}) # Make sure all passed field lists are valid if self.excluded_fields and not (self.all_fields >= self.excluded_fields): logger.warning('Excluded fields %s are not present in form fields' % (self.excluded_fields - self.all_fields)) self.excluded_fields = set() if self.included_fields and not (self.all_fields >= self.included_fields): logger.warning('Included fields %s are not present in form fields' % (self.included_fields - self.all_fields)) self.included_fields = set() if self.readonly_fields and not (self.all_fields >= self.readonly_fields): logger.warning('Readonly fields %s are not present in form fields' % (self.readonly_fields - self.all_fields)) self.readonly_fields = set() if self.ordered_fields and not (self.all_fields >= set(self.ordered_fields)): logger.warning('Readonly fields %s are not present in form fields' % (set(self.ordered_fields) - self.all_fields)) self.ordered_fields = [] if self.included_fields | self.excluded_fields: logger.warning('Included and excluded fields have following fields %s in common' % (set(self.ordered_fields) - self.all_fields)) self.excluded_fields = set() self.included_fields = set() # Extend exclude list from include list self.excluded_fields |= (self.included_fields - self.all_fields) if not self.ordered_fields: if hasattr(self.form.fields, 'keyOrder'): self.ordered_fields = self.form.fields.keyOrder else: self.ordered_fields = self.form.fields.keys() self.fields = [] # Construct ordered field list considering exclusions for field_name in self.ordered_fields: if field_name in self.excluded_fields: continue self.fields.append(field_name) # Validate fieldset fieldset_fields = set() if self.fieldsets: for fieldset_name, fieldsets_data in self.fieldsets: if 'fields' in fieldsets_data: fieldset_fields |= set(fieldsets_data['fields']) if not (self.all_fields >= fieldset_fields): logger.warning('Following fieldset fields are invalid %s' % (fieldset_fields - self.all_fields)) self.fieldsets = {} if not (set(self.fields) >= fieldset_fields): logger.warning('Following fieldset fields are excluded %s' % (fieldset_fields - set(self.fields))) self.fieldsets = {}
def __init__(self, form, *args, **kwargs): self.form = form self.all_fields = set(self.form.fields.keys()) self.excluded_fields = set(kwargs.pop("exclude", [])) self.included_fields = set(kwargs.pop("include", [])) self.readonly_fields = set(kwargs.pop("readonly", [])) self.ordered_fields = kwargs.pop("ordering", []) self.fieldsets = kwargs.pop("fieldsets", {}) # Make sure all passed field lists are valid if self.excluded_fields and not (self.all_fields >= self.excluded_fields): logger.warning( "Excluded fields %s are not present in form fields" % (self.excluded_fields - self.all_fields) ) self.excluded_fields = set() if self.included_fields and not (self.all_fields >= self.included_fields): logger.warning( "Included fields %s are not present in form fields" % (self.included_fields - self.all_fields) ) self.included_fields = set() if self.readonly_fields and not (self.all_fields >= self.readonly_fields): logger.warning( "Readonly fields %s are not present in form fields" % (self.readonly_fields - self.all_fields) ) self.readonly_fields = set() if self.ordered_fields and not (self.all_fields >= set(self.ordered_fields)): logger.warning( "Readonly fields %s are not present in form fields" % (set(self.ordered_fields) - self.all_fields) ) self.ordered_fields = [] if self.included_fields | self.excluded_fields: logger.warning( "Included and excluded fields have following fields %s in common" % (set(self.ordered_fields) - self.all_fields) ) self.excluded_fields = set() self.included_fields = set() # Extend exclude list from include list self.excluded_fields |= self.included_fields - self.all_fields if not self.ordered_fields: if "keyOrder" in self.form.fields and self.form.fields.keyOrder: self.ordered_fields = self.form.fields.keyOrder else: self.ordered_fields = self.form.fields.keys() self.fields = [] # Construct ordered field list considering exclusions for field_name in self.ordered_fields: if field_name in self.excluded_fields: continue self.fields.append(field_name) # Validate fieldset fieldset_fields = set() if self.fieldsets: for fieldset_name, fieldsets_data in self.fieldsets: if "fields" in fieldsets_data: fieldset_fields |= set(fieldsets_data["fields"]) if not (self.all_fields >= fieldset_fields): logger.warning("Following fieldset fields are invalid %s" % (fieldset_fields - self.all_fields)) self.fieldsets = {} if not (set(self.fields) >= fieldset_fields): logger.warning("Following fieldset fields are excluded %s" % (fieldset_fields - set(self.fields))) self.fieldsets = {}
def as_dict(self): """ Returns a form as a dictionary that looks like the following: form = { 'non_field_errors': [], 'label_suffix': ':', 'is_bound': False, 'prefix': 'text'. 'errors': {}, 'data': {}, 'fields': { 'name': { 'type': 'type', 'help_text': 'text', 'label': 'text', 'initial': 'data', 'max_length': 'number', 'min_length: 'number', 'required': False, 'widget': { 'attr': 'value' } } } } """ form_dict = OrderedDict() if isinstance(self.form, forms.formsets.BaseFormSet): form_dict = self.get_formset_dict(self.form) return form_dict form_dict["type"] = 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 form_dict["prefix"] = self.form.prefix form_dict["fields"] = OrderedDict() form_dict["errors"] = self.form.errors form_dict["fieldsets"] = getattr(self.form, "fieldsets", []) # If there are no fieldsets, specify order form_dict["ordered_fields"] = self.fields initial_data = {} 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. # 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 form_initial_field_data = self.form.initial.get(name) # Instantiate the Remote Forms equivalent of the field if possible # in order to retrieve the field contents as a dictionary. remote_field_class_name = "Remote%s" % field.__class__.__name__ try: remote_field_class = getattr(fields, remote_field_class_name, fields.RemoteField) remote_field = remote_field_class(field, form_initial_field_data, field_name=name) except Exception as e: logger.warning("Error serializing field %s: %s", remote_field_class_name, str(e)) field_dict = {} else: field_dict = remote_field.as_dict() if name in self.readonly_fields: field_dict["readonly"] = True form_dict["fields"][name] = field_dict # Load the initial data, which is a conglomerate of form initial and field initial if "initial" not in form_dict["fields"][name]: form_dict["fields"][name]["initial"] = None initial_data[name] = form_dict["fields"][name]["initial"] form_dict["data"] = self.get_form_data_without_prefix( self.form, initial_data) form_dict["nested"] = {} if hasattr(self.form, "nested"): if isinstance(self.form.nested, dict): for form in list(self.form.nested.values()): form_dict["nested"] = form_dict.setdefault("nested", {}) form_dict.update( self.get_nested_formset_dict(form, form_dict)) else: form_dict["nested"] = self.get_nested_formset_dict( self.form.nested, form_dict) return resolve_promise(form_dict)
def __init__(self, form, *args, **kwargs): self.form = form if isinstance(form, forms.formsets.BaseFormSet): return self.all_fields = set(self.form.fields.keys()) self.excluded_fields = set(kwargs.pop("exclude", [])) self.included_fields = set(kwargs.pop("include", [])) self.readonly_fields = set(kwargs.pop("readonly", [])) self.ordered_fields = kwargs.pop("ordering", []) self.fieldsets = kwargs.pop("fieldsets", {}) # Make sure all passed field lists are valid if self.excluded_fields and not (self.all_fields >= self.excluded_fields): logger.warning( "Excluded fields %s are not present in form fields" % (self.excluded_fields - self.all_fields)) self.excluded_fields = set() if self.included_fields and not (self.all_fields >= self.included_fields): logger.warning( "Included fields %s are not present in form fields" % (self.included_fields - self.all_fields)) self.included_fields = set() if self.readonly_fields and not (self.all_fields >= self.readonly_fields): logger.warning( "Readonly fields %s are not present in form fields" % (self.readonly_fields - self.all_fields)) self.readonly_fields = set() if self.ordered_fields and not (self.all_fields >= set( self.ordered_fields)): logger.warning( "Readonly fields %s are not present in form fields" % (set(self.ordered_fields) - self.all_fields)) self.ordered_fields = [] if self.included_fields | self.excluded_fields: logger.warning( "Included and excluded fields have following fields %s in common" % (set(self.ordered_fields) - self.all_fields)) self.excluded_fields = set() self.included_fields = set() # Extend exclude list from include list self.excluded_fields |= self.included_fields - self.all_fields if not self.ordered_fields: if hasattr(self.form.fields, "keyOrder"): self.ordered_fields = self.form.fields.keyOrder else: self.ordered_fields = list(self.form.fields.keys()) self.fields = [] # Construct ordered field list considering exclusions for field_name in self.ordered_fields: if field_name in self.excluded_fields: continue self.fields.append(field_name) # Validate fieldset fieldset_fields = set() if self.fieldsets: for fieldset_name, fieldsets_data in self.fieldsets: if "fields" in fieldsets_data: fieldset_fields |= set(fieldsets_data["fields"]) if not (self.all_fields >= fieldset_fields): logger.warning("Following fieldset fields are invalid %s" % (fieldset_fields - self.all_fields)) self.fieldsets = {} if not (set(self.fields) >= fieldset_fields): logger.warning("Following fieldset fields are excluded %s" % (fieldset_fields - set(self.fields))) self.fieldsets = {}