2018-09-01 19:57:09 +00:00
from django . shortcuts import render , get_object_or_404 , redirect
from django . urls import reverse
from django . contrib . auth . models import User , Group , Permission
from django . contrib . auth import authenticate , login , logout
from django . contrib import messages
2018-10-05 22:03:02 +00:00
from django . db . models import Q
from django . http import HttpResponse , HttpResponseRedirect
2018-11-22 21:52:15 +00:00
from django . core . paginator import EmptyPage , PageNotAnInteger , Paginator
from django . contrib . auth . decorators import login_required , permission_required
2018-08-31 12:46:35 +00:00
2018-11-22 21:52:15 +00:00
import simplejson as json
2018-10-05 22:03:02 +00:00
from datetime import datetime , timedelta
from dal import autocomplete
2018-11-22 21:52:15 +00:00
from coopeV3 . acl import admin_required , superuser_required , self_or_has_perm , active_required
2018-10-05 22:03:02 +00:00
from . models import CotisationHistory , WhiteListHistory , School
from . forms import CreateUserForm , LoginForm , CreateGroupForm , EditGroupForm , SelectUserForm , GroupsEditForm , EditPasswordForm , addCotisationHistoryForm , addCotisationHistoryForm , addWhiteListHistoryForm , SelectNonAdminUserForm , SelectNonSuperUserForm , SchoolForm
2018-11-22 21:52:15 +00:00
from gestion . models import Reload
2018-09-01 19:57:09 +00:00
2018-11-22 21:52:15 +00:00
@active_required
2018-09-01 19:57:09 +00:00
def loginView ( request ) :
2018-11-22 21:52:15 +00:00
"""
Display the login form for : model : ` User ` .
* * Context * *
` ` form_entete ` `
Title of the form .
` ` form ` `
The login form .
` ` form_button ` `
Content of the form button .
* * Template * *
: template : ` form . html `
"""
2018-09-01 19:57:09 +00:00
form = LoginForm ( request . POST or None )
if ( form . is_valid ( ) ) :
user = authenticate ( username = form . cleaned_data [ ' username ' ] , password = form . cleaned_data [ ' password ' ] )
if user is not None :
login ( request , user )
messages . success ( request , " Vous êtes à présent connecté sous le compte " + str ( user ) )
if ( request . user . has_perm ( ' gestion.can_manage ' ) ) :
return redirect ( reverse ( ' gestion:manage ' ) )
else :
return redirect ( reverse ( ' users:profile ' , kwargs = { ' pk ' : request . user . pk } ) )
else :
messages . error ( request , " Nom d ' utilisateur et/ou mot de passe invalide " )
return render ( request , " form.html " , { " form_entete " : " Connexion " , " form " : form , " form_title " : " Connexion " , " form_button " : " Se connecter " } )
2018-11-22 21:52:15 +00:00
@active_required
@login_required
2018-09-01 19:57:09 +00:00
def logoutView ( request ) :
2018-11-22 21:52:15 +00:00
"""
Logout the logged user
"""
2018-09-01 19:57:09 +00:00
logout ( request )
messages . success ( request , " Vous êtes à présent déconnecté " )
return redirect ( reverse ( ' home ' ) )
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required ( ' auth.view_user ' )
2018-09-01 19:57:09 +00:00
def index ( request ) :
2018-11-22 21:52:15 +00:00
"""
Display the index for user related actions
* * Template * *
2018-09-01 19:57:09 +00:00
2018-11-22 21:52:15 +00:00
: template : ` users / index . html `
"""
return render ( request , " users/index.html " )
2018-10-05 22:03:02 +00:00
########## users ##########
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@self_or_has_perm ( ' pk ' , ' auth.view_user ' )
2018-09-01 19:57:09 +00:00
def profile ( request , pk ) :
2018-11-22 21:52:15 +00:00
"""
Display the profile for the requested user
` ` pk ` `
The primary key for user
* * Context * *
` ` user ` `
The instance of User
` ` self ` `
Boolean value wich indicates if the current logged user and the request user are the same
` ` cotisations ` `
List of the user ' s cotisations
` ` whitelists ` `
List of the user ' s whitelists
` ` reloads ` `
List of the last 5 reloads of the user
* * Template * *
: template : ` users / profile . html `
"""
2018-09-01 19:57:09 +00:00
user = get_object_or_404 ( User , pk = pk )
self = request . user == user
2018-10-05 22:03:02 +00:00
cotisations = CotisationHistory . objects . filter ( user = user )
whitelists = WhiteListHistory . objects . filter ( user = user )
2018-11-22 21:52:15 +00:00
reloads = Reload . objects . filter ( customer = user ) . order_by ( ' -date ' )
return render ( request , " users/profile.html " , { " user " : user , " self " : self , " cotisations " : cotisations , " whitelists " : whitelists , " reloads " : reloads } )
2018-09-01 19:57:09 +00:00
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required ( ' auth.add_user ' )
2018-09-01 19:57:09 +00:00
def createUser ( request ) :
2018-11-22 21:52:15 +00:00
"""
Display a CreateUserForm instance .
* * Context * *
` ` form_entete ` `
The form title .
` ` form ` `
The CreateUserForm instance .
` ` form_button ` `
The content of the form button .
* * Template * *
: template : ` form . html `
"""
2018-09-01 19:57:09 +00:00
form = CreateUserForm ( request . POST or None )
if ( form . is_valid ( ) ) :
user = form . save ( commit = False )
user . set_password ( user . username )
user . save ( )
user . profile . school = form . cleaned_data [ ' school ' ]
user . save ( )
return render ( request , " form.html " , { " form_entete " : " Gestion des utilisateurs " , " form " : form , " form_title " : " Création d ' un nouvel utilisateur " , " form_button " : " Créer l ' utilisateur " } )
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required ( ' auth.view_user ' )
2018-10-05 22:03:02 +00:00
def searchUser ( request ) :
2018-11-22 21:52:15 +00:00
"""
Display a simple searchForm for User .
* * Context * *
` ` form_entete ` `
The form title .
` ` form ` `
The searchForm instance .
` ` form_button ` `
The content of the form button .
* * Template * *
: template : ` form . html `
"""
2018-10-05 22:03:02 +00:00
form = SelectUserForm ( request . POST or None )
if ( form . is_valid ( ) ) :
return redirect ( reverse ( ' users:profile ' , kwargs = { " pk " : form . cleaned_data [ ' user ' ] . pk } ) )
return render ( request , " form.html " , { " form_entete " : " Gestion des utilisateurs " , " form " : form , " form_title " : " Rechercher un utilisateur " , " form_button " : " Afficher le profil " } )
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required ( ' auth.view_user ' )
2018-10-05 22:03:02 +00:00
def usersIndex ( request ) :
2018-11-22 21:52:15 +00:00
"""
Display the list of all users .
* * Context * *
` ` users ` `
The list of all users
* * Template * *
: template : ` users / users_index . html `
"""
2018-10-05 22:03:02 +00:00
users = User . objects . all ( )
return render ( request , " users/users_index.html " , { " users " : users } )
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required ( ' auth.change_user ' )
2018-10-05 22:03:02 +00:00
def editGroups ( request , pk ) :
2018-11-22 21:52:15 +00:00
"""
Edit the groups of a user .
` ` pk ` `
The pk of the user .
* * Context * *
` ` form_entete ` `
The form title .
` ` form ` `
The GroupsEditForm instance .
` ` form_button ` `
The content of the form button .
* * Template * *
: template : ` form . html `
"""
2018-10-05 22:03:02 +00:00
user = get_object_or_404 ( User , pk = pk )
form = GroupsEditForm ( request . POST or None , instance = user )
if ( form . is_valid ( ) ) :
form . save ( )
messages . success ( request , " Les groupes de l ' utilisateur " + user . username + " ont bien été enregistrés. " )
return redirect ( reverse ( ' users:profile ' , kwargs = { ' pk ' : pk } ) )
extra_css = " #id_groups { height:200px;} "
return render ( request , " form.html " , { " form_entete " : " Gestion de l ' utilisateur " + user . username , " form " : form , " form_title " : " Modification des groupes " , " form_button " : " Enregistrer " , " extra_css " : extra_css } )
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required ( ' auth.change_user ' )
2018-10-05 22:03:02 +00:00
def editPassword ( request , pk ) :
2018-11-22 21:52:15 +00:00
"""
Change the password of a user .
` ` pk ` `
The pk of the user .
* * Context * *
` ` form_entete ` `
The form title .
` ` form ` `
The EditPasswordForm instance .
` ` form_button ` `
The content of the form button .
* * Template * *
: template : ` form . html `
"""
2018-10-05 22:03:02 +00:00
user = get_object_or_404 ( User , pk = pk )
if user != request . user :
messages . error ( request , " Vous ne pouvez modifier le mot de passe d ' un autre utilisateur " )
return redirect ( reverse ( ' home ' ) )
else :
form = EditPasswordForm ( request . POST or None )
if ( form . is_valid ( ) ) :
if authenticate ( username = user . username , password = form . cleaned_data [ ' password ' ] ) is not None :
user . set_password ( form . cleaned_data [ ' password2 ' ] )
user . save ( )
messages . success ( request , " Votre mot de passe a bien été mis à jour " )
return redirect ( reverse ( ' users:profile ' , kwargs = { ' pk ' : pk } ) )
else :
messages . error ( request , " Le mot de passe actuel est incorrect " )
return render ( request , " form.html " , { " form_entete " : " Modification de mon compte " , " form " : form , " form_title " : " Modification de mon mot de passe " , " form_button " : " Modifier mon mot de passe " } )
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required ( ' auth.change_user ' )
2018-10-05 22:03:02 +00:00
def editUser ( request , pk ) :
2018-11-22 21:52:15 +00:00
"""
Edit a user and user profile
` ` pk ` `
The pk of the user .
* * Context * *
` ` form_entete ` `
The form title .
` ` form ` `
The CreateUserForm instance .
` ` form_button ` `
The content of the form button .
* * Template * *
: template : ` form . html `
"""
2018-10-05 22:03:02 +00:00
user = get_object_or_404 ( User , pk = pk )
form = CreateUserForm ( request . POST or None , instance = user , initial = { ' school ' : user . profile . school } )
if ( form . is_valid ( ) ) :
user . profile . school = form . cleaned_data [ ' school ' ]
user . save ( )
messages . success ( request , " Les modifications ont bien été enregistrées " )
return redirect ( reverse ( ' users:profile ' , kwargs = { ' pk ' : pk } ) )
return render ( request , " form.html " , { " form_entete " : " Modification du compte " + user . username , " form " : form , " form_title " : " Modification des informations " , " form_button " : " Modifier " } )
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required ( ' auth.change_user ' )
2018-10-05 22:03:02 +00:00
def resetPassword ( request , pk ) :
2018-11-22 21:52:15 +00:00
"""
Reset the password of a user .
` ` pk ` `
The pk of the user
"""
2018-10-05 22:03:02 +00:00
user = get_object_or_404 ( User , pk = pk )
if user . is_superuser :
messages . error ( request , " Impossible de réinitialiser le mot de passe de " + user . username + " : il est superuser. " )
return redirect ( reverse ( ' users:profile ' , kwargs = { ' pk ' : pk } ) )
else :
user . set_password ( user . username )
user . save ( )
messages . success ( request , " Le mot de passe de " + user . username + " a bien été réinitialisé. " )
return redirect ( reverse ( ' users:profile ' , kwargs = { ' pk ' : pk } ) )
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required ( ' auth.view_user ' )
2018-10-05 22:03:02 +00:00
def getUser ( request , pk ) :
2018-11-22 21:52:15 +00:00
"""
Return username and balance of the requested user ( pk )
` ` pk ` `
The pk of the user
"""
2018-10-05 22:03:02 +00:00
user = get_object_or_404 ( User , pk = pk )
2018-11-22 21:52:15 +00:00
data = json . dumps ( { " username " : user . username , " balance " : user . profile . balance } )
2018-10-05 22:03:02 +00:00
return HttpResponse ( data , content_type = ' application/json ' )
2018-09-01 19:57:09 +00:00
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@self_or_has_perm ( ' pk ' , ' auth.view_user ' )
def allReloads ( request , pk , page ) :
"""
Display all the reloads of the requested user .
` ` pk ` `
The pk of the user .
` ` page ` `
The page number .
* * Context * *
` ` reloads ` `
The reloads of the page .
` ` user ` `
The requested user
* * Template * *
: template : ` users / allReloads . html `
"""
user = get_object_or_404 ( User , pk = pk )
allReloads = Reload . objects . filter ( customer = user ) . order_by ( ' -date ' )
paginator = Paginator ( allReloads , 2 )
reloads = paginator . get_page ( page )
return render ( request , " users/allReloads.html " , { " reloads " : reloads , " user " : user } )
2018-09-01 19:57:09 +00:00
########## Groups ##########
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required ( ' auth.view_group ' )
2018-09-01 19:57:09 +00:00
def groupsIndex ( request ) :
2018-11-22 21:52:15 +00:00
"""
Display all the groups .
* * Context * *
` ` groups ` `
List of all groups .
* * Template * *
: template : ` users / groups_index . html `
"""
2018-09-01 19:57:09 +00:00
groups = Group . objects . all ( )
return render ( request , " users/groups_index.html " , { " groups " : groups } )
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required ( ' auth.view_group ' )
2018-09-01 19:57:09 +00:00
def groupProfile ( request , pk ) :
2018-11-22 21:52:15 +00:00
"""
Display the profile of a group .
` ` pk ` `
The pk of the group .
* * Context * *
` ` group ` `
The requested group .
* * Template * *
: template : ` users / group_profile . html `
"""
2018-09-01 19:57:09 +00:00
group = get_object_or_404 ( Group , pk = pk )
return render ( request , " users/group_profile.html " , { " group " : group } )
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required ( ' auth.add_group ' )
2018-09-01 19:57:09 +00:00
def createGroup ( request ) :
2018-11-22 21:52:15 +00:00
"""
Create a group with a CreateGroupForm instance .
* * Context * *
` ` form_entete ` `
The form title .
` ` form ` `
The CreateGroupForm instance .
` ` form_button ` `
The content of the form button .
* * Template * *
: template : ` form . html `
"""
2018-09-01 19:57:09 +00:00
form = CreateGroupForm ( request . POST or None )
if ( form . is_valid ( ) ) :
group = form . save ( )
messages . success ( request , " Le groupe " + form . cleaned_data [ ' name ' ] + " a bien été crée. " )
return redirect ( reverse ( ' users:groupProfile ' , kwargs = { ' pk ' : group . pk } ) )
return render ( request , " form.html " , { " form_entete " : " Gestion des utilisateurs " , " form " : form , " form_title " : " Création d ' un groupe de droit " , " form_button " : " Créer le groupe de droit " } )
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required ( ' auth.change_group ' )
2018-09-01 19:57:09 +00:00
def editGroup ( request , pk ) :
2018-11-22 21:52:15 +00:00
"""
Edit a group with a EditGroupForm instance .
` ` pk ` `
The pk of the group .
* * Context * *
` ` form_entete ` `
The form title .
` ` form ` `
The EditGroupForm instance .
` ` form_button ` `
The content of the form button .
* * Template * *
: template : ` form . html `
"""
2018-09-01 19:57:09 +00:00
group = get_object_or_404 ( Group , pk = pk )
form = EditGroupForm ( request . POST or None , instance = group )
extra_css = " #id_permissions { height:200px;} "
if ( form . is_valid ( ) ) :
form . save ( )
messages . success ( request , " Le groupe " + group . name + " a bien été modifié. " )
return redirect ( reverse ( ' users:groupProfile ' , kwargs = { ' pk ' : group . pk } ) )
return render ( request , " form.html " , { " form_entete " : " Gestion des utilisateurs " , " form " : form , " form_title " : " Modification du groupe de droit " + group . name , " form_button " : " Modifier le groupe de droit " , " extra_css " : extra_css } )
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required ( ' auth.delete_group ' )
2018-09-01 19:57:09 +00:00
def deleteGroup ( request , pk ) :
2018-11-22 21:52:15 +00:00
"""
Delete the requested group .
` ` pk ` `
The pk of the group
"""
2018-09-01 19:57:09 +00:00
group = get_object_or_404 ( Group , pk = pk )
if group . user_set . count ( ) == 0 :
name = group . name
group . delete ( )
messages . success ( request , " Le groupe " + name + " a bien été supprimé " )
return redirect ( reverse ( ' users:index ' ) + ' #second ' )
else :
messages . error ( request , " Impossible de supprimer le groupe " + group . name + " : il y a encore des utilisateurs " )
return redirect ( reverse ( ' users:groupProfile ' , kwargs = { ' pk ' : group . pk } ) )
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required ( ' auth.change_group ' )
2018-09-01 19:57:09 +00:00
def removeRight ( request , groupPk , permissionPk ) :
2018-11-22 21:52:15 +00:00
"""
Remove a right from a given group .
` ` groupPk ` `
The pk of the group .
` ` permissionPk ` `
The pk of the right .
"""
2018-09-01 19:57:09 +00:00
group = get_object_or_404 ( Group , pk = groupPk )
perm = get_object_or_404 ( Permission , pk = permissionPk )
if perm in group . permissions . all ( ) :
group . permissions . remove ( perm )
messages . success ( request , " La permission " + perm . codename + " a bien été retirée du groupe " + group . name )
else :
messages . error ( request , " Impossible de retirer la permission " + perm . codename + " du groupe " + group . name )
return redirect ( reverse ( ' users:groupProfile ' , kwargs = { ' pk ' : groupPk } ) + " #second " )
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required ( ' auth.change_user ' )
2018-09-01 19:57:09 +00:00
def removeUser ( request , groupPk , userPk ) :
2018-11-22 21:52:15 +00:00
"""
Remove a user from a given group .
` ` groupPk ` `
The pk of the group .
` ` userPk ` `
The pk of the user .
"""
2018-09-01 19:57:09 +00:00
group = get_object_or_404 ( Group , pk = groupPk )
user = get_object_or_404 ( User , pk = userPk )
if ( group in user . groups . all ( ) ) :
user . groups . remove ( group )
messages . success ( request , " L ' utilisateur " + user . username + " a bien été retiré du groupe " + group . name )
else :
messages . error ( request , " Impossible de retirer l ' utilisateur " + user . username + " du groupe " + group . name )
return redirect ( reverse ( ' users:groupProfile ' , kwargs = { ' pk ' : groupPk } ) + " #second " )
########## admins ##########
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@admin_required
2018-09-01 19:57:09 +00:00
def adminsIndex ( request ) :
admins = User . objects . filter ( is_staff = True )
return render ( request , " users/admins_index.html " , { " admins " : admins } )
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@admin_required
2018-09-01 19:57:09 +00:00
def addAdmin ( request ) :
2018-10-05 22:03:02 +00:00
form = SelectNonAdminUserForm ( request . POST or None )
2018-09-01 19:57:09 +00:00
if ( form . is_valid ( ) ) :
user = form . cleaned_data [ ' user ' ]
user . is_staff = True
user . save ( )
messages . success ( request , " L ' utilisateur " + user . username + " a bien été rajouté aux admins " )
return redirect ( reverse ( ' users:adminsIndex ' ) )
return render ( request , " form.html " , { " form_entete " : " Gestion des admins " , " form " : form , " form_title " : " Ajout d ' un admin " , " form_button " : " Ajouter l ' utilisateur aux admins " } )
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@admin_required
2018-09-01 19:57:09 +00:00
def removeAdmin ( request , pk ) :
user = get_object_or_404 ( User , pk = pk )
if user . is_staff :
if user . is_superuser :
messages . error ( request , " Impossible de retirer l ' utilisateur " + user . username + " des admins : il est superuser " )
else :
if User . objects . filter ( is_staff = True ) . count ( ) > 1 :
user . is_staff = False
user . save ( )
messages . success ( request , " L ' utilisateur " + user . username + " a bien été retiré des admins. " )
else :
messages . error ( request , " Impossible de retirer l ' utilisateur " + user . username + " des admins : il doit en rester au moins un. " )
else :
messages . error ( request , " Impossible de retirer l ' utilisateur " + user . username + " des admins : il n ' en fait pas partie. " )
return redirect ( reverse ( ' users:adminsIndex ' ) )
########## superusers ##########
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@superuser_required
2018-09-01 19:57:09 +00:00
def superusersIndex ( request ) :
superusers = User . objects . filter ( is_superuser = True )
return render ( request , " users/superusers_index.html " , { " superusers " : superusers } )
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@superuser_required
2018-09-01 19:57:09 +00:00
def addSuperuser ( request ) :
2018-10-05 22:03:02 +00:00
form = SelectNonSuperUserForm ( request . POST or None )
2018-09-01 19:57:09 +00:00
if ( form . is_valid ( ) ) :
user = form . cleaned_data [ ' user ' ]
user . is_admin = True
user . is_superuser = True
user . save ( )
messages . success ( request , " L ' utilisateur " + user . username + " a bien été rajouté aux superusers " )
return redirect ( reverse ( ' users:superusersIndex ' ) )
return render ( request , " form.html " , { " form_entete " : " Gestion des superusers " , " form " : form , " form_title " : " Ajout d ' un superuser " , " form_button " : " Ajouter l ' utilisateur aux superusers " } )
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@superuser_required
2018-09-01 19:57:09 +00:00
def removeSuperuser ( request , pk ) :
user = get_object_or_404 ( User , pk = pk )
if user . is_superuser :
if User . objects . filter ( is_superuser = True ) . count ( ) > 1 :
user . is_superuser = False
user . save ( )
messages . success ( request , " L ' utilisateur " + user . username + " a bien été retiré des superusers. " )
else :
messages . error ( request , " Impossible de retirer l ' utilisateur " + user . username + " des superusers : il doit en rester au moins un. " )
else :
messages . error ( request , " Impossible de retirer l ' utilisateur " + user . username + " des superusers : il n ' en fait pas partie. " )
return redirect ( reverse ( ' users:superusersIndex ' ) )
2018-10-05 22:03:02 +00:00
########## Cotisations ##########
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required ( ' users.add_cotisationhistory ' )
2018-10-05 22:03:02 +00:00
def addCotisationHistory ( request , pk ) :
user = get_object_or_404 ( User , pk = pk )
form = addCotisationHistoryForm ( request . POST or None )
if ( form . is_valid ( ) ) :
cotisation = form . save ( commit = False )
2018-11-22 21:52:15 +00:00
if ( cotisation . paymentMethod . affect_balance ) :
2018-11-25 12:52:32 +00:00
if ( user . profile . balance > = cotisation . cotisation . amount ) :
user . profile . debit + = cotisation . cotisation . amount
2018-11-22 21:52:15 +00:00
else :
cotisation . delete ( )
messages . error ( request , " Solde insuffisant " )
2018-10-05 22:03:02 +00:00
cotisation . user = user
cotisation . coopeman = request . user
cotisation . amount = cotisation . cotisation . amount
cotisation . duration = cotisation . cotisation . duration
if ( user . profile . cotisationEnd ) :
cotisation . endDate = user . profile . cotisationEnd + timedelta ( days = cotisation . cotisation . duration )
else :
cotisation . endDate = datetime . now ( ) + timedelta ( days = cotisation . cotisation . duration )
user . profile . cotisationEnd = cotisation . endDate
user . save ( )
cotisation . save ( )
messages . success ( request , " La cotisation a bien été ajoutée " )
return redirect ( reverse ( ' users:profile ' , kwargs = { ' pk ' : user . pk } ) )
return render ( request , " form.html " , { " form " : form , " form_title " : " Ajout d ' une cotisation pour l ' utilisateur " + str ( user ) , " form_button " : " Ajouter " } )
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required ( ' users.validate_consumptionhistory ' )
2018-10-05 22:03:02 +00:00
def validateCotisationHistory ( request , pk ) :
cotisationHistory = get_object_or_404 ( CotisationHistory , pk = pk )
cotisationHistory . valid = CotisationHistory . VALID
cotisationHistory . save ( )
messages . success ( request , " La cotisation a bien été validée " )
return HttpResponseRedirect ( request . META . get ( ' HTTP_REFERER ' ) )
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required ( ' users.validate_consumptionhistory ' )
2018-10-05 22:03:02 +00:00
def invalidateCotisationHistory ( request , pk ) :
cotisationHistory = get_object_or_404 ( CotisationHistory , pk = pk )
cotisationHistory . valid = CotisationHistory . INVALID
cotisationHistory . save ( )
user = cotisationHistory . user
user . profile . cotisationEnd = user . profile . cotisationEnd - timedelta ( days = cotisationHistory . duration )
2018-11-22 21:52:15 +00:00
if ( cotisationHistory . paymentMethod . affect_balance ) :
user . profile . balance + = cotisation . amount
2018-10-05 22:03:02 +00:00
user . save ( )
messages . success ( request , " La cotisation a bien été invalidée " )
return HttpResponseRedirect ( request . META . get ( ' HTTP_REFERER ' ) )
########## Whitelist ##########
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required ( ' users.add_whitelisthistory ' )
2018-10-05 22:03:02 +00:00
def addWhiteListHistory ( request , pk ) :
user = get_object_or_404 ( User , pk = pk )
form = addWhiteListHistoryForm ( request . POST or None )
if ( form . is_valid ( ) ) :
whiteList = form . save ( commit = False )
whiteList . user = user
whiteList . coopeman = request . user
if ( user . profile . cotisationEnd ) :
whiteList . endDate = user . profile . cotisationEnd + timedelta ( days = whiteList . duration )
else :
whiteList = datetime . now ( ) + timedelta ( days = whiteList . duration )
user . profile . cotisationEnd = whiteList . endDate
user . save ( )
whiteList . save ( )
messages . success ( request , " L ' accès gracieux a bien été ajouté " )
return redirect ( reverse ( ' users:profile ' , kwargs = { ' pk ' : user . pk } ) )
return render ( request , " form.html " , { " form " : form , " form_title " : " Ajout d ' un accès gracieux pour " + user . username , " form_button " : " Ajouter " } )
########## Schools ##########
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required ( ' users.view_school ' )
2018-10-05 22:03:02 +00:00
def schoolsIndex ( request ) :
schools = School . objects . all ( )
return render ( request , " users/schools_index.html " , { " schools " : schools } )
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required ( ' users.add_school ' )
2018-10-05 22:03:02 +00:00
def createSchool ( request ) :
form = SchoolForm ( request . POST or None )
if ( form . is_valid ( ) ) :
form . save ( )
messages . success ( request , " L ' école a bien été créée " )
return redirect ( reverse ( ' users:schoolsIndex ' ) )
return render ( request , " form.html " , { " form " : form , " form_title " : " Création d ' une école " , " form_button " : " Créer " } )
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required ( ' users.change_school ' )
2018-10-05 22:03:02 +00:00
def editSchool ( request , pk ) :
school = get_object_or_404 ( School , pk = pk )
form = SchoolForm ( request . POST or None , instance = school )
if ( form . is_valid ( ) ) :
form . save ( )
messages . success ( request , " L ' école a bien été modifiée " )
return redirect ( reverse ( ' users:schoolsIndex ' ) )
return render ( request , " form.html " , { " form " : form , " form_title " : " Modification de l ' école " + str ( school ) , " form_button " : " Modifier " } )
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required ( ' users.delete_school ' )
2018-10-05 22:03:02 +00:00
def deleteSchool ( request , pk ) :
school = get_object_or_404 ( School , pk = pk )
message = " L ' école " + str ( school ) + " a bien été supprimée "
school . delete ( )
messages . success ( request , message )
return redirect ( reverse ( ' users:schoolsIndex ' ) )
########## Autocomplete searchs ##########
class AllUsersAutocomplete ( autocomplete . Select2QuerySetView ) :
def get_queryset ( self ) :
qs = User . objects . all ( )
if self . q :
qs = qs . filter ( Q ( username__istartswith = self . q ) | Q ( first_name__istartswith = self . q ) | Q ( last_name__istartswith = self . q ) )
return qs
class ActiveUsersAutocomplete ( autocomplete . Select2QuerySetView ) :
def get_queryset ( self ) :
qs = User . objects . filter ( is_active = True )
if self . q :
qs = qs . filter ( Q ( username__istartswith = self . q ) | Q ( first_name__istartswith = self . q ) | Q ( last_name__istartswith = self . q ) )
return qs
class AdherentAutocomplete ( autocomplete . Select2QuerySetView ) :
def get_queryset ( self ) :
qs = User . objects . all ( )
return qs
class NonSuperUserAutocomplete ( autocomplete . Select2QuerySetView ) :
def get_queryset ( self ) :
qs = User . objects . filter ( is_superuser = False )
2018-11-22 21:52:15 +00:00
if self . q :
qs = qs . filter ( Q ( username__istartswith = self . q ) | Q ( first_name__istartswith = self . q ) | Q ( last_name__istartswith = self . q ) )
return qs
class NonAdminUserAutocomplete ( autocomplete . Select2QuerySetView ) :
def get_queryset ( self ) :
qs = User . objects . filter ( is_staff = False )
2018-10-05 22:03:02 +00:00
if self . q :
qs = qs . filter ( Q ( username__istartswith = self . q ) | Q ( first_name__istartswith = self . q ) | Q ( last_name__istartswith = self . q ) )
return qs