Formatting cleanup in widgets.py

This commit is contained in:
Swen Kooij 2017-07-15 13:48:54 +03:00
parent e8e044f6e2
commit 968840188d

View File

@ -23,9 +23,9 @@ class LocalizedFieldWidget(forms.MultiWidget):
super().__init__(initial_widgets, *args, **kwargs) super().__init__(initial_widgets, *args, **kwargs)
for ((lc, ln), w) in zip(settings.LANGUAGES, self.widgets): for ((lang_code, lang_name), widget) in zip(settings.LANGUAGES, self.widgets):
w.attrs['lang_code'] = lc widget.attrs['lang_code'] = lang_code
w.attrs['lang_name'] = ln widget.attrs['lang_name'] = lang_name
def decompress(self, value: LocalizedValue) -> List[str]: def decompress(self, value: LocalizedValue) -> List[str]:
"""Decompresses the specified value so """Decompresses the specified value so
@ -68,13 +68,16 @@ class AdminLocalizedFieldWidget(LocalizedFieldWidget):
if self.is_localized: if self.is_localized:
for widget in self.widgets: for widget in self.widgets:
widget.is_localized = self.is_localized widget.is_localized = self.is_localized
# value is a list of values, each corresponding to a widget # value is a list of values, each corresponding to a widget
# in self.widgets. # in self.widgets.
if not isinstance(value, list): if not isinstance(value, list):
value = self.decompress(value) value = self.decompress(value)
output = [] output = []
final_attrs = self.build_attrs(attrs) final_attrs = self.build_attrs(attrs)
id_ = final_attrs.get('id') id_ = final_attrs.get('id')
for i, widget in enumerate(self.widgets): for i, widget in enumerate(self.widgets):
try: try:
widget_value = value[i] widget_value = value[i]
@ -82,22 +85,27 @@ class AdminLocalizedFieldWidget(LocalizedFieldWidget):
widget_value = None widget_value = None
if id_: if id_:
final_attrs = dict(final_attrs, id='%s_%s' % (id_, i)) final_attrs = dict(final_attrs, id='%s_%s' % (id_, i))
widget_attrs = self.build_widget_attrs(widget, widget_value, final_attrs) widget_attrs = self.build_widget_attrs(widget, widget_value, final_attrs)
output.append(widget.render(name + '_%s' % i, widget_value, widget_attrs)) output.append(widget.render(name + '_%s' % i, widget_value, widget_attrs))
context = { context = {
'id': final_attrs.get('id'), 'id': final_attrs.get('id'),
'name': name, 'name': name,
'widgets': zip([code for code, lang in settings.LANGUAGES], output), 'widgets': zip([code for code, lang in settings.LANGUAGES], output),
'available_languages': settings.LANGUAGES 'available_languages': settings.LANGUAGES
} }
return render_to_string(self.template, context) return render_to_string(self.template, context)
@staticmethod @staticmethod
def build_widget_attrs(widget, value, attrs): def build_widget_attrs(widget, value, attrs):
attrs = dict(attrs) # Copy attrs to avoid modifying the argument. attrs = dict(attrs) # Copy attrs to avoid modifying the argument.
if (not widget.use_required_attribute(value) or not widget.is_required) \ if (not widget.use_required_attribute(value) or not widget.is_required) \
and 'required' in attrs: and 'required' in attrs:
del attrs['required'] del attrs['required']
return attrs return attrs