mirror of
				https://github.com/SectorLabs/django-localized-fields.git
				synced 2025-10-30 02:28:57 +03:00 
			
		
		
		
	Use template-based widget rendering in AdminLocalizedFieldWidget
This commit is contained in:
		| @@ -1,14 +1,16 @@ | ||||
| {% with widget_id=widget.attrs.id %} | ||||
| <div class="localized-fields-widget" role="tabs" data-synctabs="translation"> | ||||
|     <ul class="localized-fields-widget tabs"> | ||||
|     {% for key, lang in available_languages %} | ||||
|     {% for widget in widget.subwidgets %} | ||||
|         <li class="localized-fields-widget tab"> | ||||
|             <a href="#{{ id }}_{{ key }}">{{ lang|capfirst }}</a> | ||||
|             <a href="#{{ widget_id }}_{{ widget.attrs.lang_code }}">{{ widget.attrs.lang_name|capfirst }}</a> | ||||
|         </li> | ||||
|     {% endfor %} | ||||
|     </ul> | ||||
| {% for key, widget in widgets %} | ||||
|     <div role="tabpanel" id="{{ id }}_{{ key }}"> | ||||
|         {{ widget }} | ||||
| {% for widget in widget.subwidgets %} | ||||
|     <div role="tabpanel" id="{{ widget_id }}_{{ widget.attrs.lang_code }}"> | ||||
|         {% include widget.template_name %} | ||||
|     </div> | ||||
| {% endfor %} | ||||
| </div> | ||||
| {% endwith %} | ||||
|   | ||||
| @@ -3,7 +3,6 @@ from typing import List | ||||
| from django.conf import settings | ||||
| from django import forms | ||||
| from django.contrib.admin import widgets | ||||
| from django.template.loader import render_to_string | ||||
|  | ||||
| from .value import LocalizedValue | ||||
|  | ||||
| @@ -49,6 +48,16 @@ class LocalizedFieldWidget(forms.MultiWidget): | ||||
|  | ||||
|         return result | ||||
|  | ||||
|     @staticmethod | ||||
|     def build_widget_attrs(widget, value, attrs): | ||||
|         attrs = dict(attrs)  # Copy attrs to avoid modifying the argument. | ||||
|  | ||||
|         if (not widget.use_required_attribute(value) or not widget.is_required) \ | ||||
|                 and 'required' in attrs: | ||||
|             del attrs['required'] | ||||
|  | ||||
|         return attrs | ||||
|  | ||||
|  | ||||
| class LocalizedCharFieldWidget(LocalizedFieldWidget): | ||||
|     """Widget that has an input box for every language.""" | ||||
| @@ -61,52 +70,8 @@ class LocalizedFileWidget(LocalizedFieldWidget): | ||||
|  | ||||
|  | ||||
| class AdminLocalizedFieldWidget(LocalizedFieldWidget): | ||||
|     template_name = 'localized_fields/admin/widget.html' | ||||
|     widget = widgets.AdminTextareaWidget | ||||
|     template = 'localized_fields/admin/widget.html' | ||||
|  | ||||
|     def render(self, name, value, attrs=None): | ||||
|         if self.is_localized: | ||||
|             for widget in self.widgets: | ||||
|                 widget.is_localized = self.is_localized | ||||
|  | ||||
|         # value is a list of values, each corresponding to a widget | ||||
|         # in self.widgets. | ||||
|         if not isinstance(value, list): | ||||
|             value = self.decompress(value) | ||||
|  | ||||
|         output = [] | ||||
|         final_attrs = self.build_attrs(attrs) | ||||
|         id_ = final_attrs.get('id') | ||||
|  | ||||
|         for i, widget in enumerate(self.widgets): | ||||
|             try: | ||||
|                 widget_value = value[i] | ||||
|             except IndexError: | ||||
|                 widget_value = None | ||||
|             if id_: | ||||
|                 final_attrs = dict(final_attrs, id='%s_%s' % (id_, i)) | ||||
|  | ||||
|             widget_attrs = self.build_widget_attrs(widget, widget_value, final_attrs) | ||||
|             output.append(widget.render(name + '_%s' % i, widget_value, widget_attrs)) | ||||
|  | ||||
|         context = { | ||||
|             'id': final_attrs.get('id'), | ||||
|             'name': name, | ||||
|             'widgets': zip([code for code, lang in settings.LANGUAGES], output), | ||||
|             'available_languages': settings.LANGUAGES | ||||
|         } | ||||
|  | ||||
|         return render_to_string(self.template, context) | ||||
|  | ||||
|     @staticmethod | ||||
|     def build_widget_attrs(widget, value, attrs): | ||||
|         attrs = dict(attrs)  # Copy attrs to avoid modifying the argument. | ||||
|  | ||||
|         if (not widget.use_required_attribute(value) or not widget.is_required) \ | ||||
|                 and 'required' in attrs: | ||||
|             del attrs['required'] | ||||
|  | ||||
|         return attrs | ||||
|  | ||||
|  | ||||
| class AdminLocalizedCharFieldWidget(AdminLocalizedFieldWidget): | ||||
|   | ||||
		Reference in New Issue
	
	Block a user