From bf189f4c4fca702975f27744ff8f48ba10d9ee48 Mon Sep 17 00:00:00 2001 From: chirac Date: Fri, 29 Jun 2018 18:48:27 +0200 Subject: [PATCH] Affichage des champs supp de mail que si compte mails actifs --- users/forms.py | 10 ++++++---- users/models.py | 32 ++++++++++++++++++++++++++++++-- users/views.py | 8 ++++++-- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/users/forms.py b/users/forms.py index fb29e73f..ebc4b945 100644 --- a/users/forms.py +++ b/users/forms.py @@ -603,14 +603,16 @@ class MailAliasForm(FormRevMixin, ModelForm): model = MailAlias exclude = ['mail'] -class MailForm(FormRevMixin, ModelForm): - """Creation, edition d'un objet mail""" +class MailForm(FormRevMixin, FieldPermissionFormMixin, ModelForm): + """Creation, edition des paramètres mail""" def __init__(self, *args, **kwargs): prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(MailForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields['external_mail'].label = 'Adresse mail externe' - self.fields['redirection'].label = 'Activation de la redirection vers l\'adress externe' - self.fields['internal_address'].label = 'Adresse mail interne' + if 'redirection' in self.fields: + self.fields['redirection'].label = 'Activation de la redirection vers l\'adress externe' + if 'internal_address' in self.fields: + self.fields['internal_address'].label = 'Adresse mail interne' class Meta: model = Mail diff --git a/users/models.py b/users/models.py index ab0d623b..37204a45 100644 --- a/users/models.py +++ b/users/models.py @@ -679,8 +679,8 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser, """ Return the mail address choosen by the user """ - if not self.mail.internal_address: - return self.mail.external_mail + if not OptionalUser.get_cached_value('mail_accounts') or not self.internal_address: + return self.external_mail else: return self.mail.mailalias_set.get(valeur=self.pseudo) @@ -802,6 +802,32 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser, "Droit requis pour changer le shell" ) + @staticmethod + def can_change_redirection(user_request, *_args, **_kwargs): + """ Check if a user can change redirection. + + :param user_request: The user who request + :returns: a message and a boolean which is True if the user has + the right to change a redirection + """ + return ( + OptionalUser.get_cached_value('mail_accounts'), + "La gestion des comptes mails doit être activée" + ) + + @staticmethod + def can_change_internal_address(user_request, *_args, **_kwargs): + """ Check if a user can change internal address . + + :param user_request: The user who request + :returns: a message and a boolean which is True if the user has + the right to change internal address + """ + return ( + OptionalUser.get_cached_value('mail_accounts'), + "La gestion des comptes mails doit être activée" + ) + @staticmethod def can_change_force(user_request, *_args, **_kwargs): """ Check if a user can change a force @@ -896,6 +922,8 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser, 'shell': self.can_change_shell, 'force': self.can_change_force, 'selfpasswd': self.check_selfpasswd, + 'redirection': self.can_change_redirection, + 'internal_address' : self.can_change_internal_address, } self.__original_state = self.state diff --git a/users/views.py b/users/views.py index acc3c1d7..4ee03458 100644 --- a/users/views.py +++ b/users/views.py @@ -564,13 +564,17 @@ def edit_mail(request, mail_instance, **_kwargs): """ Editer un compte mail""" mail = MailForm( request.POST or None, - instance=mail_instance + instance=user_instance, + user=request.user ) if mail.is_valid(): if mail.changed_data: mail.save() messages.success(request, "Compte mail modifiée") - return redirect(reverse('users:index')) + return redirect(reverse( + 'users:profil', + kwargs={'userid': str(user_instance.id)} + )) return form( {'userform': mail, 'action_name': 'Editer un compte mail'}, 'users/user.html',