From 236bd4f25540b73225575914ef3666bc834e9ce3 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Tue, 20 Sep 2022 11:51:05 +0200 Subject: [PATCH 02/10] auth_saml: move model form parameters to class (#53442) --- src/authentic2_auth_saml/forms.py | 8 ++++++++ src/authentic2_auth_saml/views.py | 10 ++-------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/authentic2_auth_saml/forms.py b/src/authentic2_auth_saml/forms.py index c0af5890a..c4e15415c 100644 --- a/src/authentic2_auth_saml/forms.py +++ b/src/authentic2_auth_saml/forms.py @@ -17,6 +17,7 @@ from django import forms from authentic2.a2_rbac.models import Role +from authentic2.forms.widgets import SelectAttributeWidget from authentic2.manager.utils import label_from_role from .models import SAMLAuthenticator @@ -65,3 +66,10 @@ class RoleChoiceField(forms.ModelChoiceField): def label_from_instance(self, obj): return label_from_role(obj) + + +class SAMLRelatedObjectForm(forms.ModelForm): + class Meta: + exclude = ('authenticator',) + field_classes = {'role': RoleChoiceField} + widgets = {'user_field': SelectAttributeWidget} diff --git a/src/authentic2_auth_saml/views.py b/src/authentic2_auth_saml/views.py index 95c390f16..ba8958a94 100644 --- a/src/authentic2_auth_saml/views.py +++ b/src/authentic2_auth_saml/views.py @@ -7,11 +7,10 @@ from django.urls import reverse from django.views.generic import CreateView, DeleteView, UpdateView from mellon.utils import get_idp -from authentic2.forms.widgets import SelectAttributeWidget from authentic2.manager.views import MediaMixin, TitleMixin from authentic2.utils.misc import redirect_to_login -from .forms import RoleChoiceField +from .forms import SAMLRelatedObjectForm from .models import AddRoleAction, SAMLAttributeLookup, SAMLAuthenticator, SetAttributeAction @@ -60,12 +59,7 @@ class SAMLAuthenticatorMixin(MediaMixin, TitleMixin): return super().dispatch(request, *args, **kwargs) def get_form_class(self): - return modelform_factory( - self.model, - exclude=('authenticator',), - field_classes={'role': RoleChoiceField}, - widgets={'user_field': SelectAttributeWidget}, - ) + return modelform_factory(self.model, SAMLRelatedObjectForm) def get_form_kwargs(self): kwargs = super().get_form_kwargs() -- 2.30.2