8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-05 01:16:27 +00:00

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

This commit is contained in:
Gabriel Detraz 2017-10-25 23:37:12 +02:00 committed by root
parent 473ae3afea
commit d7e75e59b6
8 changed files with 183 additions and 96 deletions

View file

@ -42,7 +42,7 @@ from django.db.models import Q
from cotisations.models import Cotisation, Facture, Paiement, Vente from cotisations.models import Cotisation, Facture, Paiement, Vente
from machines.models import Domain, Interface, Machine 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 from preferences.models import Service
DT_NOW = timezone.now() DT_NOW = timezone.now()
@ -238,3 +238,13 @@ class SortTable:
return request.reverse() return request.reverse()
else: else:
return request 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()

View file

@ -81,7 +81,7 @@ def search_result(search, type, request):
for i in aff: for i in aff:
if i == '0': 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',)): if request.user.has_perms(('cableur',)):
recherche['users_list'] = User.objects.filter(query_user_list).order_by('state', 'surname').distinct() recherche['users_list'] = User.objects.filter(query_user_list).order_by('state', 'surname').distinct()
else : else :

View file

@ -44,13 +44,12 @@ class UserAdmin(admin.ModelAdmin):
list_display = ( list_display = (
'surname', 'surname',
'pseudo', 'pseudo',
'room',
'email', 'email',
'school', 'school',
'shell', 'shell',
'state' 'state'
) )
search_fields = ('surname', 'pseudo', 'room') search_fields = ('surname', 'pseudo')
class LdapUserAdmin(admin.ModelAdmin): class LdapUserAdmin(admin.ModelAdmin):

View file

@ -41,7 +41,8 @@ from django.utils import timezone
from preferences.models import OptionalUser from preferences.models import OptionalUser
from .models import User, ServiceUser, Right, School, ListRight, Whitelist 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() NOW = timezone.now()
@ -252,13 +253,13 @@ class MassArchiveForm(forms.Form):
utilisateurs dont la fin d'accès se situe dans le futur !") 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é """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 pour l'edition de self par self ou un cableur. On formate les champs
avec des label plus jolis""" avec des label plus jolis"""
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(NewUserForm, self).__init__(*args, prefix=prefix, **kwargs) super(AdherentForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['name'].label = 'Prénom' self.fields['name'].label = 'Prénom'
self.fields['surname'].label = 'Nom' self.fields['surname'].label = 'Nom'
self.fields['school'].label = 'Établissement' self.fields['school'].label = 'Établissement'
@ -291,18 +292,31 @@ class NewUserForm(ModelForm):
) )
return telephone return telephone
force = forms.BooleanField(
label="Forcer le déménagement ?",
initial=False,
required=False
)
class NewClubForm(ModelForm): 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 ClubForm(ModelForm):
"""Formulaire de base d'edition d'un user. Formulaire de base, utilisé """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 pour l'edition de self par self ou un cableur. On formate les champs
avec des label plus jolis""" avec des label plus jolis"""
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(NewClubForm, self).__init__(*args, prefix=prefix, **kwargs) super(ClubForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['surname'].label = 'Nom' self.fields['surname'].label = 'Nom'
self.fields['school'].label = 'Établissement' self.fields['school'].label = 'Établissement'
self.fields['comment'].label = 'Commentaire' 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['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"
@ -330,51 +344,13 @@ class NewClubForm(ModelForm):
return telephone return telephone
class FullAdherentForm(AdherentForm):
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
fields = [
'surname',
'pseudo',
'email',
'school',
'comment',
'room',
'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):
"""Edition complète d'un user. Utilisé par admin, """Edition complète d'un user. Utilisé par admin,
permet d'editer normalement la chambre, ou le shell permet d'editer normalement la chambre, ou le shell
Herite de la base""" Herite de la base"""
class Meta(BaseInfoForm.Meta): class Meta(AdherentForm.Meta):
fields = [ fields = [
'name',
'surname', 'surname',
'pseudo', 'pseudo',
'email', 'email',
@ -386,27 +362,21 @@ class EditInfoForm(BaseInfoForm):
] ]
class InfoForm(EditInfoForm): class FullClubForm(ClubForm):
""" Utile pour forcer un déménagement quand il y a déjà un user en place """Edition complète d'un user. Utilisé par admin,
Formuaire utilisé pour la creation initiale""" permet d'editer normalement la chambre, ou le shell
force = forms.BooleanField( Herite de la base"""
label="Forcer le déménagement ?", class Meta(ClubForm.Meta):
initial=False, fields = [
required=False 'surname',
) 'pseudo',
'email',
def clean_force(self): 'school',
"""On supprime l'ancien user de la chambre si et seulement si la 'comment',
case est cochée""" 'room',
if self.cleaned_data.get('force', False): 'shell',
remove_user_room(self.cleaned_data.get('room')) 'telephone',
return ]
class UserForm(InfoForm):
""" Model form general"""
class Meta(InfoForm.Meta):
fields = '__all__'
class PasswordForm(ModelForm): class PasswordForm(ModelForm):

View file

@ -0,0 +1,61 @@
# -*- 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',
),
]

View file

@ -0,0 +1,25 @@
# -*- 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',
),
]

View file

@ -81,15 +81,6 @@ DT_NOW = timezone.now()
# Utilitaires généraux # 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): def linux_user_check(login):
""" Validation du pseudo pour respecter les contraintes unix""" """ Validation du pseudo pour respecter les contraintes unix"""
@ -230,12 +221,6 @@ class User(AbstractBaseUser):
max_length=255, max_length=255,
blank=True blank=True
) )
room = models.OneToOneField(
'topologie.Room',
on_delete=models.PROTECT,
blank=True,
null=True
)
pwd_ntlm = models.CharField(max_length=255) pwd_ntlm = models.CharField(max_length=255)
state = models.IntegerField(choices=STATES, default=STATE_ACTIVE) state = models.IntegerField(choices=STATES, default=STATE_ACTIVE)
registered = models.DateTimeField(auto_now_add=True) registered = models.DateTimeField(auto_now_add=True)
@ -256,6 +241,16 @@ class User(AbstractBaseUser):
else: else:
return '' 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 @cached_property
def class_name(self): def class_name(self):
"""Renvoie si il s'agit d'un adhérent ou d'un club""" """Renvoie si il s'agit d'un adhérent ou d'un club"""
@ -712,10 +707,22 @@ class User(AbstractBaseUser):
class Adherent(User): class Adherent(User):
name = models.CharField(max_length=255) name = models.CharField(max_length=255)
room = models.OneToOneField(
'topologie.Room',
on_delete=models.PROTECT,
blank=True,
null=True
)
pass pass
class Club(User): class Club(User):
room = models.ForeignKey(
'topologie.Room',
on_delete=models.PROTECT,
blank=True,
null=True
)
pass pass

View file

@ -55,10 +55,10 @@ from users.serializers import MailSerializer
from users.models import User, Right, Ban, Whitelist, School, ListRight from users.models import User, Right, Ban, Whitelist, School, ListRight
from users.models import Request, ServiceUser, Adherent, Club from users.models import Request, ServiceUser, Adherent, Club
from users.forms import DelRightForm, BanForm, WhitelistForm, DelSchoolForm from users.forms import DelRightForm, BanForm, WhitelistForm, DelSchoolForm
from users.forms import DelListRightForm, NewListRightForm from users.forms import DelListRightForm, NewListRightForm, FullAdherentForm
from users.forms import InfoForm, BaseInfoForm, StateForm from users.forms import StateForm, FullClubForm
from users.forms import RightForm, SchoolForm, EditServiceUserForm 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 users.forms import MassArchiveForm, PassForm, ResetPasswordForm
from cotisations.models import Facture from cotisations.models import Facture
from machines.models import Machine from machines.models import Machine
@ -85,7 +85,7 @@ def password_change_action(u_form, user, request, req=False):
def new_user(request): def new_user(request):
""" Vue de création d'un nouvel utilisateur, """ Vue de création d'un nouvel utilisateur,
envoie un mail pour le mot de passe""" envoie un mail pour le mot de passe"""
user = NewUserForm(request.POST or None) user = AdherentForm(request.POST or None)
if user.is_valid(): if user.is_valid():
user = user.save(commit=False) user = user.save(commit=False)
with transaction.atomic(), reversion.create_revision(): with transaction.atomic(), reversion.create_revision():
@ -104,7 +104,7 @@ def new_user(request):
def new_club(request): def new_club(request):
""" Vue de création d'un nouveau club, """ Vue de création d'un nouveau club,
envoie un mail pour le mot de passe""" envoie un mail pour le mot de passe"""
club = NewClubForm(request.POST or None) club = ClubForm(request.POST or None)
if club.is_valid(): if club.is_valid():
club = club.save(commit=False) club = club.save(commit=False)
with transaction.atomic(), reversion.create_revision(): with transaction.atomic(), reversion.create_revision():
@ -118,6 +118,24 @@ def new_club(request):
return form({'userform': club}, 'users/user.html', 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 @login_required
def edit_info(request, userid): def edit_info(request, userid):
""" Edite un utilisateur à partir de son id, """ 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\ messages.error(request, "Vous ne pouvez pas modifier un autre\
user que vous sans droit cableur") user que vous sans droit cableur")
return redirect("/users/profil/" + str(request.user.id)) return redirect("/users/profil/" + str(request.user.id))
if not request.user.has_perms(('cableur',)): user = select_user_edit_form(request, user)
user = BaseInfoForm(request.POST or None, instance=user)
else:
user = InfoForm(request.POST or None, instance=user)
if user.is_valid(): if user.is_valid():
with transaction.atomic(), reversion.create_revision(): with transaction.atomic(), reversion.create_revision():
user.save() user.save()