diff --git a/users/forms.py b/users/forms.py index 91b8d6dc..fb29e73f 100644 --- a/users/forms.py +++ b/users/forms.py @@ -597,7 +597,7 @@ class MailAliasForm(FormRevMixin, ModelForm): def __init__(self, *args, **kwargs): 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['valeur'].label = "Prefixe de l'alias mail. Ne peut contenir de @" class Meta: model = MailAlias diff --git a/users/migrations/0074_auto_20180629_1717.py b/users/migrations/0074_auto_20180629_1717.py new file mode 100644 index 00000000..446a2cdd --- /dev/null +++ b/users/migrations/0074_auto_20180629_1717.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2018-06-29 15:17 +from __future__ import unicode_literals + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0073_auto_20180629_1614'), + ] + + operations = [ + migrations.AlterField( + model_name='mailalias', + name='user', + field=models.ForeignKey(help_text='Utilisateur associé', on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), + ), + migrations.AlterField( + model_name='mailalias', + name='valeur', + field=models.CharField(help_text="Valeur de l'alias mail", max_length=64, unique=True), + ), + ] diff --git a/users/models.py b/users/models.py index 018e1994..ab0d623b 100644 --- a/users/models.py +++ b/users/models.py @@ -53,6 +53,7 @@ from django.db import models from django.db.models import Q from django import forms from django.db.models.signals import post_save, post_delete, m2m_changed +from django.forms import ValidationError from django.dispatch import receiver from django.utils.functional import cached_property from django.template import Context, loader @@ -97,7 +98,7 @@ def linux_user_validator(login): """ Retourne une erreur de validation si le login ne respecte pas les contraintes unix (maj, min, chiffres ou tiret)""" if not linux_user_check(login): - raise forms.ValidationError( + raise ValidationError( ", ce pseudo ('%(label)s') contient des carractères interdits", params={'label': login}, ) @@ -1646,12 +1647,12 @@ class MailAlias(RevMixin, AclMixin, models.Model): mail = models.ForeignKey( 'Mail', on_delete=models.CASCADE, - help_text="Objects Mail associé" + help_text="Compte mail", ) valeur = models.CharField( unique=True, max_length=64, - help_text="username de l'adresse mail" + help_text="Valeur de l'alias mail" ) @@ -1663,7 +1664,7 @@ class MailAlias(RevMixin, AclMixin, models.Model): Check if the user can view the aliases """ - if user_request.has_perm('users.view_mailalias') or user.request == self.mail.user: + if user_request.has_perm('users.view_mailalias') or user.request == self.user: return True, None else: return False, "Vous n'avais pas les droits suffisants et n'êtes pas propriétaire de ces alias" @@ -1676,8 +1677,8 @@ class MailAlias(RevMixin, AclMixin, models.Model): if user_request.has_perm('users.delete_mailalias'): return True, None else: - if user_request == self.mail.user: - if self.valeur == self.mail.user.pseudo: + if user_request == self.user: + if self.valeur != self.user.pseudo: return True, None else: return False, "Vous ne pouvez pas supprimer l'alias lié à votre pseudo" @@ -1692,13 +1693,16 @@ class MailAlias(RevMixin, AclMixin, models.Model): if user_request.has_perm('users.change_mailalias'): return True, None else: - if user_request == self.mail.user: - if self.valeur == self.mail.user.pseudo: + if user_request == self.user: + if self.valeur != self.user.pseudo: return True, None else: return False, "Vous ne pouvez pas modifier l'alias lié à votre pseudo" else: return False, "Vous n'avez pas les droits suffisants et n'êtes pas propriétairs de cet alias" - + def clean(self, *args, **kwargs): + if "@" in self.valeur: + raise ValidationError("Cet alias ne peut contenir un @") + super(MailAlias, self).clean(*args, **kwargs) diff --git a/users/urls.py b/users/urls.py index 5aaf893e..9289d6c2 100644 --- a/users/urls.py +++ b/users/urls.py @@ -67,7 +67,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'^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 a6a79093..acc3c1d7 100644 --- a/users/views.py +++ b/users/views.py @@ -513,7 +513,6 @@ def add_mailalias(request, user, userid): 'users:profil', kwargs={'userid': str(userid)} )) - return form( {'userform': mailalias, 'action_name': 'Ajouter un alias mail'}, 'users/user.html', @@ -532,7 +531,10 @@ def edit_mailalias(request, mailalias_instance, **_kwargs): if mailalias.changed_data: mailalias.save() messages.success(request, "Alias modifiée") - return redirect(reverse('users:index')) + return redirect(reverse( + 'users:profil', + kwargs={'userid': str(mailalias_instance.user.id)} + )) return form( {'userform': mailalias, 'action_name': 'Editer un alias mail'}, 'users/user.html', @@ -548,7 +550,7 @@ def del_mailalias(request, mailalias, **_kwargs): messages.success(request, "L'alias a été supprimé") return redirect(reverse( 'users:profil', - kwargs={'userid': str(mailalias.mail.user.id)} + kwargs={'userid': str(mailalias.user.id)} )) return form( {'objet': mailalias, 'objet_name': 'mailalias'},