8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-27 15:12:25 +00:00

Les champs mails sont dans users

This commit is contained in:
chirac 2018-06-29 16:36:04 +02:00 committed by Maël Kervella
parent 04de9db6d4
commit da87579f81
7 changed files with 75 additions and 129 deletions

View file

@ -34,7 +34,6 @@ from reversion.admin import VersionAdmin
from .models import ( from .models import (
User, User,
Mail,
MailAlias, MailAlias,
ServiceUser, ServiceUser,
School, School,
@ -132,6 +131,7 @@ class UserAdmin(VersionAdmin, BaseUserAdmin):
list_display = ( list_display = (
'pseudo', 'pseudo',
'surname', 'surname',
'external_mail',
'school', 'school',
'is_admin', 'is_admin',
'shell' 'shell'
@ -145,7 +145,7 @@ class UserAdmin(VersionAdmin, BaseUserAdmin):
'Personal info', 'Personal info',
{ {
'fields': 'fields':
('surname', 'school', 'shell', 'uid_number') ('surname', 'external_mail', 'school', 'shell', 'uid_number')
} }
), ),
('Permissions', {'fields': ('is_admin', )}), ('Permissions', {'fields': ('is_admin', )}),
@ -160,6 +160,7 @@ class UserAdmin(VersionAdmin, BaseUserAdmin):
'fields': ( 'fields': (
'pseudo', 'pseudo',
'surname', 'surname',
'external_mail',
'school', 'school',
'is_admin', 'is_admin',
'password1', 'password1',

View file

@ -53,7 +53,6 @@ from .models import (
School, School,
ListRight, ListRight,
Whitelist, Whitelist,
Mail,
MailAlias, MailAlias,
ListShell, ListShell,
Ban, Ban,
@ -307,7 +306,6 @@ class AdherentForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
self.fields['room'].label = 'Chambre' self.fields['room'].label = 'Chambre'
self.fields['room'].empty_label = "Pas de chambre" self.fields['room'].empty_label = "Pas de chambre"
self.fields['school'].empty_label = "Séléctionner un établissement" self.fields['school'].empty_label = "Séléctionner un établissement"
class Meta: class Meta:
model = Adherent model = Adherent
fields = [ fields = [
@ -601,7 +599,7 @@ class MailAliasForm(FormRevMixin, ModelForm):
class Meta: class Meta:
model = MailAlias model = MailAlias
exclude = ['mail'] exclude = ['user']
class MailForm(FormRevMixin, FieldPermissionFormMixin, ModelForm): class MailForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
"""Creation, edition des paramètres mail""" """Creation, edition des paramètres mail"""
@ -615,5 +613,5 @@ class MailForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
self.fields['internal_address'].label = 'Adresse mail interne' self.fields['internal_address'].label = 'Adresse mail interne'
class Meta: class Meta:
model = Mail model = User
exclude = ['user'] fields = ['external_mail', 'redirection', 'internal_address']

View file

@ -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),
),
]

View file

@ -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),
),
]

View file

@ -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)
]

View file

@ -135,6 +135,7 @@ class UserManager(BaseUserManager):
self, self,
pseudo, pseudo,
surname, surname,
external_mail,
password=None, password=None,
su=False su=False
): ):
@ -148,6 +149,7 @@ class UserManager(BaseUserManager):
pseudo=pseudo, pseudo=pseudo,
surname=surname, surname=surname,
name=surname, name=surname,
external_mail=external_mail,
) )
user.set_password(password) user.set_password(password)
@ -156,19 +158,19 @@ class UserManager(BaseUserManager):
user.save(using=self._db) user.save(using=self._db)
return user 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, Creates and saves a User with the given pseudo, name, surname, email,
and password. 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, Creates and saves a superuser with the given pseudo, name, surname,
email, and password. 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, class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser,
@ -187,13 +189,21 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser,
) )
surname = models.CharField(max_length=255) surname = models.CharField(max_length=255)
email = models.EmailField()
pseudo = models.CharField( pseudo = models.CharField(
max_length=32, max_length=32,
unique=True, unique=True,
help_text="Doit contenir uniquement des lettres, chiffres, ou tirets", help_text="Doit contenir uniquement des lettres, chiffres, ou tirets",
validators=[linux_user_validator] 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 = models.ForeignKey(
'School', 'School',
on_delete=models.PROTECT, on_delete=models.PROTECT,
@ -226,7 +236,7 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser,
) )
USERNAME_FIELD = 'pseudo' USERNAME_FIELD = 'pseudo'
REQUIRED_FIELDS = ['surname'] REQUIRED_FIELDS = ['surname', 'external_mail']
objects = UserManager() objects = UserManager()
@ -526,7 +536,7 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser,
user_ldap.sn = self.pseudo user_ldap.sn = self.pseudo
user_ldap.dialupAccess = str(self.has_access()) user_ldap.dialupAccess = str(self.has_access())
user_ldap.home_directory = '/home/' + self.pseudo 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() + '_'\ user_ldap.given_name = self.surname.lower() + '_'\
+ self.name.lower()[:3] + self.name.lower()[:3]
user_ldap.gid = LDAP['user_gid'] user_ldap.gid = LDAP['user_gid']
@ -1646,49 +1656,16 @@ class LdapServiceUserGroup(ldapdb.models.Model):
return self.name 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): class MailAlias(RevMixin, AclMixin, models.Model):
""" """
Define a alias for a user Mail Define a alias for a user Mail
Définit un alias pour un Mail d'utilisateur Définit un alias pour un Mail d'utilisateur
""" """
mail = models.ForeignKey( user = models.ForeignKey(
'Mail', User,
on_delete=models.CASCADE, on_delete=models.CASCADE,
help_text="Compte mail", help_text="Utilisateur associé",
) )
valeur = models.CharField( valeur = models.CharField(
unique=True, unique=True,
@ -1696,7 +1673,6 @@ class MailAlias(RevMixin, AclMixin, models.Model):
help_text="Valeur de l'alias mail" help_text="Valeur de l'alias mail"
) )
def __str__(self): def __str__(self):
return self.valeur + OptionalUser.get_cached_value('mail_extension') return self.valeur + OptionalUser.get_cached_value('mail_extension')

View file

@ -82,7 +82,6 @@ from .models import (
Club, Club,
ListShell, ListShell,
MailAlias, MailAlias,
Mail,
) )
from .forms import ( from .forms import (
BanForm, BanForm,
@ -501,7 +500,7 @@ def del_whitelist(request, whitelist, **_kwargs):
@can_edit(User) @can_edit(User)
def add_mailalias(request, user, userid): def add_mailalias(request, user, userid):
""" Créer un alias """ """ Créer un alias """
mailalias_instance = MailAlias(mail=user.mail) mailalias_instance = MailAlias(user=user)
mailalias = MailAliasForm( mailalias = MailAliasForm(
request.POST or None, request.POST or None,
instance=mailalias_instance instance=mailalias_instance
@ -559,8 +558,8 @@ def del_mailalias(request, mailalias, **_kwargs):
) )
@login_required @login_required
@can_edit(Mail) @can_edit(User)
def edit_mail(request, mail_instance, **_kwargs): def edit_mail(request, user_instance, **_kwargs):
""" Editer un compte mail""" """ Editer un compte mail"""
mail = MailForm( mail = MailForm(
request.POST or None, request.POST or None,