From 1f3f557944ebbceb3abb53b80eebdcfe4f1b7e39 Mon Sep 17 00:00:00 2001 From: grisel-davy Date: Wed, 27 Jun 2018 23:13:43 +0200 Subject: [PATCH] Affichage et modification du compte mail et des alias mail --- users/forms.py | 15 ++++++++++- users/models.py | 12 ++++++++- users/templates/users/aff_alias.html | 5 ++-- users/templates/users/profil.html | 40 ++++++++++++++++++++++++++++ users/urls.py | 1 + users/views.py | 39 +++++++++++++++++++++------ 6 files changed, 99 insertions(+), 13 deletions(-) diff --git a/users/forms.py b/users/forms.py index 044c170b..edcaf569 100644 --- a/users/forms.py +++ b/users/forms.py @@ -53,6 +53,7 @@ from .models import ( School, ListRight, Whitelist, + Mail, MailAlias, ListShell, Ban, @@ -596,8 +597,20 @@ class MailAliasForm(FormRevMixin, ModelForm): prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(MailAliasForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields['valeur'].label = 'nom de l\'adresse mail' - self.fields['extension'].label = 'extension de l\'adresse mail' class Meta: model = MailAlias exclude = ['mail'] + +class MailForm(FormRevMixin, ModelForm): + """Creation, edition d'un objet 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' + + class Meta: + model = Mail + exclude = ['user'] diff --git a/users/models.py b/users/models.py index b9a41b81..233db6a4 100644 --- a/users/models.py +++ b/users/models.py @@ -675,7 +675,7 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser, if not self.mail.internal_address: return self.mail.external_mail else: - return self.mail.mailalias_set.get(valeur=pseudo) + return self.mail.mailalias_set.get(valeur=self.pseudo) def get_next_domain_name(self): """Look for an available name for a new interface for @@ -1608,6 +1608,16 @@ class Mail(RevMixin, AclMixin, models.Model): def __str__(self): return self.user.get_mail() + def can_edit(self, user_request, *_args, **_kwargs): + """ + Check if the user can edit the mail + """ + + if user_request.has_perm('users.change_mail') or user_request == self.user: + return True, None + else: + return False, "Vous n'avez pas les droits suffisants et n'êtes pas propriétairs de cet alias" + class MailAlias(RevMixin, AclMixin, models.Model): """ diff --git a/users/templates/users/aff_alias.html b/users/templates/users/aff_alias.html index 59a9b6f1..ec5b7313 100644 --- a/users/templates/users/aff_alias.html +++ b/users/templates/users/aff_alias.html @@ -25,7 +25,6 @@ with this program; if not, write to the Free Software Foundation, Inc., {% if alias_list.paginator %} {% include "pagination.html" with list=alias_list %} {% endif %} - @@ -37,10 +36,10 @@ with this program; if not, write to the Free Software Foundation, Inc., diff --git a/users/templates/users/profil.html b/users/templates/users/profil.html index bc0a5ac4..d402e108 100644 --- a/users/templates/users/profil.html +++ b/users/templates/users/profil.html @@ -427,6 +427,46 @@ with this program; if not, write to the Free Software Foundation, Inc., +
+
+

+ + Gestion des mails +

+
+
+
+ {% can_edit user.mail %} + + + Modifier les options mail + + {% acl_end %} +
+
{{ alias }} {% can_delete alias %} - {% include 'buttons/suppr.html' with href='users:del-alias' id=alias.id %} + {% include 'buttons/suppr.html' with href='users:del-mailalias' id=alias.id %} {% acl_end %} {% can_edit alias %} - {% include 'buttons/edit.html' with href='users:edit-alias' id=alias.id %} + {% include 'buttons/edit.html' with href='users:edit-mailalias' id=alias.id %} {% acl_end %} {% include 'buttons/history.html' with href='users:history' name='alias' id=alias.id %}
+ + + + + + + + + +
Adresse mail externeCompte mail {{ asso_name }}Adresse mail de contact
{{ user.mail.external_mail }}{{ user.mail.internal_address|yesno:"Activé,Désactivé" }}{{ user.mail.external_mail }}
+ + {% can_create MailAlias %} + + + Ajouter un alias mail + + {% acl_end %} + {% if alias_list %} + {% include "users/aff_alias.html" with alias_list=alias_list %} + {% endif %} + + +

diff --git a/users/urls.py b/users/urls.py index f27a15c3..d77d015b 100644 --- a/users/urls.py +++ b/users/urls.py @@ -68,6 +68,7 @@ urlpatterns = [ url(r'^add_mailalias/(?P[0-9]+)$', views.add_mailalias, name='add-mailalias'), url(r'^edit_mailalias/(?P[0-9]+)$', views.edit_mailalias, name='edit-mailalias'), url(r'^del-mailalias/(?P[0-9]+)$', views.del_mailalias, name='del-mailalias'), + url(r'^edit_mail/(?P[0-9]+)$', views.edit_mail, name='edit-mail'), url(r'^add_school/$', views.add_school, name='add-school'), url(r'^edit_school/(?P[0-9]+)$', views.edit_school, diff --git a/users/views.py b/users/views.py index 1cf4a4de..1f708bca 100644 --- a/users/views.py +++ b/users/views.py @@ -86,6 +86,8 @@ from .models import ( from .forms import ( BanForm, WhitelistForm, + MailAliasForm, + MailForm, DelSchoolForm, DelListRightForm, NewListRightForm, @@ -503,12 +505,12 @@ def del_whitelist(request, whitelist, **_kwargs): def add_mailalias(request, user, userid): """ Créer un alias """ mailalias_instance = MailAlias(mail=user.mail) - whitelist = WhitelistForm( + mailalias = MailAliasForm( request.POST or None, - instance=whitelist_instance + instance=mailalias_instance ) - if whitelist.is_valid(): - whitelist.save() + if mailalias.is_valid(): + mailalias.save() messages.success(request, "Alias créé") return redirect(reverse( 'users:profil', @@ -529,9 +531,9 @@ def edit_mailalias(request, mailalias_instance, **_kwargs): request.POST or None, instance=mailalias_instance ) - if whitelist.is_valid(): - if whitelist.changed_data: - whitelist.save() + if mailalias.is_valid(): + if mailalias.changed_data: + mailalias.save() messages.success(request, "Alias modifiée") return redirect(reverse('users:index')) return form( @@ -557,6 +559,25 @@ def del_mailalias(request, mailalias, **_kwargs): request ) +@login_required +@can_edit(Mail) +def edit_mail(request, mail_instance, **_kwargs): + """ Editer un compte mail""" + mail = MailForm( + request.POST or None, + instance=mail_instance + ) + if mail.is_valid(): + if mail.changed_data: + mail.save() + messages.success(request, "Compte mail modifiée") + return redirect(reverse('users:index')) + return form( + {'userform': mail, 'action_name': 'Editer un compte mail'}, + 'users/user.html', + request + ) + @login_required @can_create(School) def add_school(request): @@ -957,7 +978,9 @@ def profil(request, users, **_kwargs): 'white_list': whitelists, 'user_solde': user_solde, 'allow_online_payment': allow_online_payment, - 'solde_activated': OptionalUser.objects.first().user_solde + 'solde_activated': OptionalUser.objects.first().user_solde, + 'asso_name': AssoOption.objects.first().name, + 'alias_list': users.mail.mailalias_set.all() } )