authentic2: remove role form (#69896)
This commit is contained in:
parent
cf7c18f812
commit
fbafdfbfcd
|
@ -385,9 +385,6 @@ MIDDLEWARE = MIDDLEWARE + ('hobo.middleware.PrometheusStatsMiddleware',)
|
|||
HOBO_MANAGER_HOMEPAGE_URL_VAR = 'portal_agent_url'
|
||||
HOBO_MANAGER_HOMEPAGE_TITLE_VAR = 'portal_agent_title'
|
||||
|
||||
# Authentic2 role attributes for Publik
|
||||
A2_MANAGER_ROLE_FORM_CLASS = 'hobo.agent.authentic2.role_forms.RoleForm'
|
||||
|
||||
# Locale and timezone
|
||||
LANGUAGE_CODE = 'fr-fr'
|
||||
TIME_ZONE = 'Europe/Paris'
|
||||
|
|
|
@ -1,117 +0,0 @@
|
|||
# hobo - portal to configure and deploy applications
|
||||
# Copyright (C) 2015 Entr'ouvert
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify it
|
||||
# under the terms of the GNU Affero General Public License as published
|
||||
# by the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import json
|
||||
|
||||
from authentic2.a2_rbac.models import Role, RoleAttribute
|
||||
from authentic2.manager.forms import RoleEditForm
|
||||
from authentic2.validators import EmailValidator
|
||||
from django import forms
|
||||
from django.core import validators
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
|
||||
class ListValidator:
|
||||
def __init__(self, item_validator):
|
||||
self.item_validator = item_validator
|
||||
|
||||
def __call__(self, value):
|
||||
for i, item in enumerate(value):
|
||||
try:
|
||||
self.item_validator(item)
|
||||
except ValidationError as e:
|
||||
raise ValidationError(_('Item {0} is invalid: {1}').format(i, e.args[0]))
|
||||
|
||||
|
||||
class CommaSeparatedInput(forms.TextInput):
|
||||
def format_value(self, value):
|
||||
if not value:
|
||||
return ''
|
||||
if not isinstance(value, str):
|
||||
return ', '.join(value)
|
||||
return value
|
||||
|
||||
|
||||
class CommaSeparatedCharField(forms.Field):
|
||||
widget = CommaSeparatedInput
|
||||
|
||||
def __init__(self, dedup=True, max_length=None, min_length=None, *args, **kwargs):
|
||||
self.dedup = dedup
|
||||
self.max_length = max_length
|
||||
self.min_length = min_length
|
||||
item_validators = kwargs.pop('item_validators', [])
|
||||
super().__init__(*args, **kwargs)
|
||||
for item_validator in item_validators:
|
||||
self.validators.append(ListValidator(item_validator))
|
||||
|
||||
def to_python(self, value):
|
||||
if value in validators.EMPTY_VALUES:
|
||||
return []
|
||||
|
||||
value = [item.strip() for item in value.split(',') if item.strip()]
|
||||
if self.dedup:
|
||||
value = list(set(value))
|
||||
|
||||
return value
|
||||
|
||||
def clean(self, value):
|
||||
value = self.to_python(value)
|
||||
self.validate(value)
|
||||
self.run_validators(value)
|
||||
return value
|
||||
|
||||
|
||||
class RoleForm(RoleEditForm):
|
||||
details = forms.CharField(
|
||||
label=_('Role details (frontoffice)'), widget=forms.Textarea, initial='', required=False
|
||||
)
|
||||
emails = CommaSeparatedCharField(
|
||||
label=_('Emails'),
|
||||
item_validators=[EmailValidator()],
|
||||
required=False,
|
||||
help_text=_('Emails must be separated by commas.'),
|
||||
)
|
||||
emails_to_members = forms.BooleanField(required=False, initial=True, label=_('Emails to members'))
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
instance = kwargs.get('instance')
|
||||
if instance:
|
||||
fields = [x.name for x in Role._meta.get_fields()]
|
||||
initial = kwargs.setdefault('initial', {})
|
||||
role_attributes = RoleAttribute.objects.filter(role=instance, kind='json')
|
||||
for role_attribute in role_attributes:
|
||||
if role_attribute.name in fields:
|
||||
continue
|
||||
initial[role_attribute.name] = json.loads(role_attribute.value)
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def save(self, commit=True):
|
||||
fields = [x.name for x in Role._meta.get_fields()]
|
||||
assert commit
|
||||
instance = super().save(commit=commit)
|
||||
for field in self.cleaned_data:
|
||||
if field in fields:
|
||||
continue
|
||||
value = json.dumps(self.cleaned_data[field])
|
||||
ra, created = RoleAttribute.objects.get_or_create(
|
||||
role=instance, name=field, kind='json', defaults={'value': value}
|
||||
)
|
||||
if not created and ra.value != value:
|
||||
ra.value = value
|
||||
ra.save()
|
||||
instance.save()
|
||||
return instance
|
Loading…
Reference in New Issue