views.py 35 KB
Newer Older
lhark's avatar
lhark committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
# Copyright © 2017  Gabriel Détraz
# Copyright © 2017  Goulven Kermarec
# Copyright © 2017  Augustin Lemesle
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

chirac's avatar
chirac committed
23
# App de gestion des users pour re2o
Dalahro's avatar
Dalahro committed
24
# Goulven Kermarec, Gabriel Détraz, Lemesle Augustin
chirac's avatar
chirac committed
25
# Gplv2
26 27 28 29 30 31 32 33 34
"""
Module des views.

On définit les vues pour l'ajout, l'edition des users : infos personnelles,
mot de passe, etc

Permet aussi l'ajout, edition et suppression des droits, des bannissements,
des whitelist, des services users et des écoles
"""
35 36 37

from __future__ import unicode_literals

38
from django.urls import reverse
39
from django.shortcuts import get_object_or_404, render, redirect
40
from django.contrib import messages
41
from django.contrib.auth.decorators import login_required, permission_required
42
from django.db.models import ProtectedError, Count, Max
43
from django.utils import timezone
44
from django.db import transaction
45
from django.http import HttpResponse
46
from django.http import HttpResponseRedirect
47 48 49
from django.views.decorators.csrf import csrf_exempt

from rest_framework.renderers import JSONRenderer
Maël Kervella's avatar
Maël Kervella committed
50
from reversion import revisions as reversion
51

52
from cotisations.models import Facture, Paiement
Maël Kervella's avatar
Maël Kervella committed
53
from machines.models import Machine
Gabriel Detraz's avatar
Gabriel Detraz committed
54
from preferences.models import OptionalUser, GeneralOption, AssoOption
Maël Kervella's avatar
Maël Kervella committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
from re2o.views import form
from re2o.utils import (
    all_has_access,
    SortTable,
    re2o_paginator
)
from re2o.acl import (
    can_create,
    can_edit,
    can_delete_set,
    can_delete,
    can_view,
    can_view_all,
    can_change
)
70
from cotisations.utils import find_payment_method
lhark's avatar
lhark committed
71

Maël Kervella's avatar
Maël Kervella committed
72 73
from .serializers import MailingSerializer, MailingMemberSerializer
from .models import (
74 75 76 77 78 79 80 81
    User,
    Ban,
    Whitelist,
    School,
    ListRight,
    Request,
    ServiceUser,
    Adherent,
LEVY-FALK Hugo's avatar
LEVY-FALK Hugo committed
82
    Club,
83
    ListShell,
84 85
    MailAlias,
    Mail,
86
)
Maël Kervella's avatar
Maël Kervella committed
87
from .forms import (
88 89
    BanForm,
    WhitelistForm,
90 91
    MailAliasForm,
    MailForm,
92 93 94 95 96
    DelSchoolForm,
    DelListRightForm,
    NewListRightForm,
    StateForm,
    SchoolForm,
97
    ShellForm,
98 99 100 101 102 103 104 105
    EditServiceUserForm,
    ServiceUserForm,
    ListRightForm,
    AdherentForm,
    ClubForm,
    MassArchiveForm,
    PassForm,
    ResetPasswordForm,
106 107
    ClubAdminandMembersForm,
    GroupForm
108
)
109

Maël Kervella's avatar
Maël Kervella committed
110

111
@can_create(Adherent)
chirac's avatar
chirac committed
112
def new_user(request):
113 114
    """ Vue de création d'un nouvel utilisateur,
    envoie un mail pour le mot de passe"""
115
    user = AdherentForm(request.POST or None, user=request.user)
116 117
    GTU_sum_up = GeneralOption.get_cached_value('GTU_sum_up')
    GTU = GeneralOption.get_cached_value('GTU')
118
    if user.is_valid():
119
        user = user.save()
120
        user.reset_passwd_mail(request)
121 122
        messages.success(request, "L'utilisateur %s a été crée, un mail\
        pour l'initialisation du mot de passe a été envoyé" % user.pseudo)
123 124
        return redirect(reverse(
            'users:profil',
Maël Kervella's avatar
Maël Kervella committed
125 126 127 128 129 130 131 132 133 134 135 136 137
            kwargs={'userid': str(user.id)}
        ))
    return form(
        {
            'userform': user,
            'GTU_sum_up': GTU_sum_up,
            'GTU': GTU,
            'showCGU': True,
            'action_name': 'Créer un utilisateur'
        },
        'users/user.html',
        request
    )
138

139

140
@login_required
141
@can_create(Club)
142 143 144
def new_club(request):
    """ Vue de création d'un nouveau club,
    envoie un mail pour le mot de passe"""
145
    club = ClubForm(request.POST or None, user=request.user)
146 147
    if club.is_valid():
        club = club.save(commit=False)
148
        club.save()
149 150 151
        club.reset_passwd_mail(request)
        messages.success(request, "L'utilisateur %s a été crée, un mail\
        pour l'initialisation du mot de passe a été envoyé" % club.pseudo)
152 153
        return redirect(reverse(
            'users:profil',
Maël Kervella's avatar
Maël Kervella committed
154 155 156 157 158 159 160
            kwargs={'userid': str(club.id)}
        ))
    return form(
        {'userform': club, 'showCGU': False, 'action_name': 'Créer un club'},
        'users/user.html',
        request
    )
161 162


163
@login_required
164
@can_edit(Club)
165
def edit_club_admin_members(request, club_instance, **_kwargs):
166 167
    """Vue d'edition de la liste des users administrateurs et
    membres d'un club"""
Maël Kervella's avatar
Maël Kervella committed
168 169 170 171
    club = ClubAdminandMembersForm(
        request.POST or None,
        instance=club_instance
    )
172
    if club.is_valid():
173 174 175
        if club.changed_data:
            club.save()
            messages.success(request, "Le club a bien été modifié")
176 177
        return redirect(reverse(
            'users:profil',
Maël Kervella's avatar
Maël Kervella committed
178 179 180 181 182 183 184 185 186 187 188
            kwargs={'userid': str(club_instance.id)}
        ))
    return form(
        {
            'userform': club,
            'showCGU': False,
            'action_name': 'Editer les admin et membres'
        },
        'users/user.html',
        request
    )
189 190


chirac's avatar
chirac committed
191
@login_required
192
@can_edit(User)
193
def edit_info(request, user, userid):
194 195 196
    """ Edite un utilisateur à partir de son id,
    si l'id est différent de request.user, vérifie la
    possession du droit cableur """
197
    if user.is_class_adherent:
Maël Kervella's avatar
Maël Kervella committed
198
        user_form = AdherentForm(
199 200 201 202
            request.POST or None,
            instance=user.adherent,
            user=request.user
        )
Maël Kervella's avatar
Maël Kervella committed
203 204
    else:
        user_form = ClubForm(
205 206 207 208
            request.POST or None,
            instance=user.club,
            user=request.user
        )
Maël Kervella's avatar
Maël Kervella committed
209 210 211
    if user_form.is_valid():
        if user_form.changed_data:
            user_form.save()
212
            messages.success(request, "L'user a bien été modifié")
213 214
        return redirect(reverse(
            'users:profil',
Maël Kervella's avatar
Maël Kervella committed
215 216 217
            kwargs={'userid': str(userid)}
        ))
    return form(
Maël Kervella's avatar
Maël Kervella committed
218
        {'userform': user_form, 'action_name': "Editer l'utilisateur"},
Maël Kervella's avatar
Maël Kervella committed
219 220 221
        'users/user.html',
        request
    )
222

223

chirac's avatar
chirac committed
224
@login_required
225
@can_edit(User, 'state')
226
def state(request, user, userid):
chirac's avatar
chirac committed
227
    """ Change the state (active/unactive/archived) of a user"""
Maël Kervella's avatar
Maël Kervella committed
228 229 230 231
    state_form = StateForm(request.POST or None, instance=user)
    if state_form.is_valid():
        if state_form.changed_data:
            state_form.save()
232
            messages.success(request, "Etat changé avec succès")
233 234
        return redirect(reverse(
            'users:profil',
Maël Kervella's avatar
Maël Kervella committed
235 236 237
            kwargs={'userid': str(userid)}
        ))
    return form(
Maël Kervella's avatar
Maël Kervella committed
238
        {'userform': state_form, 'action_name': "Editer l'état"},
Maël Kervella's avatar
Maël Kervella committed
239 240 241
        'users/user.html',
        request
    )
242

243

244
@login_required
245
@can_edit(User, 'groups')
246
def groups(request, user, userid):
Maël Kervella's avatar
Maël Kervella committed
247
    """ View to edit the groups of a user """
248 249
    group_form = GroupForm(request.POST or None,
                           instance=user, user=request.user)
Maël Kervella's avatar
Maël Kervella committed
250 251 252
    if group_form.is_valid():
        if group_form.changed_data:
            group_form.save()
253
            messages.success(request, "Groupes changés avec succès")
254 255
        return redirect(reverse(
            'users:profil',
Maël Kervella's avatar
Maël Kervella committed
256
            kwargs={'userid': str(userid)}
257
        ))
Maël Kervella's avatar
Maël Kervella committed
258
    return form(
Maël Kervella's avatar
Maël Kervella committed
259
        {'userform': group_form, 'action_name': 'Editer les groupes'},
Maël Kervella's avatar
Maël Kervella committed
260 261 262
        'users/user.html',
        request
    )
263 264


chirac's avatar
chirac committed
265
@login_required
266
@can_edit(User, 'password')
267
def password(request, user, userid):
chirac's avatar
chirac committed
268 269 270
    """ Reinitialisation d'un mot de passe à partir de l'userid,
    pour self par défaut, pour tous sans droit si droit cableur,
    pour tous si droit bureau """
271
    u_form = PassForm(request.POST or None, instance=user, user=request.user)
272
    if u_form.is_valid():
273 274 275
        if u_form.changed_data:
            u_form.save()
            messages.success(request, "Le mot de passe a changé")
276
        return redirect(reverse(
Maël Kervella's avatar
Maël Kervella committed
277
            'users:profil',
Maël Kervella's avatar
Maël Kervella committed
278
            kwargs={'userid': str(userid)}
279
        ))
Maël Kervella's avatar
Maël Kervella committed
280 281 282 283 284
    return form(
        {'userform': u_form, 'action_name': 'Changer le mot de passe'},
        'users/user.html',
        request
    )
chirac's avatar
chirac committed
285

286

287
@login_required
288
@can_edit(User, 'groups')
289
def del_group(request, user, listrightid, **_kwargs):
Maël Kervella's avatar
Maël Kervella committed
290
    """ View used to delete a group """
291 292 293
    user.groups.remove(ListRight.objects.get(id=listrightid))
    user.save()
    messages.success(request, "Droit supprimé à %s" % user)
294
    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
295 296


Hugo LEVY-FALK's avatar
Hugo LEVY-FALK committed
297
@login_required
Hugo LEVY-FALK's avatar
ACL  
Hugo LEVY-FALK committed
298
@can_edit(User, 'is_superuser')
Hugo LEVY-FALK's avatar
Hugo LEVY-FALK committed
299 300 301 302 303 304 305 306
def del_superuser(request, user, **_kwargs):
    """Remove the superuser right of an user."""
    user.is_superuser = False
    user.save()
    messages.success(request, "%s n'est plus superuser" % user)
    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))


307
@login_required
308
@can_create(ServiceUser)
309 310 311 312
def new_serviceuser(request):
    """ Vue de création d'un nouvel utilisateur service"""
    user = ServiceUserForm(request.POST or None)
    if user.is_valid():
313
        user.save()
314 315
        messages.success(
            request,
316
            "L'utilisateur a été crée"
317
        )
318
        return redirect(reverse('users:index-serviceusers'))
Maël Kervella's avatar
Maël Kervella committed
319 320 321 322 323
    return form(
        {'userform': user, 'action_name': 'Créer un serviceuser'},
        'users/user.html',
        request
    )
324

325

326
@login_required
327
@can_edit(ServiceUser)
328
def edit_serviceuser(request, serviceuser, **_kwargs):
329
    """ Edit a ServiceUser """
Maël Kervella's avatar
Maël Kervella committed
330 331 332 333
    serviceuser = EditServiceUserForm(
        request.POST or None,
        instance=serviceuser
    )
Gabriel Detraz's avatar
Gabriel Detraz committed
334 335
    if serviceuser.is_valid():
        if serviceuser.changed_data:
336
            serviceuser.save()
337
        messages.success(request, "L'user a bien été modifié")
338
        return redirect(reverse('users:index-serviceusers'))
Maël Kervella's avatar
Maël Kervella committed
339 340 341 342 343
    return form(
        {'userform': serviceuser, 'action_name': 'Editer un serviceuser'},
        'users/user.html',
        request
    )
344

345

346
@login_required
347
@can_delete(ServiceUser)
348
def del_serviceuser(request, serviceuser, **_kwargs):
349
    """Suppression d'un ou plusieurs serviceusers"""
350
    if request.method == "POST":
Gabriel Detraz's avatar
Gabriel Detraz committed
351
        serviceuser.delete()
352
        messages.success(request, "L'user a été détruit")
353
        return redirect(reverse('users:index-serviceusers'))
354
    return form(
Gabriel Detraz's avatar
Gabriel Detraz committed
355
        {'objet': serviceuser, 'objet_name': 'serviceuser'},
356 357 358 359
        'users/delete.html',
        request
    )

360

chirac's avatar
chirac committed
361
@login_required
362 363 364
@can_create(Ban)
@can_edit(User)
def add_ban(request, user, userid):
365 366
    """ Ajouter un banissement, nécessite au moins le droit bofh
    (a fortiori bureau)
chirac's avatar
chirac committed
367
    Syntaxe : JJ/MM/AAAA , heure optionnelle, prend effet immédiatement"""
368 369 370
    ban_instance = Ban(user=user)
    ban = BanForm(request.POST or None, instance=ban_instance)
    if ban.is_valid():
Maël Kervella's avatar
Maël Kervella committed
371
        ban.save()
372
        messages.success(request, "Bannissement ajouté")
373 374
        return redirect(reverse(
            'users:profil',
Maël Kervella's avatar
Maël Kervella committed
375
            kwargs={'userid': str(userid)}
376
        ))
377
    if user.is_ban():
378 379 380 381
        messages.error(
            request,
            "Attention, cet utilisateur a deja un bannissement actif"
        )
Maël Kervella's avatar
Maël Kervella committed
382 383 384 385 386 387
    return form(
        {'userform': ban, 'action_name': 'Ajouter un ban'},
        'users/user.html',
        request
    )

388

chirac's avatar
chirac committed
389
@login_required
390
@can_edit(Ban)
391
def edit_ban(request, ban_instance, **_kwargs):
392 393
    """ Editer un bannissement, nécessite au moins le droit bofh
    (a fortiori bureau)
chirac's avatar
chirac committed
394
    Syntaxe : JJ/MM/AAAA , heure optionnelle, prend effet immédiatement"""
395 396
    ban = BanForm(request.POST or None, instance=ban_instance)
    if ban.is_valid():
397 398 399
        if ban.changed_data:
            ban.save()
            messages.success(request, "Bannissement modifié")
400
        return redirect(reverse('users:index'))
Maël Kervella's avatar
Maël Kervella committed
401 402 403 404 405
    return form(
        {'userform': ban, 'action_name': 'Editer un ban'},
        'users/user.html',
        request
    )
406

407

408 409 410
@login_required
@can_delete(Ban)
def del_ban(request, ban, **_kwargs):
411 412 413 414 415 416 417 418 419 420 421 422 423
    """ Supprime un banissement"""
    if request.method == "POST":
        ban.delete()
        messages.success(request, "Le banissement a été supprimé")
        return redirect(reverse(
            'users:profil',
            kwargs={'userid': str(ban.user.id)}
        ))
    return form(
        {'objet': ban, 'objet_name': 'ban'},
        'users/delete.html',
        request
    )
424

425

chirac's avatar
chirac committed
426
@login_required
427 428 429
@can_create(Whitelist)
@can_edit(User)
def add_whitelist(request, user, userid):
430 431 432 433
    """ Accorder un accès gracieux, temporaire ou permanent.
    Need droit cableur
    Syntaxe : JJ/MM/AAAA , heure optionnelle, prend effet immédiatement,
    raison obligatoire"""
chirac's avatar
chirac committed
434
    whitelist_instance = Whitelist(user=user)
435 436 437 438
    whitelist = WhitelistForm(
        request.POST or None,
        instance=whitelist_instance
    )
chirac's avatar
chirac committed
439
    if whitelist.is_valid():
440
        whitelist.save()
chirac's avatar
chirac committed
441
        messages.success(request, "Accès à titre gracieux accordé")
442 443
        return redirect(reverse(
            'users:profil',
Maël Kervella's avatar
Maël Kervella committed
444 445
            kwargs={'userid': str(userid)}
        ))
446
    if user.is_whitelisted():
447 448 449 450
        messages.error(
            request,
            "Attention, cet utilisateur a deja un accès gracieux actif"
        )
Maël Kervella's avatar
Maël Kervella committed
451 452 453 454 455
    return form(
        {'userform': whitelist, 'action_name': 'Ajouter une whitelist'},
        'users/user.html',
        request
    )
chirac's avatar
chirac committed
456

457

chirac's avatar
chirac committed
458
@login_required
459
@can_edit(Whitelist)
460
def edit_whitelist(request, whitelist_instance, **_kwargs):
461 462 463 464 465 466 467 468
    """ Editer un accès gracieux, temporaire ou permanent.
    Need droit cableur
    Syntaxe : JJ/MM/AAAA , heure optionnelle, prend effet immédiatement,
    raison obligatoire"""
    whitelist = WhitelistForm(
        request.POST or None,
        instance=whitelist_instance
    )
chirac's avatar
chirac committed
469
    if whitelist.is_valid():
470 471 472
        if whitelist.changed_data:
            whitelist.save()
            messages.success(request, "Whitelist modifiée")
473
        return redirect(reverse('users:index'))
Maël Kervella's avatar
Maël Kervella committed
474 475 476 477 478
    return form(
        {'userform': whitelist, 'action_name': 'Editer une whitelist'},
        'users/user.html',
        request
    )
chirac's avatar
chirac committed
479

480

481 482 483
@login_required
@can_delete(Whitelist)
def del_whitelist(request, whitelist, **_kwargs):
484 485 486 487 488 489 490 491 492 493 494 495 496 497
    """ Supprime un acces gracieux"""
    if request.method == "POST":
        whitelist.delete()
        messages.success(request, "L'accés gracieux a été supprimé")
        return redirect(reverse(
            'users:profil',
            kwargs={'userid': str(whitelist.user.id)}
        ))
    return form(
        {'objet': whitelist, 'objet_name': 'whitelist'},
        'users/delete.html',
        request
    )

498

499 500 501 502 503 504
@login_required
@can_create(MailAlias)
@can_edit(User)
def add_mailalias(request, user, userid):
    """ Créer un alias """
    mailalias_instance = MailAlias(mail=user.mail)
505
    mailalias = MailAliasForm(
506
        request.POST or None,
507
        instance=mailalias_instance
508
    )
509 510
    if mailalias.is_valid():
        mailalias.save()
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
        messages.success(request, "Alias créé")
        return redirect(reverse(
            'users:profil',
            kwargs={'userid': str(userid)}
        ))
    return form(
        {'userform': mailalias, 'action_name': 'Ajouter un alias mail'},
        'users/user.html',
        request
    )

@login_required
@can_edit(MailAlias)
def edit_mailalias(request, mailalias_instance, **_kwargs):
    """ Editer un alias mail"""
    mailalias = MailAliasForm(
        request.POST or None,
        instance=mailalias_instance
    )
530 531 532
    if mailalias.is_valid():
        if mailalias.changed_data:
            mailalias.save()
533
            messages.success(request, "Alias modifiée")
chirac's avatar
chirac committed
534 535 536 537
        return redirect(reverse(
            'users:profil',
            kwargs={'userid': str(mailalias_instance.user.id)}
        ))
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
    return form(
        {'userform': mailalias, 'action_name': 'Editer un alias mail'},
        'users/user.html',
        request
    )

@login_required
@can_delete(MailAlias)
def del_mailalias(request, mailalias, **_kwargs):
        """ Supprime un alias mail"""
        if request.method == "POST":
            mailalias.delete()
            messages.success(request, "L'alias a été supprimé")
            return redirect(reverse(
                'users:profil',
chirac's avatar
chirac committed
553
                kwargs={'userid': str(mailalias.user.id)}
554 555 556 557 558 559 560
                ))
        return form(
            {'objet': mailalias, 'objet_name': 'mailalias'},
            'users/delete.html',
            request
        )

561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
@login_required
@can_edit(Mail)
def edit_mail(request, mail_instance, **_kwargs):
    """ Editer un compte mail"""
    mail = MailForm(
        request.POST or None,
        instance=mail_instance
    )
    if mail.is_valid():
        if mail.changed_data:
            mail.save()
            messages.success(request, "Compte mail modifiée")
        return redirect(reverse('users:index'))
    return form(
        {'userform': mail, 'action_name': 'Editer un compte mail'},
        'users/user.html',
        request
    )

chirac's avatar
chirac committed
580
@login_required
581
@can_create(School)
chirac's avatar
chirac committed
582
def add_school(request):
583 584
    """ Ajouter un établissement d'enseignement à la base de donnée,
    need cableur"""
chirac's avatar
chirac committed
585 586
    school = SchoolForm(request.POST or None)
    if school.is_valid():
587
        school.save()
chirac's avatar
chirac committed
588
        messages.success(request, "L'établissement a été ajouté")
589
        return redirect(reverse('users:index-school'))
Maël Kervella's avatar
Maël Kervella committed
590 591 592 593 594
    return form(
        {'userform': school, 'action_name': 'Ajouter'},
        'users/user.html',
        request
    )
chirac's avatar
chirac committed
595

596

chirac's avatar
chirac committed
597
@login_required
598
@can_edit(School)
599
def edit_school(request, school_instance, **_kwargs):
600 601
    """ Editer un établissement d'enseignement à partir du schoolid dans
    la base de donnée, need cableur"""
602 603
    school = SchoolForm(request.POST or None, instance=school_instance)
    if school.is_valid():
604 605 606
        if school.changed_data:
            school.save()
            messages.success(request, "Établissement modifié")
607
        return redirect(reverse('users:index-school'))
Maël Kervella's avatar
Maël Kervella committed
608 609 610 611 612
    return form(
        {'userform': school, 'action_name': 'Editer'},
        'users/user.html',
        request
    )
613

614

chirac's avatar
chirac committed
615
@login_required
616
@can_delete_set(School)
617
def del_school(request, instances):
618 619 620 621
    """ Supprimer un établissement d'enseignement à la base de donnée,
    need cableur
    Objet protégé, possible seulement si aucun user n'est affecté à
    l'établissement """
622
    school = DelSchoolForm(request.POST or None, instances=instances)
chirac's avatar
chirac committed
623 624 625 626
    if school.is_valid():
        school_dels = school.cleaned_data['schools']
        for school_del in school_dels:
            try:
627
                school_del.delete()
chirac's avatar
chirac committed
628 629
                messages.success(request, "L'établissement a été supprimé")
            except ProtectedError:
630 631 632
                messages.error(
                    request,
                    "L'établissement %s est affecté à au moins un user, \
lhark's avatar
lhark committed
633
                        vous ne pouvez pas le supprimer" % school_del)
634
        return redirect(reverse('users:index-school'))
Maël Kervella's avatar
Maël Kervella committed
635 636 637 638 639
    return form(
        {'userform': school, 'action_name': 'Supprimer'},
        'users/user.html',
        request
    )
chirac's avatar
chirac committed
640

641

642 643 644
@login_required
@can_create(ListShell)
def add_shell(request):
Gabriel Detraz's avatar
Gabriel Detraz committed
645
    """ Ajouter un shell à la base de donnée"""
646 647
    shell = ShellForm(request.POST or None)
    if shell.is_valid():
648
        shell.save()
649 650
        messages.success(request, "Le shell a été ajouté")
        return redirect(reverse('users:index-shell'))
Maël Kervella's avatar
Maël Kervella committed
651 652 653 654 655
    return form(
        {'userform': shell, 'action_name': 'Ajouter'},
        'users/user.html',
        request
    )
656 657 658 659


@login_required
@can_edit(ListShell)
660
def edit_shell(request, shell_instance, **_kwargs):
Gabriel Detraz's avatar
Gabriel Detraz committed
661
    """ Editer un shell à partir du listshellid"""
662 663
    shell = ShellForm(request.POST or None, instance=shell_instance)
    if shell.is_valid():
664 665 666
        if shell.changed_data:
            shell.save()
            messages.success(request, "Le shell a été modifié")
667
        return redirect(reverse('users:index-shell'))
Maël Kervella's avatar
Maël Kervella committed
668 669 670 671 672
    return form(
        {'userform': shell, 'action_name': 'Editer'},
        'users/user.html',
        request
    )
673 674 675 676


@login_required
@can_delete(ListShell)
677
def del_shell(request, shell, **_kwargs):
678 679
    """Destruction d'un shell"""
    if request.method == "POST":
680
        shell.delete()
681 682 683 684 685 686 687 688 689
        messages.success(request, "Le shell a été détruit")
        return redirect(reverse('users:index-shell'))
    return form(
        {'objet': shell, 'objet_name': 'shell'},
        'users/delete.html',
        request
    )


690
@login_required
691
@can_create(ListRight)
692
def add_listright(request):
chirac's avatar
chirac committed
693 694
    """ Ajouter un droit/groupe, nécessite droit bureau.
    Obligation de fournir un gid pour la synchro ldap, unique """
695 696
    listright = NewListRightForm(request.POST or None)
    if listright.is_valid():
697
        listright.save()
698
        messages.success(request, "Le droit/groupe a été ajouté")
699
        return redirect(reverse('users:index-listright'))
Maël Kervella's avatar
Maël Kervella committed
700 701 702 703 704
    return form(
        {'userform': listright, 'action_name': 'Ajouter'},
        'users/user.html',
        request
    )
705

706

707
@login_required
708
@can_edit(ListRight)
709
def edit_listright(request, listright_instance, **_kwargs):
710 711 712 713 714 715
    """ Editer un groupe/droit, necessite droit bureau,
    à partir du listright id """
    listright = ListRightForm(
        request.POST or None,
        instance=listright_instance
    )
716
    if listright.is_valid():
717 718 719
        if listright.changed_data:
            listright.save()
            messages.success(request, "Droit modifié")
720
        return redirect(reverse('users:index-listright'))
Maël Kervella's avatar
Maël Kervella committed
721 722 723 724 725
    return form(
        {'userform': listright, 'action_name': 'Editer'},
        'users/user.html',
        request
    )
726

727

728
@login_required
729
@can_delete_set(ListRight)
730
def del_listright(request, instances):
731 732
    """ Supprimer un ou plusieurs groupe, possible si il est vide, need droit
    bureau """
733
    listright = DelListRightForm(request.POST or None, instances=instances)
734 735 736 737
    if listright.is_valid():
        listright_dels = listright.cleaned_data['listrights']
        for listright_del in listright_dels:
            try:
738
                listright_del.delete()
739 740 741 742
                messages.success(request, "Le droit/groupe a été supprimé")
            except ProtectedError:
                messages.error(
                    request,
743
                    "Le groupe %s est affecté à au moins un user, \
744
                        vous ne pouvez pas le supprimer" % listright_del)
745
        return redirect(reverse('users:index-listright'))
Maël Kervella's avatar
Maël Kervella committed
746 747 748 749 750
    return form(
        {'userform': listright, 'action_name': 'Supprimer'},
        'users/user.html',
        request
    )
751

752

753
@login_required
754 755
@can_view_all(User)
@can_change(User, 'state')
756 757 758 759 760 761
def mass_archive(request):
    """ Permet l'archivage massif"""
    to_archive_date = MassArchiveForm(request.POST or None)
    to_archive_list = []
    if to_archive_date.is_valid():
        date = to_archive_date.cleaned_data['date']
762 763 764 765
        to_archive_list = [user for user in
                           User.objects.exclude(state=User.STATE_ARCHIVE)
                           if not user.end_access()
                           or user.end_access() < date]
766 767 768
        if "valider" in request.POST:
            for user in to_archive_list:
                with transaction.atomic(), reversion.create_revision():
769
                    user.archive()
770 771
                    user.save()
                    reversion.set_comment("Archivage")
772 773 774
            messages.success(request, "%s users ont été archivés" % len(
                to_archive_list
            ))
775
            return redirect(reverse('users:index'))
776 777 778 779 780 781
    return form(
        {'userform': to_archive_date, 'to_archive_list': to_archive_list},
        'users/mass_archive.html',
        request
    )

782

chirac's avatar
chirac committed
783
@login_required
784
@can_view_all(Adherent)
chirac's avatar
chirac committed
785
def index(request):
786
    """ Affiche l'ensemble des adherents, need droit cableur """
787
    pagination_number = GeneralOption.get_cached_value('pagination_number')
788
    users_list = Adherent.objects.select_related('room')
789 790 791 792 793 794
    users_list = SortTable.sort(
        users_list,
        request.GET.get('col'),
        request.GET.get('order'),
        SortTable.USERS_INDEX
    )
795
    users_list = re2o_paginator(request, users_list, pagination_number)
796
    return render(request, 'users/index.html', {'users_list': users_list})
root's avatar
root committed
797

798

799
@login_required
800
@can_view_all(Club)
801 802
def index_clubs(request):
    """ Affiche l'ensemble des clubs, need droit cableur """
803
    pagination_number = GeneralOption.get_cached_value('pagination_number')
804
    clubs_list = Club.objects.select_related('room')
805 806 807 808 809 810
    clubs_list = SortTable.sort(
        clubs_list,
        request.GET.get('col'),
        request.GET.get('order'),
        SortTable.USERS_INDEX
    )
811
    clubs_list = re2o_paginator(request, clubs_list, pagination_number)
Maël Kervella's avatar
Maël Kervella committed
812 813 814 815 816
    return render(
        request,
        'users/index_clubs.html',
        {'clubs_list': clubs_list}
    )
817 818


chirac's avatar
chirac committed
819
@login_required
820
@can_view_all(Ban)
821
def index_ban(request):
chirac's avatar
chirac committed
822
    """ Affiche l'ensemble des ban, need droit cableur """
823
    pagination_number = GeneralOption.get_cached_value('pagination_number')
824 825 826 827 828 829 830
    ban_list = Ban.objects.select_related('user')
    ban_list = SortTable.sort(
        ban_list,
        request.GET.get('col'),
        request.GET.get('order'),
        SortTable.USERS_INDEX_BAN
    )
831
    ban_list = re2o_paginator(request, ban_list, pagination_number)
832
    return render(request, 'users/index_ban.html', {'ban_list': ban_list})
833

834

chirac's avatar
chirac committed
835
@login_required
836
@can_view_all(Whitelist)
837
def index_white(request):
chirac's avatar
chirac committed
838
    """ Affiche l'ensemble des whitelist, need droit cableur """
839
    pagination_number = GeneralOption.get_cached_value('pagination_number')
840 841 842 843 844 845 846
    white_list = Whitelist.objects.select_related('user')
    white_list = SortTable.sort(
        white_list,
        request.GET.get('col'),
        request.GET.get('order'),
        SortTable.USERS_INDEX_BAN
    )
847
    white_list = re2o_paginator(request, white_list, pagination_number)
848 849 850 851 852 853
    return render(
        request,
        'users/index_whitelist.html',
        {'white_list': white_list}
    )

854

chirac's avatar
chirac committed
855
@login_required
856
@can_view_all(School)
857
def index_school(request):
858
    """ Affiche l'ensemble des établissement"""
859
    school_list = School.objects.order_by('name')
860 861 862 863 864 865 866
    pagination_number = GeneralOption.get_cached_value('pagination_number')
    school_list = SortTable.sort(
        school_list,
        request.GET.get('col'),
        request.GET.get('order'),
        SortTable.USERS_INDEX_SCHOOL
    )
867
    school_list = re2o_paginator(request, school_list, pagination_number)
868 869 870 871 872 873
    return render(
        request,
        'users/index_schools.html',
        {'school_list': school_list}
    )

874

875 876 877 878 879 880 881 882 883 884 885 886
@login_required
@can_view_all(ListShell)
def index_shell(request):
    """ Affiche l'ensemble des shells"""
    shell_list = ListShell.objects.order_by('shell')
    return render(
        request,
        'users/index_shell.html',
        {'shell_list': shell_list}
    )


887
@login_required
888