Commit 5873ecde authored by John Kirkwood's avatar John Kirkwood

Tidy code #1 37d14937#note_601

parent 37d14937
Pipeline #1281 passed with stage
in 42 seconds
...@@ -17,6 +17,7 @@ class Factory: ...@@ -17,6 +17,7 @@ class Factory:
@staticmethod @staticmethod
def get_label(field): def get_label(field):
"""Return a field label with suffix."""
# code taken from Boundfield.label_tag() # code taken from Boundfield.label_tag()
contents = field.label contents = field.label
label_suffix = (field.field.label_suffix label_suffix = (field.field.label_suffix
...@@ -32,43 +33,50 @@ class Factory: ...@@ -32,43 +33,50 @@ class Factory:
@staticmethod @staticmethod
def render(field, widget=None, attrs=None, only_initial=False): def render(field, widget=None, attrs=None, only_initial=False):
"""Introspect the field and call an MDC render method.""" """Introspect the field and call an MDC render method.
if isinstance(field, BoundField):
widget_type = field.field.widget.__class__.__name__ Prepare the widget attrs, field label and context first.
mdc_render = getattr( """
Factory, f"mdc_{widget_type.lower()}", None) if not isinstance(field, BoundField):
if mdc_render is not None: raise NotImplementedError
# code from from boundfield.as_widget()
widget = field.field.widget widget_type = field.field.widget.__class__.__name__
if field.field.localize: mdc_render = getattr(
widget.is_localized = True Factory, f"mdc_{widget_type.lower()}", None)
attrs = attrs or {} if mdc_render is None:
attrs = field.build_widget_attrs(attrs, widget) raise NotImplementedError
if field.auto_id and 'id' not in widget.attrs:
attrs.setdefault('id', # code from from boundfield.as_widget()
field.html_initial_id widget = field.field.widget
if only_initial else field.auto_id) if field.field.localize:
context = widget.get_context( widget.is_localized = True
name=(field.html_initial_name attrs = attrs or {}
if only_initial else field.html_name), attrs = field.build_widget_attrs(attrs, widget)
value=field.value(), if field.auto_id and 'id' not in widget.attrs:
attrs=attrs attrs.setdefault('id',
) field.html_initial_id
label = Factory.get_label(field) if only_initial else field.auto_id)
for_id = widget.attrs.get('id') or field.auto_id context = widget.get_context(
context.update( name=(field.html_initial_name
{'label': label, if only_initial else field.html_name),
'for': widget.id_for_label(for_id), value=field.value(),
'type': attrs=attrs
context['widget'].get('type', )
field.field.widget.input_type), label = Factory.get_label(field)
}) for_id = widget.attrs.get('id') or field.auto_id
context.update(
return mdc_render(context) {'label': label,
raise NotImplementedError 'for': widget.id_for_label(for_id),
'type':
context['widget'].get('type',
field.field.widget.input_type),
})
return mdc_render(context)
@staticmethod @staticmethod
def mdc_checkboxinput(context): def mdc_checkboxinput(context):
"""Render a BooleanField/CheckBoxInput field/widget."""
mdc_type = "checkbox" mdc_type = "checkbox"
props, children = [], [] props, children = [], []
...@@ -84,6 +92,7 @@ class Factory: ...@@ -84,6 +92,7 @@ class Factory:
@staticmethod @staticmethod
def mdc_textinput(context): def mdc_textinput(context):
"""Render a CharField/TextInput field/widget."""
mdc_type = "text" mdc_type = "text"
props, children = [], [] props, children = [], []
...@@ -100,6 +109,7 @@ class Factory: ...@@ -100,6 +109,7 @@ class Factory:
@staticmethod @staticmethod
def mdc_dateinput(context): def mdc_dateinput(context):
"""Render a DateField/DateInput field/widget."""
mdc_type = "date" mdc_type = "date"
props, children = [], [] props, children = [], []
...@@ -116,6 +126,8 @@ class Factory: ...@@ -116,6 +126,8 @@ class Factory:
@staticmethod @staticmethod
def mdc_select(context): def mdc_select(context):
"""Render a CharField/choices or ForeignKey/Select field/widget.
"""
mdc_type = "select" mdc_type = "select"
props, children = [], [] props, children = [], []
props.append( props.append(
......
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