Commit d7e75e59 authored by Gabriel Detraz's avatar Gabriel Detraz Committed by root

Les champs room sont portés par adherent et club : permet plusieurs clubs dans un local

parent 473ae3af
......@@ -42,7 +42,7 @@ from django.db.models import Q
from cotisations.models import Cotisation, Facture, Paiement, Vente
from machines.models import Domain, Interface, Machine
from users.models import User, Ban, Whitelist
from users.models import Adherent, User, Ban, Whitelist
from preferences.models import Service
DT_NOW = timezone.now()
......@@ -238,3 +238,13 @@ class SortTable:
return request.reverse()
else:
return request
def remove_user_room(room):
""" Déménage de force l'ancien locataire de la chambre """
try:
user = Adherent.objects.get(room=room)
except Adherent.DoesNotExist:
return
user.room = None
user.save()
......@@ -81,7 +81,7 @@ def search_result(search, type, request):
for i in aff:
if i == '0':
query_user_list = Q(room__name__icontains = search) | Q(pseudo__icontains = search) | Q(adherent__name__icontains = search) | Q(surname__icontains = search) & query1
query_user_list = Q(adherent__room__name__icontains = search) | Q(club__room__name__icontains = search) | Q(pseudo__icontains = search) | Q(adherent__name__icontains = search) | Q(surname__icontains = search) & query1
if request.user.has_perms(('cableur',)):
recherche['users_list'] = User.objects.filter(query_user_list).order_by('state', 'surname').distinct()
else :
......
......@@ -44,13 +44,12 @@ class UserAdmin(admin.ModelAdmin):
list_display = (
'surname',
'pseudo',
'room',
'email',
'school',
'shell',
'state'
)
search_fields = ('surname', 'pseudo', 'room')
search_fields = ('surname', 'pseudo')
class LdapUserAdmin(admin.ModelAdmin):
......
......@@ -41,7 +41,8 @@ from django.utils import timezone
from preferences.models import OptionalUser
from .models import User, ServiceUser, Right, School, ListRight, Whitelist
from .models import Ban, remove_user_room, Adherent, Club
from .models import Ban, Adherent, Club
from re2o.utils import remove_user_room
NOW = timezone.now()
......@@ -252,13 +253,13 @@ class MassArchiveForm(forms.Form):
utilisateurs dont la fin d'accès se situe dans le futur !")
class NewUserForm(ModelForm):
class AdherentForm(ModelForm):
"""Formulaire de base d'edition d'un user. Formulaire de base, utilisé
pour l'edition de self par self ou un cableur. On formate les champs
avec des label plus jolis"""
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(NewUserForm, self).__init__(*args, prefix=prefix, **kwargs)
super(AdherentForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['name'].label = 'Prénom'
self.fields['surname'].label = 'Nom'
self.fields['school'].label = 'Établissement'
......@@ -291,18 +292,31 @@ class NewUserForm(ModelForm):
)
return telephone
force = forms.BooleanField(
label="Forcer le déménagement ?",
initial=False,
required=False
)
def clean_force(self):
"""On supprime l'ancien user de la chambre si et seulement si la
case est cochée"""
if self.cleaned_data.get('force', False):
remove_user_room(self.cleaned_data.get('room'))
return
class NewClubForm(ModelForm):
class ClubForm(ModelForm):
"""Formulaire de base d'edition d'un user. Formulaire de base, utilisé
pour l'edition de self par self ou un cableur. On formate les champs
avec des label plus jolis"""
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(NewClubForm, self).__init__(*args, prefix=prefix, **kwargs)
super(ClubForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['surname'].label = 'Nom'
self.fields['school'].label = 'Établissement'
self.fields['comment'].label = 'Commentaire'
self.fields['room'].label = 'Chambre'
self.fields['room'].label = 'Local'
self.fields['room'].empty_label = "Pas de chambre"
self.fields['school'].empty_label = "Séléctionner un établissement"
......@@ -330,50 +344,29 @@ class NewClubForm(ModelForm):
return telephone
class BaseInfoForm(ModelForm):
"""Formulaire de base d'edition d'un user. Formulaire de base, utilisé
pour l'edition de self par self ou un cableur. On formate les champs
avec des label plus jolis"""
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(BaseInfoForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['surname'].label = 'Nom'
self.fields['school'].label = 'Établissement'
self.fields['comment'].label = 'Commentaire'
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 = User
class FullAdherentForm(AdherentForm):
"""Edition complète d'un user. Utilisé par admin,
permet d'editer normalement la chambre, ou le shell
Herite de la base"""
class Meta(AdherentForm.Meta):
fields = [
'name',
'surname',
'pseudo',
'email',
'school',
'comment',
'room',
'shell',
'telephone',
]
def clean_telephone(self):
"""Verifie que le tel est présent si 'option est validée
dans preferences"""
telephone = self.cleaned_data['telephone']
preferences, _created = OptionalUser.objects.get_or_create()
if not telephone and preferences.is_tel_mandatory:
raise forms.ValidationError(
"Un numéro de téléphone valide est requis"
)
return telephone
class EditInfoForm(BaseInfoForm):
class FullClubForm(ClubForm):
"""Edition complète d'un user. Utilisé par admin,
permet d'editer normalement la chambre, ou le shell
Herite de la base"""
class Meta(BaseInfoForm.Meta):
class Meta(ClubForm.Meta):
fields = [
'surname',
'pseudo',
......@@ -386,29 +379,6 @@ class EditInfoForm(BaseInfoForm):
]
class InfoForm(EditInfoForm):
""" Utile pour forcer un déménagement quand il y a déjà un user en place
Formuaire utilisé pour la creation initiale"""
force = forms.BooleanField(
label="Forcer le déménagement ?",
initial=False,
required=False
)
def clean_force(self):
"""On supprime l'ancien user de la chambre si et seulement si la
case est cochée"""
if self.cleaned_data.get('force', False):
remove_user_room(self.cleaned_data.get('room'))
return
class UserForm(InfoForm):
""" Model form general"""
class Meta(InfoForm.Meta):
fields = '__all__'
class PasswordForm(ModelForm):
""" Formulaire de changement brut de mot de passe.
Ne pas utiliser sans traitement"""
......
# -*- coding: utf-8 -*-
# Generated by Django 1.10.7 on 2017-10-24 23:54
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
def create_move_room(apps, schema_editor):
User = apps.get_model('users', 'User')
Adherent = apps.get_model('users', 'Adherent')
Club = apps.get_model('users', 'Club')
db_alias = schema_editor.connection.alias
users = Adherent.objects.using(db_alias).all()
clubs = Club.objects.using(db_alias).all()
for user in users:
user.room_adherent_id = user.room_id
user.save(using=db_alias)
for user in clubs:
user.room_club_id = user.room_id
user.save(using=db_alias)
def delete_move_room(apps, schema_editor):
User = apps.get_model('users', 'User')
Adherent = apps.get_model('users', 'Adherent')
Club = apps.get_model('users', 'Club')
db_alias = schema_editor.connection.alias
users = Adherent.objects.using(db_alias).all()
clubs = Club.objects.using(db_alias).all()
for user in users:
user.room_id = user.room_adherent_id
user.save(using=db_alias)
for user in clubs:
user.room_id = user.room_club_id
user.save(using=db_alias)
class Migration(migrations.Migration):
dependencies = [
('topologie', '0031_auto_20171015_2033'),
('users', '0057_auto_20171023_0301'),
]
operations = [
migrations.AddField(
model_name='adherent',
name='room_adherent',
field=models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='topologie.Room'),
),
migrations.AddField(
model_name='club',
name='room_club',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='topologie.Room'),
),
migrations.RunPython(create_move_room, delete_move_room),
migrations.RemoveField(
model_name='user',
name='room',
),
]
# -*- coding: utf-8 -*-
# Generated by Django 1.10.7 on 2017-10-25 16:54
from __future__ import unicode_literals
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('users', '0058_auto_20171025_0154'),
]
operations = [
migrations.RenameField(
model_name='adherent',
old_name='room_adherent',
new_name='room',
),
migrations.RenameField(
model_name='club',
old_name='room_club',
new_name='room',
),
]
......@@ -81,15 +81,6 @@ DT_NOW = timezone.now()
# Utilitaires généraux
def remove_user_room(room):
""" Déménage de force l'ancien locataire de la chambre """
try:
user = User.objects.get(room=room)
except User.DoesNotExist:
return
user.room = None
user.save()
def linux_user_check(login):
""" Validation du pseudo pour respecter les contraintes unix"""
......@@ -230,12 +221,6 @@ class User(AbstractBaseUser):
max_length=255,
blank=True
)
room = models.OneToOneField(
'topologie.Room',
on_delete=models.PROTECT,
blank=True,
null=True
)
pwd_ntlm = models.CharField(max_length=255)
state = models.IntegerField(choices=STATES, default=STATE_ACTIVE)
registered = models.DateTimeField(auto_now_add=True)
......@@ -256,6 +241,16 @@ class User(AbstractBaseUser):
else:
return ''
@cached_property
def room(self):
"""Alias vers room """
if self.is_class_adherent:
return self.adherent.room
elif self.is_class_club:
return self.club.room
else:
raise NotImplementedError("Type inconnu")
@cached_property
def class_name(self):
"""Renvoie si il s'agit d'un adhérent ou d'un club"""
......@@ -712,10 +707,22 @@ class User(AbstractBaseUser):
class Adherent(User):
name = models.CharField(max_length=255)
room = models.OneToOneField(
'topologie.Room',
on_delete=models.PROTECT,
blank=True,
null=True
)
pass
class Club(User):
room = models.ForeignKey(
'topologie.Room',
on_delete=models.PROTECT,
blank=True,
null=True
)
pass
......
......@@ -55,10 +55,10 @@ from users.serializers import MailSerializer
from users.models import User, Right, Ban, Whitelist, School, ListRight
from users.models import Request, ServiceUser, Adherent, Club
from users.forms import DelRightForm, BanForm, WhitelistForm, DelSchoolForm
from users.forms import DelListRightForm, NewListRightForm
from users.forms import InfoForm, BaseInfoForm, StateForm
from users.forms import DelListRightForm, NewListRightForm, FullAdherentForm
from users.forms import StateForm, FullClubForm
from users.forms import RightForm, SchoolForm, EditServiceUserForm
from users.forms import ServiceUserForm, ListRightForm, NewUserForm, NewClubForm
from users.forms import ServiceUserForm, ListRightForm, AdherentForm, ClubForm
from users.forms import MassArchiveForm, PassForm, ResetPasswordForm
from cotisations.models import Facture
from machines.models import Machine
......@@ -85,7 +85,7 @@ def password_change_action(u_form, user, request, req=False):
def new_user(request):
""" Vue de création d'un nouvel utilisateur,
envoie un mail pour le mot de passe"""
user = NewUserForm(request.POST or None)
user = AdherentForm(request.POST or None)
if user.is_valid():
user = user.save(commit=False)
with transaction.atomic(), reversion.create_revision():
......@@ -104,7 +104,7 @@ def new_user(request):
def new_club(request):
""" Vue de création d'un nouveau club,
envoie un mail pour le mot de passe"""
club = NewClubForm(request.POST or None)
club = ClubForm(request.POST or None)
if club.is_valid():
club = club.save(commit=False)
with transaction.atomic(), reversion.create_revision():
......@@ -118,6 +118,24 @@ def new_club(request):
return form({'userform': club}, 'users/user.html', request)
def select_user_edit_form(request, user):
"""Fonction de choix du bon formulaire, en fonction de:
- droit
- type d'object
"""
if not request.user.has_perms(('cableur',)):
if user.is_class_adherent:
user = AdherentForm(request.POST or None, instance=user.adherent)
elif user.is_class_club:
user = ClubForm(request.POST or None, instance=user.club)
else:
if user.is_class_adherent:
user = FullAdherentForm(request.POST or None, instance=user.adherent)
elif user.is_class_club:
user = FullClubForm(request.POST or None, instance=user.club)
return user
@login_required
def edit_info(request, userid):
""" Edite un utilisateur à partir de son id,
......@@ -132,10 +150,7 @@ def edit_info(request, userid):
messages.error(request, "Vous ne pouvez pas modifier un autre\
user que vous sans droit cableur")
return redirect("/users/profil/" + str(request.user.id))
if not request.user.has_perms(('cableur',)):
user = BaseInfoForm(request.POST or None, instance=user)
else:
user = InfoForm(request.POST or None, instance=user)
user = select_user_edit_form(request, user)
if user.is_valid():
with transaction.atomic(), reversion.create_revision():
user.save()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment