From da87579f81ed40487c1aa39405441fcc8c1c09d0 Mon Sep 17 00:00:00 2001 From: chirac Date: Fri, 29 Jun 2018 16:36:04 +0200 Subject: [PATCH] Les champs mails sont dans users --- users/admin.py | 5 +- users/forms.py | 8 +-- users/migrations/0073_auto_20180629_1614.py | 46 +++++++++++++++ users/migrations/0073_mail_mailalias.py | 38 ------------ users/migrations/0074_transfermail.py | 36 ------------ users/models.py | 64 +++++++-------------- users/views.py | 7 +-- 7 files changed, 75 insertions(+), 129 deletions(-) create mode 100644 users/migrations/0073_auto_20180629_1614.py delete mode 100644 users/migrations/0073_mail_mailalias.py delete mode 100644 users/migrations/0074_transfermail.py diff --git a/users/admin.py b/users/admin.py index ce02aa09..ee71713c 100644 --- a/users/admin.py +++ b/users/admin.py @@ -34,7 +34,6 @@ from reversion.admin import VersionAdmin from .models import ( User, - Mail, MailAlias, ServiceUser, School, @@ -132,6 +131,7 @@ class UserAdmin(VersionAdmin, BaseUserAdmin): list_display = ( 'pseudo', 'surname', + 'external_mail', 'school', 'is_admin', 'shell' @@ -145,7 +145,7 @@ class UserAdmin(VersionAdmin, BaseUserAdmin): 'Personal info', { 'fields': - ('surname', 'school', 'shell', 'uid_number') + ('surname', 'external_mail', 'school', 'shell', 'uid_number') } ), ('Permissions', {'fields': ('is_admin', )}), @@ -160,6 +160,7 @@ class UserAdmin(VersionAdmin, BaseUserAdmin): 'fields': ( 'pseudo', 'surname', + 'external_mail', 'school', 'is_admin', 'password1', diff --git a/users/forms.py b/users/forms.py index ebc4b945..144d5ecc 100644 --- a/users/forms.py +++ b/users/forms.py @@ -53,7 +53,6 @@ from .models import ( School, ListRight, Whitelist, - Mail, MailAlias, ListShell, Ban, @@ -307,7 +306,6 @@ class AdherentForm(FormRevMixin, FieldPermissionFormMixin, ModelForm): self.fields['room'].label = 'Chambre' self.fields['room'].empty_label = "Pas de chambre" self.fields['school'].empty_label = "Séléctionner un établissement" - class Meta: model = Adherent fields = [ @@ -601,7 +599,7 @@ class MailAliasForm(FormRevMixin, ModelForm): class Meta: model = MailAlias - exclude = ['mail'] + exclude = ['user'] class MailForm(FormRevMixin, FieldPermissionFormMixin, ModelForm): """Creation, edition des paramètres mail""" @@ -615,5 +613,5 @@ class MailForm(FormRevMixin, FieldPermissionFormMixin, ModelForm): self.fields['internal_address'].label = 'Adresse mail interne' class Meta: - model = Mail - exclude = ['user'] + model = User + fields = ['external_mail', 'redirection', 'internal_address'] diff --git a/users/migrations/0073_auto_20180629_1614.py b/users/migrations/0073_auto_20180629_1614.py new file mode 100644 index 00000000..681cb722 --- /dev/null +++ b/users/migrations/0073_auto_20180629_1614.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2018-06-29 14:14 +from __future__ import unicode_literals + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion +import re2o.mixins + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0072_auto_20180426_2021'), + ] + + operations = [ + migrations.CreateModel( + name='MailAlias', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('valeur', models.CharField(help_text="username de l'adresse mail", max_length=64, unique=True)), + ], + bases=(re2o.mixins.RevMixin, re2o.mixins.AclMixin, models.Model), + ), + migrations.RenameField( + model_name='user', + old_name='email', + new_name='external_mail', + ), + migrations.AddField( + model_name='user', + name='internal_address', + field=models.BooleanField(default=False, help_text="Activer ou non l'utilisation de l'adresse mail interne"), + ), + migrations.AddField( + model_name='user', + name='redirection', + field=models.BooleanField(default=False, help_text='Activer ou non la redirection du mail interne vers le mail externe'), + ), + migrations.AddField( + model_name='mailalias', + name='user', + field=models.ForeignKey(blank=True, help_text='Utilisateur associé', null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/users/migrations/0073_mail_mailalias.py b/users/migrations/0073_mail_mailalias.py deleted file mode 100644 index ce676464..00000000 --- a/users/migrations/0073_mail_mailalias.py +++ /dev/null @@ -1,38 +0,0 @@ -# -*- coding: utf-8 -*- -# Generated by Django 1.10.7 on 2018-06-26 19:07 -from __future__ import unicode_literals - -from django.conf import settings -from django.db import migrations, models -import django.db.models.deletion -import re2o.mixins - - -class Migration(migrations.Migration): - - dependencies = [ - ('users', '0072_auto_20180426_2021'), - ] - - operations = [ - migrations.CreateModel( - name='Mail', - fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('external_mail', models.EmailField(help_text='Mail externe', max_length=254)), - ('redirection', models.BooleanField(default=False)), - ('internal_address', models.BooleanField(default=False)), - ('user', models.OneToOneField(help_text="Object mail d'un User", on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), - ], - bases=(re2o.mixins.RevMixin, re2o.mixins.AclMixin, models.Model), - ), - migrations.CreateModel( - name='MailAlias', - fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('valeur', models.CharField(help_text="username de l'adresse mail", max_length=64, unique=True)), - ('mail', models.ForeignKey(help_text='Objects Mail associé', on_delete=django.db.models.deletion.CASCADE, to='users.Mail')), - ], - bases=(re2o.mixins.RevMixin, re2o.mixins.AclMixin, models.Model), - ), - ] diff --git a/users/migrations/0074_transfermail.py b/users/migrations/0074_transfermail.py deleted file mode 100644 index dbf6adca..00000000 --- a/users/migrations/0074_transfermail.py +++ /dev/null @@ -1,36 +0,0 @@ -# -*- coding: utf-8 -*- -# Generated by Django 1.10.7 on 2018-06-26 18:34 -from __future__ import unicode_literals - -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - dependencies = [ - ('users', '0073_mail_mailalias'), - ] - - def create_mail(apps, schema_editor): - db_alias = schema_editor.connection.alias - user = apps.get_model('users','User') - mail = apps.get_model('users','Mail') - mailalias = apps.get_model('users','MailAlias') - users = user.objects.using(db_alias).all() - for us in users: - ma = mail() - ma.user=us - ma.external_mail = us.email - ma.save() - al = mailalias() - al.mail = ma - al.valeur = us.pseudo - al.save() - - def uncreatemail(apps,schema_editor): - pass - - operations = [ - migrations.RunPython(create_mail, uncreatemail) - ] diff --git a/users/models.py b/users/models.py index d6607623..4f0b2794 100755 --- a/users/models.py +++ b/users/models.py @@ -135,6 +135,7 @@ class UserManager(BaseUserManager): self, pseudo, surname, + external_mail, password=None, su=False ): @@ -148,6 +149,7 @@ class UserManager(BaseUserManager): pseudo=pseudo, surname=surname, name=surname, + external_mail=external_mail, ) user.set_password(password) @@ -156,19 +158,19 @@ class UserManager(BaseUserManager): user.save(using=self._db) return user - def create_user(self, pseudo, surname, password=None): + def create_user(self, pseudo, surname, external_mail, password=None): """ Creates and saves a User with the given pseudo, name, surname, email, and password. """ - return self._create_user(pseudo, surname, password, False) + return self._create_user(pseudo, surname, external_mail, password, False) - def create_superuser(self, pseudo, surname, password): + def create_superuser(self, pseudo, surname, external_mail, password): """ Creates and saves a superuser with the given pseudo, name, surname, email, and password. """ - return self._create_user(pseudo, surname, password, True) + return self._create_user(pseudo, surname, external_mail, password, True) class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser, @@ -187,13 +189,21 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser, ) surname = models.CharField(max_length=255) - email = models.EmailField() pseudo = models.CharField( max_length=32, unique=True, help_text="Doit contenir uniquement des lettres, chiffres, ou tirets", validators=[linux_user_validator] ) + external_mail = models.EmailField() + redirection = models.BooleanField( + default=False, + help_text='Activer ou non la redirection du mail interne vers le mail externe' + ) + internal_address = models.BooleanField( + default=False, + help_text='Activer ou non l\'utilisation de l\'adresse mail interne' + ) school = models.ForeignKey( 'School', on_delete=models.PROTECT, @@ -226,7 +236,7 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser, ) USERNAME_FIELD = 'pseudo' - REQUIRED_FIELDS = ['surname'] + REQUIRED_FIELDS = ['surname', 'external_mail'] objects = UserManager() @@ -526,7 +536,7 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser, user_ldap.sn = self.pseudo user_ldap.dialupAccess = str(self.has_access()) user_ldap.home_directory = '/home/' + self.pseudo - user_ldap.mail = self.email + user_ldap.mail = self.get_mail() user_ldap.given_name = self.surname.lower() + '_'\ + self.name.lower()[:3] user_ldap.gid = LDAP['user_gid'] @@ -1646,49 +1656,16 @@ class LdapServiceUserGroup(ldapdb.models.Model): return self.name -class Mail(RevMixin, AclMixin, models.Model): - """ - Mail account of a user - - Compte mail d'un utilisateur - """ - external_mail = models.EmailField(help_text="Mail externe") - user = models.OneToOneField( - 'User', - on_delete=models.CASCADE, - help_text="Object mail d'un User" - ) - redirection = models.BooleanField( - default=False - ) - internal_address = models.BooleanField( - default=False - ) - - 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): """ Define a alias for a user Mail Définit un alias pour un Mail d'utilisateur """ - mail = models.ForeignKey( - 'Mail', + user = models.ForeignKey( + User, on_delete=models.CASCADE, - help_text="Compte mail", + help_text="Utilisateur associé", ) valeur = models.CharField( unique=True, @@ -1696,7 +1673,6 @@ class MailAlias(RevMixin, AclMixin, models.Model): help_text="Valeur de l'alias mail" ) - def __str__(self): return self.valeur + OptionalUser.get_cached_value('mail_extension') diff --git a/users/views.py b/users/views.py index ab428853..09590be6 100644 --- a/users/views.py +++ b/users/views.py @@ -82,7 +82,6 @@ from .models import ( Club, ListShell, MailAlias, - Mail, ) from .forms import ( BanForm, @@ -501,7 +500,7 @@ def del_whitelist(request, whitelist, **_kwargs): @can_edit(User) def add_mailalias(request, user, userid): """ Créer un alias """ - mailalias_instance = MailAlias(mail=user.mail) + mailalias_instance = MailAlias(user=user) mailalias = MailAliasForm( request.POST or None, instance=mailalias_instance @@ -559,8 +558,8 @@ def del_mailalias(request, mailalias, **_kwargs): ) @login_required -@can_edit(Mail) -def edit_mail(request, mail_instance, **_kwargs): +@can_edit(User) +def edit_mail(request, user_instance, **_kwargs): """ Editer un compte mail""" mail = MailForm( request.POST or None,