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
2019-09-23 12:45:52 +00:00
from django . contrib . auth . tokens import default_token_generator
from django . utils . http import urlsafe_base64_encode
2018-09-01 19:57:09 +00:00
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
2019-09-23 12:45:52 +00:00
from django . core . mail import EmailMultiAlternatives
from django . template . loader import get_template
from django . template import Context
2018-11-22 21:52:15 +00:00
from django . contrib . auth . decorators import login_required , permission_required
2018-12-18 10:16:06 +00:00
from django . forms . models import model_to_dict
2019-01-11 07:46:29 +00:00
from django . utils import timezone
2019-02-26 22:18:53 +00:00
from django . conf import settings
2019-09-23 12:45:52 +00:00
from django . contrib . sites . shortcuts import get_current_site
from django . utils . encoding import force_bytes
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-12-18 10:16:06 +00:00
import csv
2019-02-26 22:18:53 +00:00
import os
2018-10-05 22:03:02 +00:00
2019-02-26 22:18:53 +00:00
from django_tex . views import render_to_pdf
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
2018-12-18 10:16:06 +00:00
from . forms import CreateUserForm , LoginForm , CreateGroupForm , EditGroupForm , SelectUserForm , GroupsEditForm , EditPasswordForm , addCotisationHistoryForm , addCotisationHistoryForm , addWhiteListHistoryForm , SelectNonAdminUserForm , SelectNonSuperUserForm , SchoolForm , ExportForm
2018-11-30 18:54:12 +00:00
from gestion . models import Reload , Consumption , ConsumptionHistory , MenuHistory
2019-09-23 12:45:52 +00:00
from preferences . models import GeneralPreferences
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
"""
2019-02-28 12:18:41 +00:00
Displays the : class : ` users . forms . LoginForm ` .
2018-11-22 21:52:15 +00:00
"""
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 ) )
2019-06-09 23:40:08 +00:00
return redirect ( reverse ( ' home ' ) )
2018-09-01 19:57:09 +00:00
else :
messages . error ( request , " Nom d ' utilisateur et/ou mot de passe invalide " )
2019-09-23 11:28:58 +00:00
return render ( request , " users/login.html " , { " form_entete " : " Connexion " , " form " : form , " form_title " : " Connexion " , " form_button " : " Se connecter " , " form_button_icon " : " sign-in-alt " } )
2018-09-01 19:57:09 +00:00
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
"""
2019-02-28 12:18:41 +00:00
Logout the logged user ( : class : ` django . contrib . auth . models . User ` ) .
2018-11-22 21:52:15 +00:00
"""
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
"""
2019-02-28 12:18:41 +00:00
Display the index for user related actions .
2018-11-22 21:52:15 +00:00
"""
2018-12-18 10:16:06 +00:00
export_form = ExportForm ( request . POST or None )
return render ( request , " users/index.html " , { " export_form " : export_form } )
def export_csv ( request ) :
2019-02-28 12:18:41 +00:00
"""
Displays a : class : ` users . forms . ExportForm ` to export csv files of users .
"""
2018-12-18 10:16:06 +00:00
export_form = ExportForm ( request . POST or None )
if export_form . is_valid ( ) :
users = User . objects
qt = export_form . cleaned_data [ ' query_type ' ]
if qt == ' all ' :
2019-01-06 05:02:16 +00:00
filename = " Utilisateurs-coope "
if not export_form . cleaned_data [ ' group ' ] :
users = users . all ( )
2018-12-18 10:16:06 +00:00
elif qt == ' all_active ' :
users = users . filter ( is_active = True )
2019-01-06 05:02:16 +00:00
filename = " Utilisateurs-actifs-coope "
2018-12-18 10:16:06 +00:00
elif qt == ' adherent ' :
pks = [ x . pk for x in User . objects . all ( ) if x . profile . is_adherent ]
users = users . filter ( pk__in = pks )
2019-01-06 05:02:16 +00:00
filename = " Adherents-coope "
2018-12-18 10:16:06 +00:00
elif qt == ' adherent_active ' :
pks = [ x . pk for x in User . objects . filter ( is_active = True ) if x . profile . is_adherent ]
users = users . filter ( pk__in = pks )
2019-01-06 05:02:16 +00:00
filename = " Adherents-actifs-coope "
if export_form . cleaned_data [ ' group ' ] :
group = export_form . cleaned_data [ ' group ' ]
users = users . filter ( groups = group )
filename + = " ( " + group . name + " ) "
2018-12-18 10:16:06 +00:00
response = HttpResponse ( content_type = ' text/csv ' )
response [ ' Content-Disposition ' ] = ' attachment; filename= " ' + filename + ' .csv " '
writer = csv . writer ( response )
fields = export_form . cleaned_data [ ' fields ' ]
top = [ " # " ]
for field in fields :
top . append ( dict ( ExportForm . FIELDS_CHOICES ) [ field ] )
writer . writerow ( top )
for user in users :
row = [ user . pk ]
for field in fields :
2018-12-18 18:01:09 +00:00
r = getattr ( user . profile , field , None )
if r is not None :
row . append ( str ( r ) )
2018-12-18 10:16:06 +00:00
writer . writerow ( row )
return response
else :
return redirect ( reverse ( ' users:index ' ) )
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
"""
2019-02-28 12:18:41 +00:00
Displays the profile for the requested user ( : class : ` django . contrib . auth . models . User ` ) .
2018-11-22 21:52:15 +00:00
2019-02-28 12:18:41 +00:00
pk
The primary key of the user ( : class : ` django . contrib . auth . models . User ` ) to display profile
2018-11-22 21:52:15 +00:00
"""
2018-09-01 19:57:09 +00:00
user = get_object_or_404 ( User , pk = pk )
self = request . user == user
2019-02-26 22:18:53 +00:00
cotisations = CotisationHistory . objects . filter ( user = user ) . order_by ( ' -paymentDate ' )
2018-10-05 22:03:02 +00:00
whitelists = WhiteListHistory . objects . filter ( user = user )
2018-12-16 08:38:03 +00:00
reloads = Reload . objects . filter ( customer = user ) . order_by ( ' -date ' ) [ : 5 ]
2018-11-25 23:15:09 +00:00
consumptionsChart = Consumption . objects . filter ( customer = user )
2019-01-06 03:39:30 +00:00
products_pre = [ ]
quantities_pre = [ ]
2018-11-25 23:15:09 +00:00
for ch in consumptionsChart :
2019-01-06 03:39:30 +00:00
if ch . product in products_pre :
i = products_pre . index ( ch . product )
quantities_pre [ i ] + = int ( ch . quantity / ch . product . showingMultiplier )
2018-11-25 23:15:09 +00:00
else :
2019-01-06 03:39:30 +00:00
products_pre . append ( ch . product )
quantities_pre . append ( int ( ch . quantity / ch . product . showingMultiplier ) )
tot = len ( products_pre )
totQ = sum ( quantities_pre )
products = [ ]
quantities = [ ]
for k in range ( tot ) :
2019-06-19 10:47:55 +00:00
if totQ > 0 and quantities_pre [ k ] / totQ > = 0.01 :
2019-01-06 03:39:30 +00:00
products . append ( products_pre [ k ] )
quantities . append ( quantities_pre [ k ] )
2018-11-25 23:15:09 +00:00
lastConsumptions = ConsumptionHistory . objects . filter ( customer = user ) . order_by ( ' -date ' ) [ : 10 ]
2018-11-30 18:54:12 +00:00
lastMenus = MenuHistory . objects . filter ( customer = user ) . order_by ( ' -date ' ) [ : 10 ]
2018-11-25 23:15:09 +00:00
return render ( request , " users/profile.html " ,
{
" user " : user ,
" self " : self ,
" cotisations " : cotisations ,
" whitelists " : whitelists ,
" reloads " : reloads ,
" products " : products ,
" quantities " : quantities ,
2018-11-30 18:54:12 +00:00
" lastConsumptions " : lastConsumptions ,
" lastMenus " : lastMenus ,
2018-11-25 23:15:09 +00:00
} )
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
"""
2019-02-28 12:18:41 +00:00
Displays a : class : ` ~ users . forms . CreateUserForm ` to create a user ( : class : ` django . contrib . auth . models . User ` ) .
2018-11-22 21:52:15 +00:00
"""
2019-09-25 19:51:33 +00:00
form = CreateUserForm ( request . POST or None , user = User . objects . none ( ) )
2018-09-01 19:57:09 +00:00
if ( form . is_valid ( ) ) :
user = form . save ( commit = False )
user . save ( )
user . profile . school = form . cleaned_data [ ' school ' ]
user . save ( )
2019-09-23 12:45:52 +00:00
uid = urlsafe_base64_encode ( force_bytes ( user . pk ) ) . decode ( ' UTF-8 ' )
print ( uid )
token = default_token_generator . make_token ( user )
plaintext = get_template ( ' users/welcome_email.txt ' )
htmly = get_template ( ' users/welcome_email.html ' )
context = { ' user ' : user , ' uid ' : uid , ' token ' : token , ' protocol ' : " http " , ' domain ' : get_current_site ( request ) . name }
text_content = plaintext . render ( context )
html_content = htmly . render ( context )
email = EmailMultiAlternatives (
" Bienvenue à l ' association Coopé Technopôle Metz " ,
text_content ,
2019-09-23 16:43:29 +00:00
" Coopé Technopôle Metz <no-reply-coope@rezometz.org> " ,
2019-09-23 12:45:52 +00:00
[ user . email ] ,
reply_to = [ " coopemetz@gmail.com " ]
)
email . attach_alternative ( html_content , " text/html " )
gp , _ = GeneralPreferences . objects . get_or_create ( pk = 1 )
if gp . statutes :
2019-09-23 16:43:29 +00:00
email . attach ( " statuts.pdf " , gp . statutes . read ( ) , " application/pdf " )
2019-09-23 12:45:52 +00:00
if gp . rules :
2019-09-23 16:43:29 +00:00
email . attach ( " ri.pdf " , gp . rules . read ( ) , " application/pdf " )
2019-09-23 12:45:52 +00:00
email . send ( )
2018-12-05 00:43:21 +00:00
messages . success ( request , " L ' utilisateur a bien été créé " )
return redirect ( reverse ( ' users:profile ' , kwargs = { ' pk ' : user . pk } ) )
2019-02-21 19:08:29 +00:00
return render ( request , " form.html " , { " form_entete " : " Gestion des utilisateurs " , " form " : form , " form_title " : " Création d ' un nouvel utilisateur " , " form_button " : " Créer mon compte " , " form_button_icon " : " user-plus " , ' extra_html ' : ' <strong>En cliquant sur le bouton " Créer mon compte " , vous :<ul><li>attestez sur l \' honneur que les informations fournies à l \' association Coopé Technopôle Metz sont correctes et que vous n \' avez jamais été enregistré dans l \' association sous un autre nom / pseudonyme</li><li>joignez l \' association de votre plein gré</li><li>vous engagez à respecter les statuts et le réglement intérieur de l \' association (envoyés par mail)</li><li>reconnaissez le but de l \' assocation Coopé Technopôle Metz et vous attestez avoir pris conaissances des droits et des devoirs des membres de l \' association</li><li>consentez à ce que les données fournies à l \' association, ainsi que vos autres données de compte (débit, crédit, solde et historique des transactions) soient stockées dans le logiciel de gestion et accessibles par tous les membres actifs de l \' association, en particulier par le comité de direction</li></ul></strong> ' } )
2018-09-01 19:57:09 +00:00
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
"""
2019-02-28 12:18:41 +00:00
Displays a : class : ` ~ users . forms . SelectUserForm ` to search a user ( : class : ` django . contrib . auth . models . User ` ) .
2018-11-22 21:52:15 +00:00
"""
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 } ) )
2019-01-17 22:16:43 +00:00
return render ( request , " form.html " , { " form_entete " : " Gestion des utilisateurs " , " form " : form , " form_title " : " Rechercher un utilisateur " , " form_button " : " Afficher le profil " , " form_button_icon " : " search " } )
2018-10-05 22:03:02 +00:00
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
"""
2019-02-28 12:18:41 +00:00
Display the list of all users ( : class : ` django . contrib . auth . models . User ` ) .
2018-11-22 21:52:15 +00:00
"""
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
"""
2019-02-28 12:18:41 +00:00
Displays a : class : ` users . form . GroupsEditForm ` to edit the groups of a user ( : class : ` django . contrib . auth . models . User ` ) .
2018-11-22 21:52:15 +00:00
"""
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;} "
2019-01-17 22:16:43 +00:00
return render ( request , " form.html " , { " form_entete " : " Gestion de l ' utilisateur " + user . username , " form " : form , " form_title " : " Modification des groupes " , " form_button " : " Enregistrer " , " form_button_icon " : " pencil-alt " , " extra_css " : extra_css } )
2018-10-05 22:03:02 +00:00
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
"""
2019-02-28 12:18:41 +00:00
Displays a : class : ` users . form . EditPasswordForm ` to edit the password of a user ( : class : ` django . contrib . auth . models . User ` ) .
2018-11-22 21:52:15 +00:00
"""
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 " )
2019-01-17 22:16:43 +00:00
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 " , " form_button_icon " : " pencil-alt " } )
2018-10-05 22:03:02 +00:00
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
"""
2019-02-28 12:18:41 +00:00
Displays a : class : ` ~ users . forms . CreateUserForm ` to edit a user ( : class : ` django . contrib . auth . models . User ` ) .
2018-11-22 21:52:15 +00:00
"""
2018-10-05 22:03:02 +00:00
user = get_object_or_404 ( User , pk = pk )
2019-09-25 19:51:33 +00:00
form = CreateUserForm ( request . POST or None , instance = user , initial = { ' school ' : user . profile . school } , user = user )
2018-10-05 22:03:02 +00:00
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 } ) )
2019-01-17 22:16:43 +00:00
return render ( request , " form.html " , { " form_entete " : " Modification du compte " + user . username , " form " : form , " form_title " : " Modification des informations " , " form_button " : " Modifier " , " form_button_icon " : " pencil-alt " } )
2018-10-05 22:03:02 +00:00
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
"""
2019-02-28 12:18:41 +00:00
Get requested user ( : class : ` django . contrib . auth . models . User ` ) and return username , balance and is_adherent in JSON format .
2018-11-22 21:52:15 +00:00
2019-02-28 12:18:41 +00:00
pk
The primary key of the user to get infos .
2018-11-22 21:52:15 +00:00
"""
2018-10-05 22:03:02 +00:00
user = get_object_or_404 ( User , pk = pk )
2019-01-22 19:27:18 +00:00
data = json . dumps ( { " username " : user . username , " balance " : user . profile . balance , " is_adherent " : user . profile . is_adherent } )
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 ) :
"""
2019-02-28 12:18:41 +00:00
Display all the : class : ` reloads < gestion . models . Reload > ` of the requested user ( : class : ` django . contrib . auth . models . User ` ) .
2018-11-22 21:52:15 +00:00
"""
user = get_object_or_404 ( User , pk = pk )
allReloads = Reload . objects . filter ( customer = user ) . order_by ( ' -date ' )
2018-11-30 18:54:12 +00:00
paginator = Paginator ( allReloads , 10 )
2018-11-22 21:52:15 +00:00
reloads = paginator . get_page ( page )
return render ( request , " users/allReloads.html " , { " reloads " : reloads , " user " : user } )
2018-11-30 18:54:12 +00:00
@active_required
@login_required
@self_or_has_perm ( ' pk ' , ' auth.view_user ' )
def all_consumptions ( request , pk , page ) :
"""
2019-02-28 12:18:41 +00:00
Display all the ` consumptions < gestion . models . ConsumptionHistory > ` of the requested user ( : class : ` django . contrib . auth . models . User ` ) .
2018-11-30 18:54:12 +00:00
"""
user = get_object_or_404 ( User , pk = pk )
all_consumptions = ConsumptionHistory . objects . filter ( customer = user ) . order_by ( ' -date ' )
paginator = Paginator ( all_consumptions , 10 )
consumptions = paginator . get_page ( page )
return render ( request , " users/all_consumptions.html " , { " consumptions " : consumptions , " user " : user } )
@active_required
@login_required
@self_or_has_perm ( ' pk ' , ' auth.view_user ' )
def all_menus ( request , pk , page ) :
"""
2019-02-28 12:18:41 +00:00
Display all the ` menus < gestion . models . MenuHistory > ` of the requested user ( : class : ` django . contrib . auth . models . User ` ) .
2018-11-30 18:54:12 +00:00
"""
user = get_object_or_404 ( User , pk = pk )
all_menus = MenuHistory . objects . filter ( customer = user ) . order_by ( ' -date ' )
paginator = Paginator ( all_menus , 10 )
menus = paginator . get_page ( page )
return render ( request , " users/all_menus.html " , { " menus " : menus , " user " : user } )
2018-12-23 20:38:53 +00:00
@active_required
@login_required
@permission_required ( ' auth.change_user ' )
def switch_activate_user ( request , pk ) :
2019-02-28 12:18:41 +00:00
"""
Switch the active status of the requested user ( : class : ` django . contrib . auth . models . User ` ) .
pk
The primary key of the user to switch status
"""
2018-12-23 20:38:53 +00:00
user = get_object_or_404 ( User , pk = pk )
user . is_active = 1 - user . is_active
user . save ( )
messages . success ( request , " Le statut de l ' utilisateur a bien été changé " )
return HttpResponseRedirect ( request . META . get ( ' HTTP_REFERER ' , ' / ' ) )
2019-02-26 22:18:53 +00:00
@active_required
@login_required
@permission_required ( ' auth.view_user ' )
def gen_user_infos ( request , pk ) :
2019-02-28 12:18:41 +00:00
"""
Generates a latex document include adhesion certificate and list of ` cotisations < users . models . CotisationHistory > ` .
"""
2019-09-23 15:43:48 +00:00
user = get_object_or_404 ( User , pk = pk )
2019-02-26 22:18:53 +00:00
cotisations = CotisationHistory . objects . filter ( user = user ) . order_by ( ' -paymentDate ' )
now = datetime . now ( )
2019-06-23 11:46:12 +00:00
path = os . path . join ( settings . BASE_DIR , " templates/coope.png " )
2019-02-26 22:18:53 +00:00
return render_to_pdf ( request , ' users/bulletin.tex ' , { " user " : user , " now " : now , " cotisations " : cotisations , " path " : path } , filename = " bulletin_ " + user . first_name + " _ " + user . last_name + " .pdf " )
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
"""
2019-02-28 12:18:41 +00:00
Displays all the groups ( : class : ` django . contrib . auth . models . Group ` ) .
2018-11-22 21:52:15 +00:00
"""
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
"""
2019-02-28 12:18:41 +00:00
Displays the profile of a group ( : class : ` django . contrib . auth . models . Group ` ) .
2018-11-22 21:52:15 +00:00
"""
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
"""
2019-02-28 12:18:41 +00:00
Displays a : class : ` ~ users . forms . CreateGroupForm ` to create a group ( : class : ` django . contrib . auth . models . Group ` ) .
2018-11-22 21:52:15 +00:00
"""
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 } ) )
2019-01-17 22:16:43 +00:00
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 " , " form_button_icon " : " plus-square " } )
2018-09-01 19:57:09 +00:00
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
"""
2019-02-28 12:18:41 +00:00
Displays a : class : ` ~ users . forms . EditGroupForm ` to edit a group ( : class : ` django . contrib . auth . models . Group ` ) .
2018-11-22 21:52:15 +00:00
2019-02-28 12:18:41 +00:00
pk
The primary key of the group to edit .
2018-11-22 21:52:15 +00:00
"""
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 } ) )
2019-01-17 22:16:43 +00:00
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 " , " form_button_icon " : " pencil-alt " , " extra_css " : extra_css } )
2018-09-01 19:57:09 +00:00
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
"""
2019-02-28 12:18:41 +00:00
Deletes the requested group ( : class : ` django . contrib . auth . models . Group ` ) .
2018-11-22 21:52:15 +00:00
2019-02-28 12:18:41 +00:00
pk
The primary key of the group to delete
2018-11-22 21:52:15 +00:00
"""
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
"""
2019-02-28 12:18:41 +00:00
Removes a right from a given group ( : class : ` django . contrib . auth . models . Group ` ) .
2018-11-22 21:52:15 +00:00
"""
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
"""
2019-02-28 12:18:41 +00:00
Removes a user ( : class : ` django . contrib . auth . models . User ` ) from a given group ( : class : ` django . contrib . auth . models . Group ` ) .
2018-11-22 21:52:15 +00:00
"""
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 ) :
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Lists the staff ( : class : ` django . contrib . auth . models . User ` with is_staff True )
2018-12-02 15:28:40 +00:00
"""
2018-09-01 19:57:09 +00:00
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-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Displays a : class : ` users . forms . SelectNonAdminUserForm ` to select a non admin user ( : class : ` django . contrib . auth . models . User ` ) and add it to the admins .
2018-12-02 15:28:40 +00:00
"""
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 ' ) )
2019-01-17 22:16:43 +00:00
return render ( request , " form.html " , { " form " : form , " form_title " : " Ajout d ' un admin " , " form_button " : " Ajouter l ' utilisateur aux admins " , " form_button_icon " : " user-plus " } )
2018-09-01 19:57:09 +00:00
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 ) :
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Removes an user ( : class : ` django . contrib . auth . models . User ` ) from staff .
2018-12-02 15:28:40 +00:00
2019-02-28 12:18:41 +00:00
pk
The primary key of the user ( : class : ` django . contrib . auth . models . User ` ) to remove from staff
2018-12-02 15:28:40 +00:00
"""
2018-09-01 19:57:09 +00:00
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 ) :
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Lists the superusers ( : class : ` django . contrib . auth . models . User ` with is_superuser True ) .
2018-12-02 15:28:40 +00:00
"""
2018-09-01 19:57:09 +00:00
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-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Displays a : class : ` users . forms . SelectNonAdminUserForm ` to select a non superuser user ( : class : ` django . contrib . auth . models . User ` ) and add it to the superusers .
2018-12-02 15:28:40 +00:00
"""
2018-10-05 22:03:02 +00:00
form = SelectNonSuperUserForm ( request . POST or None )
2018-12-02 15:28:40 +00:00
if form . is_valid ( ) :
2018-09-01 19:57:09 +00:00
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 ' ) )
2019-01-17 22:16:43 +00:00
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 " , " form_button_icon " : " user-plus " } )
2018-09-01 19:57:09 +00:00
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 ) :
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Removes a user ( : class : ` django . contrib . auth . models . User ` ) from superusers .
2018-12-02 15:28:40 +00:00
"""
2018-09-01 19:57:09 +00:00
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 ) :
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Displays a : class : ` users . forms . addCotisationHistoryForm ` to add a : class : ` Cotisation History < users . models . CotisationHistory ` to the requested user ( : class : ` django . contrib . auth . models . User ` ) .
2018-12-02 15:28:40 +00:00
2019-02-28 12:18:41 +00:00
pk
The primary key of the user to add a cotisation history
2018-12-02 15:28:40 +00:00
"""
2018-10-05 22:03:02 +00:00
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 :
messages . error ( request , " Solde insuffisant " )
2018-11-27 08:07:12 +00:00
return redirect ( reverse ( ' users:profile ' , kwargs = { ' pk ' : user . pk } ) )
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
2019-06-23 08:54:21 +00:00
cotisation . amount_ptm = cotisation . cotisation . amount_ptm
2019-01-11 07:46:29 +00:00
if ( user . profile . cotisationEnd and user . profile . cotisationEnd > timezone . now ( ) ) :
2018-10-05 22:03:02 +00:00
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 } ) )
2019-01-17 22:16:43 +00:00
return render ( request , " form.html " , { " form " : form , " form_title " : " Ajout d ' une cotisation pour l ' utilisateur " + str ( user ) , " form_button " : " Ajouter " , " form_button_icon " : " plus-square " } )
2018-10-05 22:03:02 +00:00
2018-11-22 21:52:15 +00:00
@active_required
@login_required
2019-02-26 22:18:53 +00:00
@permission_required ( ' users.delete_cotisationhistory ' )
def deleteCotisationHistory ( request , pk ) :
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Delete the requested : class : ` ~ users . models . CotisationHistory ` .
2018-12-02 15:28:40 +00:00
2019-02-28 12:18:41 +00:00
pk
The primary key of tthe CotisationHistory to delete .
2018-12-02 15:28:40 +00:00
"""
2018-10-05 22:03:02 +00:00
cotisationHistory = get_object_or_404 ( CotisationHistory , pk = pk )
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 ) :
2019-01-23 09:42:54 +00:00
user . profile . debit - = cotisationHistory . cotisation . amount
2018-10-05 22:03:02 +00:00
user . save ( )
2019-02-26 22:18:53 +00:00
cotisationHistory . delete ( )
messages . success ( request , " La cotisation a bien été supprimée " )
2018-10-05 22:03:02 +00:00
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 ) :
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Displays a : class : ` users . forms . addWhiteListHistoryForm ` to add a : class : ` ~ users . models . WhiteListHistory ` to the requested user ( : class : ` django . contrib . auth . models . User ` ) .
2018-12-02 15:28:40 +00:00
"""
2018-10-05 22:03:02 +00:00
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 :
2018-11-27 08:07:12 +00:00
whiteList . endDate = datetime . now ( ) + timedelta ( days = whiteList . duration )
2018-10-05 22:03:02 +00:00
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 } ) )
2019-01-17 22:16:43 +00:00
return render ( request , " form.html " , { " form " : form , " form_title " : " Ajout d ' un accès gracieux pour " + user . username , " form_button " : " Ajouter " , " form_button_icon " : " plus-square " } )
2018-10-05 22:03:02 +00:00
########## 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 ) :
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Lists the : class : ` Schools < users . models . School > ` .
2018-12-02 15:28:40 +00:00
"""
2018-10-05 22:03:02 +00:00
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 ) :
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Displays : class : ` ~ users . forms . SchoolForm ` to add a : class : ` ~ users . models . School ` .
2018-12-02 15:28:40 +00:00
"""
2018-10-05 22:03:02 +00:00
form = SchoolForm ( request . POST or None )
2018-12-02 15:28:40 +00:00
if form . is_valid ( ) :
2018-10-05 22:03:02 +00:00
form . save ( )
messages . success ( request , " L ' école a bien été créée " )
return redirect ( reverse ( ' users:schoolsIndex ' ) )
2019-01-17 22:16:43 +00:00
return render ( request , " form.html " , { " form " : form , " form_title " : " Création d ' une école " , " form_button " : " Créer " , " form_button_icon " : " plus-square " } )
2018-10-05 22:03:02 +00:00
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 ) :
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Displays : class : ` ~ users . forms . SchoolForm ` to edit a : class : ` ~ users . models . School ` .
2018-12-02 15:28:40 +00:00
2019-02-28 12:18:41 +00:00
pk
The primary key of the school to edit .
2018-12-02 15:28:40 +00:00
"""
2018-10-05 22:03:02 +00:00
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 ' ) )
2019-01-17 22:16:43 +00:00
return render ( request , " form.html " , { " form " : form , " form_title " : " Modification de l ' école " + str ( school ) , " form_button " : " Modifier " , " form_button " : " pencil-alt " } )
2018-10-05 22:03:02 +00:00
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 ) :
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Deletes a : class : ` users . models . School ` .
2018-12-02 15:28:40 +00:00
2019-02-28 12:18:41 +00:00
pk
The primary key of the School to delete .
2018-12-02 15:28:40 +00:00
"""
2018-10-05 22:03:02 +00:00
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 ) :
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Autcomplete for all users ( : class : ` django . contrib . auth . models . User ` ) .
2018-12-02 15:28:40 +00:00
"""
2018-10-05 22:03:02 +00:00
def get_queryset ( self ) :
qs = User . objects . all ( )
if self . q :
2019-01-23 11:31:33 +00:00
qs = qs . filter ( Q ( username__icontains = self . q ) | Q ( first_name__icontains = self . q ) | Q ( last_name__icontains = self . q ) )
2018-10-05 22:03:02 +00:00
return qs
class ActiveUsersAutocomplete ( autocomplete . Select2QuerySetView ) :
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Autocomplete for active users ( : class : ` django . contrib . auth . models . User ` ) .
2018-12-02 15:28:40 +00:00
"""
2018-10-05 22:03:02 +00:00
def get_queryset ( self ) :
qs = User . objects . filter ( is_active = True )
if self . q :
2019-01-23 11:31:33 +00:00
qs = qs . filter ( Q ( username__icontains = self . q ) | Q ( first_name__icontains = self . q ) | Q ( last_name__icontains = self . q ) )
2018-10-05 22:03:02 +00:00
return qs
class AdherentAutocomplete ( autocomplete . Select2QuerySetView ) :
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Autocomplete for adherents ( : class : ` django . contrib . auth . models . User ` ) .
2018-12-02 15:28:40 +00:00
"""
2018-10-05 22:03:02 +00:00
def get_queryset ( self ) :
qs = User . objects . all ( )
2019-01-23 09:49:33 +00:00
pks = [ x . pk for x in qs if x . is_adherent ]
qs = User . objects . filter ( pk__in = pks )
if self . q :
2019-01-23 11:31:33 +00:00
qs = qs . filter ( Q ( username__icontains = self . q ) | Q ( first_name__icontains = self . q ) | Q ( last_name__icontains = self . q ) )
2018-10-05 22:03:02 +00:00
return qs
2019-01-23 09:49:33 +00:00
2018-10-05 22:03:02 +00:00
class NonSuperUserAutocomplete ( autocomplete . Select2QuerySetView ) :
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Autocomplete for non - superuser users ( : class : ` django . contrib . auth . models . User ` ) .
2018-12-02 15:28:40 +00:00
"""
2018-10-05 22:03:02 +00:00
def get_queryset ( self ) :
qs = User . objects . filter ( is_superuser = False )
2018-11-22 21:52:15 +00:00
if self . q :
2019-01-23 11:31:33 +00:00
qs = qs . filter ( Q ( username__icontains = self . q ) | Q ( first_name__icontains = self . q ) | Q ( last_name__icontains = self . q ) )
2018-11-22 21:52:15 +00:00
return qs
class NonAdminUserAutocomplete ( autocomplete . Select2QuerySetView ) :
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Autocomplete for non - admin users ( : class : ` django . contrib . auth . models . User ` ) .
2018-12-02 15:28:40 +00:00
"""
2018-11-22 21:52:15 +00:00
def get_queryset ( self ) :
qs = User . objects . filter ( is_staff = False )
2018-10-05 22:03:02 +00:00
if self . q :
2019-01-23 11:31:33 +00:00
qs = qs . filter ( Q ( username__icontains = self . q ) | Q ( first_name__icontains = self . q ) | Q ( last_name__icontains = self . q ) )
2019-06-19 10:47:55 +00:00
return qs