From 8dc792b8511ddbed12097e11a7899b06e62661a2 Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Thu, 13 May 2021 12:37:17 +0200 Subject: [PATCH] Fix render() of PaddingCheckboxSelectMultiple widget --- orchestra/contrib/bills/admin.py | 4 ++-- orchestra/contrib/contacts/admin.py | 16 ++++++++-------- orchestra/contrib/orchestration/forms.py | 4 ++-- orchestra/forms/widgets.py | 14 +++++++------- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/orchestra/contrib/bills/admin.py b/orchestra/contrib/bills/admin.py index c7c4fc43..7ef0c78e 100644 --- a/orchestra/contrib/bills/admin.py +++ b/orchestra/contrib/bills/admin.py @@ -16,7 +16,7 @@ from orchestra.admin import ExtendedModelAdmin from orchestra.admin.utils import admin_date, insertattr, admin_link, change_url from orchestra.contrib.accounts.actions import list_accounts from orchestra.contrib.accounts.admin import AccountAdminMixin, AccountAdmin -from orchestra.forms.widgets import paddingCheckboxSelectMultiple +from orchestra.forms.widgets import PaddingCheckboxSelectMultiple from . import settings, actions from .filters import (BillTypeListFilter, HasBillContactListFilter, TotalListFilter, @@ -483,7 +483,7 @@ class BillContactInline(admin.StackedInline): if db_field.name == 'address': kwargs['widget'] = forms.Textarea(attrs={'cols': 70, 'rows': 2}) if db_field.name == 'email_usage': - kwargs['widget'] = paddingCheckboxSelectMultiple(45) + kwargs['widget'] = PaddingCheckboxSelectMultiple(45) return super().formfield_for_dbfield(db_field, **kwargs) diff --git a/orchestra/contrib/contacts/admin.py b/orchestra/contrib/contacts/admin.py index f761fc28..82adc381 100644 --- a/orchestra/contrib/contacts/admin.py +++ b/orchestra/contrib/contacts/admin.py @@ -7,7 +7,7 @@ from orchestra.admin.actions import SendEmail from orchestra.admin.utils import insertattr, change_url from orchestra.contrib.accounts.actions import list_accounts from orchestra.contrib.accounts.admin import AccountAdmin, AccountAdminMixin -from orchestra.forms.widgets import paddingCheckboxSelectMultiple +from orchestra.forms.widgets import PaddingCheckboxSelectMultiple from .filters import EmailUsageListFilter from .models import Contact @@ -61,18 +61,18 @@ class ContactAdmin(AccountAdminMixin, ExtendedModelAdmin): }), ) actions = (SendEmail(), list_accounts) - + def dispaly_name(self, contact): return str(contact) dispaly_name.short_description = _("Name") dispaly_name.admin_order_field = 'short_name' - + def formfield_for_dbfield(self, db_field, **kwargs): """ Make value input widget bigger """ if db_field.name == 'address': kwargs['widget'] = forms.Textarea(attrs={'cols': 70, 'rows': 2}) if db_field.name == 'email_usage': - kwargs['widget'] = paddingCheckboxSelectMultiple(130) + kwargs['widget'] = PaddingCheckboxSelectMultiple(130) return super(ContactAdmin, self).formfield_for_dbfield(db_field, **kwargs) @@ -86,14 +86,14 @@ class ContactInline(admin.StackedInline): fields = ( ('short_name', 'full_name'), 'email', 'email_usage', ('phone', 'phone2'), ) - + def get_extra(self, request, obj=None, **kwargs): return 0 if obj and obj.contacts.exists() else 1 - + def get_view_on_site_url(self, obj=None): if obj: return change_url(obj) - + def formfield_for_dbfield(self, db_field, **kwargs): """ Make value input widget bigger """ if db_field.name == 'short_name': @@ -101,7 +101,7 @@ class ContactInline(admin.StackedInline): if db_field.name == 'address': kwargs['widget'] = forms.Textarea(attrs={'cols': 70, 'rows': 2}) if db_field.name == 'email_usage': - kwargs['widget'] = paddingCheckboxSelectMultiple(45) + kwargs['widget'] = PaddingCheckboxSelectMultiple(45) return super(ContactInline, self).formfield_for_dbfield(db_field, **kwargs) diff --git a/orchestra/contrib/orchestration/forms.py b/orchestra/contrib/orchestration/forms.py index bd3f96ad..7ea33538 100644 --- a/orchestra/contrib/orchestration/forms.py +++ b/orchestra/contrib/orchestration/forms.py @@ -1,6 +1,6 @@ from django import forms -from orchestra.forms.widgets import SpanWidget, paddingCheckboxSelectMultiple +from orchestra.forms.widgets import SpanWidget, PaddingCheckboxSelectMultiple class RouteForm(forms.ModelForm): @@ -16,5 +16,5 @@ class RouteForm(forms.ModelForm): else: self.fields['backend'].widget = SpanWidget() actions = backend_class.actions - self.fields['async_actions'].widget = paddingCheckboxSelectMultiple(45) + self.fields['async_actions'].widget = PaddingCheckboxSelectMultiple(45) self.fields['async_actions'].choices = ((action, action) for action in actions) diff --git a/orchestra/forms/widgets.py b/orchestra/forms/widgets.py index 0235db61..74a0fd29 100644 --- a/orchestra/forms/widgets.py +++ b/orchestra/forms/widgets.py @@ -37,17 +37,17 @@ class SpanWidget(forms.Widget): return False -def paddingCheckboxSelectMultiple(padding): +class PaddingCheckboxSelectMultiple(forms.CheckboxSelectMultiple): """ Ugly hack to render this widget nicely on Django admin """ - widget = forms.CheckboxSelectMultiple() - old_render = widget.render + def __init__(self, padding, attrs=None, choices=()): + super().__init__(attrs=attrs, choices=choices) + self.padding = padding + def render(self, *args, **kwargs): - value = old_render(self, *args, **kwargs) + value = super().render(*args, **kwargs) value = re.sub(r'^