8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-22 19:33:11 +00:00

Les champs mails sont dans users

This commit is contained in:
chirac 2018-06-29 16:36:04 +02:00
parent 43e2ef6b4d
commit 8e5d897615
9 changed files with 88 additions and 141 deletions

View file

@ -135,7 +135,6 @@ MODEL_NAME = {
'SwitchBay': topologie.models.SwitchBay, 'SwitchBay': topologie.models.SwitchBay,
# users # users
'User': users.models.User, 'User': users.models.User,
'Mail': users.models.Mail,
'MailAlias': users.models.MailAlias, 'MailAlias': users.models.MailAlias,
'Adherent': users.models.Adherent, 'Adherent': users.models.Adherent,
'Club': users.models.Club, 'Club': users.models.Club,

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,
@ -127,6 +126,7 @@ class UserAdmin(VersionAdmin, BaseUserAdmin):
list_display = ( list_display = (
'pseudo', 'pseudo',
'surname', 'surname',
'external_mail',
'school', 'school',
'is_admin', 'is_admin',
'shell' 'shell'
@ -140,7 +140,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', )}),
@ -155,6 +155,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 = [
@ -600,10 +598,10 @@ class MailAliasForm(FormRevMixin, ModelForm):
class Meta: class Meta:
model = MailAlias model = MailAlias
exclude = ['mail'] exclude = ['user']
class MailForm(FormRevMixin, ModelForm): class MailForm(FormRevMixin, ModelForm):
"""Creation, edition d'un objet mail""" """Creation, edition des paramètres mail"""
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__) prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(MailForm, self).__init__(*args, prefix=prefix, **kwargs) super(MailForm, self).__init__(*args, prefix=prefix, **kwargs)
@ -612,5 +610,5 @@ class MailForm(FormRevMixin, 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

@ -134,6 +134,7 @@ class UserManager(BaseUserManager):
self, self,
pseudo, pseudo,
surname, surname,
external_mail,
password=None, password=None,
su=False su=False
): ):
@ -147,6 +148,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)
@ -155,19 +157,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,
@ -186,13 +188,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,
@ -225,7 +235,7 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser,
) )
USERNAME_FIELD = 'pseudo' USERNAME_FIELD = 'pseudo'
REQUIRED_FIELDS = ['surname'] REQUIRED_FIELDS = ['surname', 'external_mail']
objects = UserManager() objects = UserManager()
@ -519,7 +529,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']
@ -672,10 +682,10 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser,
""" """
Return the mail address choosen by the user Return the mail address choosen by the user
""" """
if not self.mail.internal_address: if not self.internal_address:
return self.mail.external_mail return self.external_mail
else: else:
return self.mail.mailalias_set.get(valeur=self.pseudo) return self.mailalias_set.get(valeur=self.pseudo)
def get_next_domain_name(self): def get_next_domain_name(self):
"""Look for an available name for a new interface for """Look for an available name for a new interface for
@ -1586,49 +1596,18 @@ 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 aliase pour un Mail d'utilisateur Définit un aliase pour un Mail d'utilisateur
""" """
mail = models.ForeignKey( user = models.ForeignKey(
'Mail', User,
on_delete=models.CASCADE, on_delete=models.CASCADE,
help_text="Objects Mail associé" help_text="Utilisateur associé",
null=True,
blank=True
) )
valeur = models.CharField( valeur = models.CharField(
unique=True, unique=True,
@ -1636,7 +1615,6 @@ class MailAlias(RevMixin, AclMixin, models.Model):
help_text="username de l'adresse mail" help_text="username de l'adresse 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

@ -436,8 +436,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
</div> </div>
<div id="collapse7" class="panel-collapse collapse"> <div id="collapse7" class="panel-collapse collapse">
<div class="panel-body"> <div class="panel-body">
{% can_edit user.mail %} {% can_edit user %}
<a class="btn btn-primary btn-sm" role="button" href="{% url 'users:edit-mail' users.mail.id %}"> <a class="btn btn-primary btn-sm" role="button" href="{% url 'users:edit-mail' users.id %}">
<i class="fa fa-plus-square"></i> <i class="fa fa-plus-square"></i>
Modifier les options mail Modifier les options mail
</a> </a>
@ -450,9 +450,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
<th>Adresse mail de contact</th> <th>Adresse mail de contact</th>
</tr> </tr>
<tr> <tr>
<td>{{ user.mail.external_mail }}</td> <td>{{ user.external_mail }}</td>
<td>{{ user.mail.internal_address|yesno:"Activé,Désactivé" }}</td> <td>{{ user.internal_address|yesno:"Activé,Désactivé" }}</td>
<td>{{ user.mail.external_mail }}</td> <td>{{ user.get_mail }}</td>
</table> </table>
</div> </div>
{% can_create MailAlias %} {% can_create MailAlias %}

View file

@ -81,7 +81,6 @@ from .models import (
Club, Club,
ListShell, ListShell,
MailAlias, MailAlias,
Mail,
) )
from .forms import ( from .forms import (
BanForm, BanForm,
@ -504,7 +503,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
@ -560,12 +559,12 @@ 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,
instance=mail_instance instance=user_instance
) )
if mail.is_valid(): if mail.is_valid():
if mail.changed_data: if mail.changed_data:
@ -980,7 +979,7 @@ def profil(request, users, **_kwargs):
'allow_online_payment': allow_online_payment, '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, 'asso_name': AssoOption.objects.first().name,
'alias_list': users.mail.mailalias_set.all() 'alias_list': users.mailalias_set.all()
} }
) )