Commit 02084e12 authored by Serge S. Koval's avatar Serge S. Koval

Merge pull request #1241 from pawl/remove_unnecessary_indention

remove unnecessary indention in form.py convert
parents 624b0d8e 03a2bc6a
...@@ -147,118 +147,116 @@ class AdminModelConverter(ModelConverterBase): ...@@ -147,118 +147,116 @@ class AdminModelConverter(ModelConverterBase):
# Check if it is relation or property # Check if it is relation or property
if hasattr(prop, 'direction'): if hasattr(prop, 'direction'):
return self._convert_relation(prop, kwargs) return self._convert_relation(prop, kwargs)
else: elif hasattr(prop, 'columns'): # Ignore pk/fk
# Ignore pk/fk # Check if more than one column mapped to the property
if hasattr(prop, 'columns'): if len(prop.columns) > 1:
# Check if more than one column mapped to the property columns = filter_foreign_columns(model.__table__, prop.columns)
if len(prop.columns) > 1:
columns = filter_foreign_columns(model.__table__, prop.columns) if len(columns) > 1:
warnings.warn('Can not convert multiple-column properties (%s.%s)' % (model, prop.key))
if len(columns) > 1: return None
warnings.warn('Can not convert multiple-column properties (%s.%s)' % (model, prop.key))
return None column = columns[0]
else:
# Grab column
column = prop.columns[0]
form_columns = getattr(self.view, 'form_columns', None) or ()
# Do not display foreign keys - use relations, except when explicitly instructed
if column.foreign_keys and prop.key not in form_columns:
return None
# Only display "real" columns
if not isinstance(column, Column):
return None
unique = False
column = columns[0] if column.primary_key:
if hidden_pk:
# If requested to add hidden field, show it
return fields.HiddenField()
else: else:
# Grab column # By default, don't show primary keys either
column = prop.columns[0] # If PK is not explicitly allowed, ignore it
if prop.key not in form_columns:
return None
form_columns = getattr(self.view, 'form_columns', None) or () # Current Unique Validator does not work with multicolumns-pks
if not has_multiple_pks(model):
kwargs['validators'].append(Unique(self.session,
model,
column))
unique = True
# If field is unique, validate it
if column.unique and not unique:
kwargs['validators'].append(Unique(self.session,
model,
column))
optional_types = getattr(self.view, 'form_optional_types', (Boolean,))
if (
not column.nullable
and not isinstance(column.type, optional_types)
and not column.default
and not column.server_default
):
kwargs['validators'].append(validators.InputRequired())
# Do not display foreign keys - use relations, except when explicitly instructed # Apply label and description if it isn't inline form field
if column.foreign_keys and prop.key not in form_columns: if self.view.model == mapper.class_:
return None kwargs['label'] = self._get_label(prop.key, kwargs)
kwargs['description'] = self._get_description(prop.key, kwargs)
# Only display "real" columns # Figure out default value
if not isinstance(column, Column): default = getattr(column, 'default', None)
return None value = None
unique = False if default is not None:
value = getattr(default, 'arg', None)
if column.primary_key: if value is not None:
if hidden_pk: if getattr(default, 'is_callable', False):
# If requested to add hidden field, show it value = lambda: default.arg(None)
return fields.HiddenField()
else: else:
# By default, don't show primary keys either if not getattr(default, 'is_scalar', True):
# If PK is not explicitly allowed, ignore it value = None
if prop.key not in form_columns:
return None
# Current Unique Validator does not work with multicolumns-pks
if not has_multiple_pks(model):
kwargs['validators'].append(Unique(self.session,
model,
column))
unique = True
# If field is unique, validate it
if column.unique and not unique:
kwargs['validators'].append(Unique(self.session,
model,
column))
optional_types = getattr(self.view, 'form_optional_types', (Boolean,))
if (
not column.nullable
and not isinstance(column.type, optional_types)
and not column.default
and not column.server_default
):
kwargs['validators'].append(validators.InputRequired())
# Apply label and description if it isn't inline form field
if self.view.model == mapper.class_:
kwargs['label'] = self._get_label(prop.key, kwargs)
kwargs['description'] = self._get_description(prop.key, kwargs)
# Figure out default value
default = getattr(column, 'default', None)
value = None
if default is not None:
value = getattr(default, 'arg', None)
if value is not None:
if getattr(default, 'is_callable', False):
value = lambda: default.arg(None)
else:
if not getattr(default, 'is_scalar', True):
value = None
if value is not None: if value is not None:
kwargs['default'] = value kwargs['default'] = value
# Check nullable # Check nullable
if column.nullable: if column.nullable:
kwargs['validators'].append(validators.Optional()) kwargs['validators'].append(validators.Optional())
# Override field type if necessary # Override field type if necessary
override = self._get_field_override(prop.key) override = self._get_field_override(prop.key)
if override: if override:
return override(**kwargs) return override(**kwargs)
# Check choices # Check choices
form_choices = getattr(self.view, 'form_choices', None) form_choices = getattr(self.view, 'form_choices', None)
if mapper.class_ == self.view.model and form_choices: if mapper.class_ == self.view.model and form_choices:
choices = form_choices.get(column.key) choices = form_choices.get(column.key)
if choices: if choices:
return form.Select2Field( return form.Select2Field(
choices=choices, choices=choices,
allow_blank=column.nullable, allow_blank=column.nullable,
**kwargs **kwargs
) )
# Run converter # Run converter
converter = self.get_converter(column) converter = self.get_converter(column)
if converter is None: if converter is None:
return None return None
return converter(model=model, mapper=mapper, prop=prop, return converter(model=model, mapper=mapper, prop=prop,
column=column, field_args=kwargs) column=column, field_args=kwargs)
return None return None
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment