8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-04 17:06:27 +00:00
re2o/users/views.py

633 lines
27 KiB
Python
Raw Normal View History

2016-06-30 18:18:06 +00:00
# App de gestion des users pour re2o
2016-11-11 20:10:10 +00:00
# Goulven Kermarec, Gabriel Détraz, Lemesle Augustin
2016-06-30 18:18:06 +00:00
# Gplv2
from django.shortcuts import render_to_response, get_object_or_404, render, redirect
2016-06-30 18:18:06 +00:00
from django.core.context_processors import csrf
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.template import Context, RequestContext, loader
from django.contrib import messages
2016-07-09 02:12:09 +00:00
from django.contrib.auth.decorators import login_required, permission_required
2016-07-06 17:45:36 +00:00
from django.db.models import Max, ProtectedError
from django.db import IntegrityError
from django.core.mail import send_mail
from django.utils import timezone
from django.core.urlresolvers import reverse
from django.db import transaction
2016-06-30 01:39:07 +00:00
2016-12-11 16:24:04 +00:00
from reversion.models import Version
from reversion import revisions as reversion
from users.models import User, Right, Ban, Whitelist, School, ListRight, Request
from users.models import DelRightForm, BanForm, WhitelistForm, DelSchoolForm, DelListRightForm, NewListRightForm
from users.models import EditInfoForm, InfoForm, BaseInfoForm, StateForm, RightForm, SchoolForm, ListRightForm
2016-07-03 16:09:58 +00:00
from cotisations.models import Facture
2016-07-04 01:01:09 +00:00
from machines.models import Machine, Interface
2016-07-20 10:06:33 +00:00
from users.forms import PassForm, ResetPasswordForm
2016-07-04 15:13:26 +00:00
from machines.views import unassign_ips, assign_ips
2016-06-30 18:18:06 +00:00
from re2o.login import hashNT
from re2o.settings import REQ_EXPIRE_STR, EMAIL_FROM, ASSO_NAME, ASSO_EMAIL, SITE_NAME, PAGINATION_NUMBER
2016-07-01 22:35:44 +00:00
def archive(user):
""" Archive un utilisateur """
unassign_ips(user)
return
2016-07-04 15:13:26 +00:00
def unarchive(user):
""" Triger actions au desarchivage d'un user """
assign_ips(user)
return
2016-06-30 18:18:06 +00:00
def form(ctx, template, request):
c = ctx
c.update(csrf(request))
return render_to_response(
template,
c,
context_instance=RequestContext(request)
)
def password_change_action(u_form, user, request, req=False):
""" Fonction qui effectue le changeemnt de mdp bdd"""
if u_form.cleaned_data['passwd1'] != u_form.cleaned_data['passwd2']:
messages.error(request, "Les 2 mots de passe différent")
return form({'userform': u_form}, 'users/user.html', request)
user.set_password(u_form.cleaned_data['passwd1'])
user.pwd_ntlm = hashNT(u_form.cleaned_data['passwd1'])
with transaction.atomic(), reversion.create_revision():
user.save()
reversion.set_comment("Réinitialisation du mot de passe")
messages.success(request, "Le mot de passe a changé")
if req:
req.delete()
return redirect("/")
return redirect("/users/profil/" + str(user.id))
def reset_passwd_mail(req, request):
2016-11-21 00:49:45 +00:00
""" Prend en argument un request, envoie un mail de réinitialisation de mot de pass """
t = loader.get_template('users/email_passwd_request')
c = Context({
'name': str(req.user.name) + ' ' + str(req.user.surname),
'asso': ASSO_NAME,
'asso_mail': ASSO_EMAIL,
'site_name': SITE_NAME,
'url': request.build_absolute_uri(
reverse('users:process', kwargs={'token': req.token})),
'expire_in': REQ_EXPIRE_STR,
})
send_mail('Changement de mot de passe', t.render(c),
EMAIL_FROM, [req.user.email], fail_silently=False)
return
def notif_ban(ban):
2016-11-21 00:49:45 +00:00
""" Prend en argument un objet ban, envoie un mail de notification """
t = loader.get_template('users/email_ban_notif')
c = Context({
'name': str(ban.user.name) + ' ' + str(ban.user.surname),
'raison': ban.raison,
'date_end': ban.date_end,
})
send_mail('Deconnexion disciplinaire', t.render(c),
EMAIL_FROM, [ban.user.email], fail_silently=False)
return
2016-12-03 23:12:50 +00:00
def notif_inscription(user):
""" Prend en argument un objet user, envoie un mail de bienvenue """
t = loader.get_template('users/email_welcome')
c = Context({
'nom': str(user.name) + ' ' + str(user.surname),
2016-12-04 15:51:55 +00:00
'asso_name': ASSO_NAME,
'asso_email':ASSO_EMAIL,
2016-12-03 23:12:50 +00:00
})
2016-12-04 15:51:55 +00:00
send_mail('Bienvenue au Rézo', '',
EMAIL_FROM, [user.email], html_message=t.render(c))
2016-12-03 23:12:50 +00:00
return
2016-07-08 10:35:53 +00:00
@login_required
2016-07-09 15:16:44 +00:00
@permission_required('cableur')
2016-06-30 18:18:06 +00:00
def new_user(request):
2016-11-21 00:49:45 +00:00
""" Vue de création d'un nouvel utilisateur, envoie un mail pour le mot de passe"""
user = InfoForm(request.POST or None)
if user.is_valid():
user = user.save(commit=False)
with transaction.atomic(), reversion.create_revision():
user.save()
reversion.set_user(request.user)
reversion.set_comment("Création")
req = Request()
req.type = Request.PASSWD
req.user = user
req.save()
reset_passwd_mail(req, request)
2016-12-03 23:12:50 +00:00
notif_inscription(user)
messages.success(request, "L'utilisateur %s a été crée, un mail pour l'initialisation du mot de passe a été envoyé" % user.pseudo)
return redirect("/users/profil/" + str(user.id))
return form({'userform': user}, 'users/user.html', request)
2016-07-08 10:35:53 +00:00
@login_required
def edit_info(request, userid):
2016-11-21 00:49:45 +00:00
""" Edite un utilisateur à partir de son id,
si l'id est différent de request.user, vérifie la possession du droit cableur """
try:
user = User.objects.get(pk=userid)
except User.DoesNotExist:
messages.error(request, "Utilisateur inexistant")
return redirect("/users/")
if not request.user.has_perms(('cableur',)) and user != request.user:
messages.error(request, "Vous ne pouvez pas modifier un autre user que vous sans droit cableur")
return redirect("/users/profil/" + str(request.user.id))
if not request.user.has_perms(('cableur',)):
user = BaseInfoForm(request.POST or None, instance=user)
else:
2016-11-25 21:50:53 +00:00
user = InfoForm(request.POST or None, instance=user)
if user.is_valid():
with transaction.atomic(), reversion.create_revision():
user.save()
reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in user.changed_data))
messages.success(request, "L'user a bien été modifié")
return redirect("/users/profil/" + userid)
return form({'userform': user}, 'users/user.html', request)
2016-07-08 10:35:53 +00:00
@login_required
2016-07-09 15:16:44 +00:00
@permission_required('bureau')
def state(request, userid):
2016-11-21 00:49:45 +00:00
""" Changer l'etat actif/desactivé/archivé d'un user, need droit bureau """
try:
user = User.objects.get(pk=userid)
except User.DoesNotExist:
messages.error(request, "Utilisateur inexistant")
return redirect("/users/")
state = StateForm(request.POST or None, instance=user)
if state.is_valid():
if state.has_changed():
2016-10-23 23:28:11 +00:00
if state.cleaned_data['state'] == User.STATE_ARCHIVE:
archive(user)
2016-07-04 15:13:26 +00:00
else:
unarchive(user)
with transaction.atomic(), reversion.create_revision():
state.save()
reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in state.changed_data))
messages.success(request, "Etat changé avec succès")
return redirect("/users/profil/" + userid)
return form({'userform': state}, 'users/user.html', request)
2016-07-08 10:35:53 +00:00
@login_required
def password(request, userid):
2016-11-21 00:49:45 +00:00
""" 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 """
try:
user = User.objects.get(pk=userid)
except User.DoesNotExist:
messages.error(request, "Utilisateur inexistant")
return redirect("/users/")
if not request.user.has_perms(('cableur',)) and user != request.user:
messages.error(request, "Vous ne pouvez pas modifier un autre user que vous sans droit cableur")
return redirect("/users/profil/" + str(request.user.id))
if not request.user.has_perms(('bureau',)) and user != request.user and Right.objects.filter(user=user):
messages.error(request, "Il faut les droits bureau pour modifier le mot de passe d'un membre actif")
return redirect("/users/profil/" + str(request.user.id))
u_form = PassForm(request.POST or None)
if u_form.is_valid():
return password_change_action(u_form, user, request)
return form({'userform': u_form}, 'users/user.html', request)
2016-07-01 22:35:44 +00:00
2016-07-08 10:35:53 +00:00
@login_required
2016-07-09 02:12:09 +00:00
@permission_required('bureau')
def add_right(request, userid):
2016-11-21 00:49:45 +00:00
""" Ajout d'un droit à un user, need droit bureau """
try:
user = User.objects.get(pk=userid)
except User.DoesNotExist:
messages.error(request, "Utilisateur inexistant")
return redirect("/users/")
2016-07-02 00:07:17 +00:00
right = RightForm(request.POST or None)
if right.is_valid():
right = right.save(commit=False)
right.user = user
try:
2016-10-31 16:52:16 +00:00
with transaction.atomic(), reversion.create_revision():
reversion.set_user(request.user)
reversion.set_comment("Ajout du droit %s" % right.right)
right.save()
messages.success(request, "Droit ajouté")
except IntegrityError:
pass
return redirect("/users/profil/" + userid)
2016-07-02 00:07:17 +00:00
return form({'userform': right}, 'users/user.html', request)
2016-07-08 10:35:53 +00:00
@login_required
2016-07-09 02:12:09 +00:00
@permission_required('bureau')
def del_right(request):
2016-11-21 00:49:45 +00:00
""" Supprimer un droit à un user, need droit bureau """
user_right_list = DelRightForm(request.POST or None)
if user_right_list.is_valid():
right_del = user_right_list.cleaned_data['rights']
2016-10-31 16:52:16 +00:00
with transaction.atomic(), reversion.create_revision():
reversion.set_user(request.user)
reversion.set_comment("Retrait des droit %s" % ','.join(str(deleted_right) for deleted_right in right_del))
right_del.delete()
messages.success(request, "Droit retiré avec succès")
return redirect("/users/")
2016-10-31 16:52:16 +00:00
return form({'userform': user_right_list}, 'users/user.html', request)
2016-07-08 10:35:53 +00:00
@login_required
2016-07-09 15:16:44 +00:00
@permission_required('bofh')
def add_ban(request, userid):
2016-11-21 00:49:45 +00:00
""" Ajouter un banissement, nécessite au moins le droit bofh (a fortiori bureau)
Syntaxe : JJ/MM/AAAA , heure optionnelle, prend effet immédiatement"""
try:
user = User.objects.get(pk=userid)
except User.DoesNotExist:
messages.error(request, "Utilisateur inexistant")
return redirect("/users/")
ban_instance = Ban(user=user)
ban = BanForm(request.POST or None, instance=ban_instance)
if ban.is_valid():
with transaction.atomic(), reversion.create_revision():
ban_object = ban.save()
notif_ban(ban_object)
reversion.set_user(request.user)
reversion.set_comment("Création")
messages.success(request, "Bannissement ajouté")
return redirect("/users/profil/" + userid)
if user.is_ban():
messages.error(
request,
"Attention, cet utilisateur a deja un bannissement actif"
)
return form({'userform': ban}, 'users/user.html', request)
2016-07-08 10:35:53 +00:00
@login_required
2016-07-09 15:16:44 +00:00
@permission_required('bofh')
def edit_ban(request, banid):
2016-11-21 00:49:45 +00:00
""" Editer un bannissement, nécessite au moins le droit bofh (a fortiori bureau)
Syntaxe : JJ/MM/AAAA , heure optionnelle, prend effet immédiatement"""
try:
ban_instance = Ban.objects.get(pk=banid)
2016-07-04 18:04:11 +00:00
except Ban.DoesNotExist:
messages.error(request, "Entrée inexistante")
return redirect("/users/")
ban = BanForm(request.POST or None, instance=ban_instance)
if ban.is_valid():
with transaction.atomic(), reversion.create_revision():
ban.save()
reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in ban.changed_data))
messages.success(request, "Bannissement modifié")
return redirect("/users/")
return form({'userform': ban}, 'users/user.html', request)
2016-07-08 10:35:53 +00:00
@login_required
2016-07-09 15:16:44 +00:00
@permission_required('cableur')
2016-07-04 18:04:11 +00:00
def add_whitelist(request, userid):
2016-11-21 00:49:45 +00:00
""" Accorder un accès gracieux, temporaire ou permanent. Need droit cableur
Syntaxe : JJ/MM/AAAA , heure optionnelle, prend effet immédiatement, raison obligatoire"""
2016-07-04 18:04:11 +00:00
try:
user = User.objects.get(pk=userid)
except User.DoesNotExist:
messages.error(request, "Utilisateur inexistant")
2016-07-04 18:04:11 +00:00
return redirect("/users/")
whitelist_instance = Whitelist(user=user)
whitelist = WhitelistForm(request.POST or None, instance=whitelist_instance)
if whitelist.is_valid():
with transaction.atomic(), reversion.create_revision():
whitelist.save()
reversion.set_user(request.user)
reversion.set_comment("Création")
2016-07-04 18:04:11 +00:00
messages.success(request, "Accès à titre gracieux accordé")
return redirect("/users/profil/" + userid)
2016-10-23 23:28:11 +00:00
if user.is_whitelisted():
messages.error(
request,
"Attention, cet utilisateur a deja un accès gracieux actif"
)
2016-07-04 18:04:11 +00:00
return form({'userform': whitelist}, 'users/user.html', request)
2016-07-08 10:35:53 +00:00
@login_required
2016-07-09 15:16:44 +00:00
@permission_required('cableur')
2016-07-04 18:04:11 +00:00
def edit_whitelist(request, whitelistid):
2016-11-21 00:49:45 +00:00
""" Editer un accès gracieux, temporaire ou permanent. Need droit cableur
Syntaxe : JJ/MM/AAAA , heure optionnelle, prend effet immédiatement, raison obligatoire"""
2016-07-04 18:04:11 +00:00
try:
whitelist_instance = Whitelist.objects.get(pk=whitelistid)
except Whitelist.DoesNotExist:
messages.error(request, "Entrée inexistante")
2016-07-04 18:04:11 +00:00
return redirect("/users/")
whitelist = WhitelistForm(request.POST or None, instance=whitelist_instance)
if whitelist.is_valid():
with transaction.atomic(), reversion.create_revision():
whitelist.save()
reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in whitelist.changed_data))
2016-07-04 18:04:11 +00:00
messages.success(request, "Whitelist modifiée")
return redirect("/users/")
return form({'userform': whitelist}, 'users/user.html', request)
2016-07-08 10:35:53 +00:00
@login_required
2016-07-09 15:16:44 +00:00
@permission_required('cableur')
2016-07-06 17:45:36 +00:00
def add_school(request):
2016-11-21 00:49:45 +00:00
""" Ajouter un établissement d'enseignement à la base de donnée, need cableur"""
2016-07-06 17:45:36 +00:00
school = SchoolForm(request.POST or None)
if school.is_valid():
with transaction.atomic(), reversion.create_revision():
school.save()
reversion.set_user(request.user)
reversion.set_comment("Création")
2016-07-06 17:45:36 +00:00
messages.success(request, "L'établissement a été ajouté")
return redirect("/users/index_school/")
return form({'userform': school}, 'users/user.html', request)
2016-07-06 17:45:36 +00:00
2016-07-08 10:35:53 +00:00
@login_required
2016-07-09 15:16:44 +00:00
@permission_required('cableur')
def edit_school(request, schoolid):
2016-11-21 00:49:45 +00:00
""" Editer un établissement d'enseignement à partir du schoolid dans la base de donnée, need cableur"""
try:
school_instance = School.objects.get(pk=schoolid)
except School.DoesNotExist:
messages.error(request, u"Entrée inexistante" )
return redirect("/users/")
school = SchoolForm(request.POST or None, instance=school_instance)
if school.is_valid():
with transaction.atomic(), reversion.create_revision():
school.save()
reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in school.changed_data))
messages.success(request, "Établissement modifié")
return redirect("/users/index_school/")
return form({'userform': school}, 'users/user.html', request)
2016-07-08 10:35:53 +00:00
@login_required
2016-07-09 15:16:44 +00:00
@permission_required('cableur')
2016-07-06 17:45:36 +00:00
def del_school(request):
2016-11-21 00:49:45 +00:00
""" 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 """
2016-07-06 17:45:36 +00:00
school = DelSchoolForm(request.POST or None)
if school.is_valid():
school_dels = school.cleaned_data['schools']
for school_del in school_dels:
try:
with transaction.atomic(), reversion.create_revision():
school_del.delete()
reversion.set_comment("Destruction")
2016-07-06 17:45:36 +00:00
messages.success(request, "L'établissement a été supprimé")
except ProtectedError:
messages.error(
request,
"L'établissement %s est affecté à au moins un user, \
2016-07-08 01:40:04 +00:00
vous ne pouvez pas le supprimer" % school_del)
return redirect("/users/index_school/")
2016-07-06 17:45:36 +00:00
return form({'userform': school}, 'users/user.html', request)
@login_required
@permission_required('bureau')
def add_listright(request):
2016-11-21 00:49:45 +00:00
""" Ajouter un droit/groupe, nécessite droit bureau.
Obligation de fournir un gid pour la synchro ldap, unique """
listright = NewListRightForm(request.POST or None)
if listright.is_valid():
with transaction.atomic(), reversion.create_revision():
listright.save()
reversion.set_user(request.user)
reversion.set_comment("Création")
messages.success(request, "Le droit/groupe a été ajouté")
return redirect("/users/index_listright/")
return form({'userform': listright}, 'users/user.html', request)
@login_required
@permission_required('bureau')
def edit_listright(request, listrightid):
2016-11-21 00:49:45 +00:00
""" Editer un groupe/droit, necessite droit bureau, à partir du listright id """
try:
listright_instance = ListRight.objects.get(pk=listrightid)
except ListRight.DoesNotExist:
messages.error(request, u"Entrée inexistante" )
return redirect("/users/")
listright = ListRightForm(request.POST or None, instance=listright_instance)
if listright.is_valid():
with transaction.atomic(), reversion.create_revision():
listright.save()
reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in listright.changed_data))
messages.success(request, "Droit modifié")
return redirect("/users/index_listright/")
return form({'userform': listright}, 'users/user.html', request)
@login_required
@permission_required('bureau')
def del_listright(request):
2016-11-21 00:49:45 +00:00
""" Supprimer un ou plusieurs groupe, possible si il est vide, need droit bureau """
listright = DelListRightForm(request.POST or None)
if listright.is_valid():
listright_dels = listright.cleaned_data['listrights']
for listright_del in listright_dels:
try:
with transaction.atomic(), reversion.create_revision():
listright_del.delete()
reversion.set_comment("Destruction")
messages.success(request, "Le droit/groupe a été supprimé")
except ProtectedError:
messages.error(
request,
"L'établissement %s est affecté à au moins un user, \
vous ne pouvez pas le supprimer" % listright_del)
return redirect("/users/index_listright/")
return form({'userform': listright}, 'users/user.html', request)
2016-07-08 10:35:53 +00:00
@login_required
@permission_required('cableur')
2016-07-01 22:35:44 +00:00
def index(request):
2016-11-21 00:49:45 +00:00
""" Affiche l'ensemble des users, need droit cableur """
users_list = User.objects.order_by('state', 'name')
paginator = Paginator(users_list, PAGINATION_NUMBER)
page = request.GET.get('page')
try:
users_list = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
users_list = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
users_list = paginator.page(paginator.num_pages)
return render(request, 'users/index.html', {'users_list': users_list})
2016-07-03 16:09:58 +00:00
2016-07-08 10:35:53 +00:00
@login_required
2016-07-09 15:16:44 +00:00
@permission_required('cableur')
def index_ban(request):
2016-11-21 00:49:45 +00:00
""" Affiche l'ensemble des ban, need droit cableur """
ban_list = Ban.objects.order_by('date_start').reverse()
paginator = Paginator(ban_list, PAGINATION_NUMBER)
page = request.GET.get('page')
try:
ban_list = paginator.page(page)
except PageNotAnInteger:
# If page isn't an integer, deliver first page
ban_list = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
ban_list = paginator.page(paginator.num_pages)
return render(request, 'users/index_ban.html', {'ban_list': ban_list})
2016-07-08 10:35:53 +00:00
@login_required
2016-07-09 15:16:44 +00:00
@permission_required('cableur')
def index_white(request):
2016-11-21 00:49:45 +00:00
""" Affiche l'ensemble des whitelist, need droit cableur """
white_list = Whitelist.objects.order_by('date_start')
return render(
request,
'users/index_whitelist.html',
{'white_list': white_list}
)
2016-07-08 10:35:53 +00:00
@login_required
2016-07-09 15:16:44 +00:00
@permission_required('cableur')
def index_school(request):
2016-11-21 00:49:45 +00:00
""" Affiche l'ensemble des établissement, need droit cableur """
school_list = School.objects.order_by('name')
return render(request, 'users/index_schools.html', {'school_list':school_list})
@login_required
@permission_required('cableur')
def index_listright(request):
2016-11-21 00:49:45 +00:00
""" Affiche l'ensemble des droits , need droit cableur """
listright_list = ListRight.objects.order_by('listright')
return render(request, 'users/index_listright.html', {'listright_list':listright_list})
@login_required
def history(request, object, id):
2016-11-21 00:49:45 +00:00
""" Affichage de l'historique : (acl, argument)
user : self or cableur, userid,
ban : self or cableur, banid,
whitelist : self or cableur, whitelistid,
school : cableur, schoolid,
listright : cableur, listrightid """
if object == 'user':
try:
object_instance = User.objects.get(pk=id)
except User.DoesNotExist:
messages.error(request, "Utilisateur inexistant")
return redirect("/users/")
if not request.user.has_perms(('cableur',)) and object_instance != request.user:
messages.error(request, "Vous ne pouvez pas afficher l'historique d'un autre user que vous sans droit cableur")
return redirect("/users/profil/" + str(request.user.id))
elif object == 'ban':
try:
object_instance = Ban.objects.get(pk=id)
except Ban.DoesNotExist:
messages.error(request, "Bannissement inexistant")
return redirect("/users/")
if not request.user.has_perms(('cableur',)) and object_instance.user != request.user:
messages.error(request, "Vous ne pouvez pas afficher les bans d'un autre user que vous sans droit cableur")
return redirect("/users/profil/" + str(request.user.id))
elif object == 'whitelist':
try:
object_instance = Whitelist.objects.get(pk=id)
except Whiltelist.DoesNotExist:
messages.error(request, "Whitelist inexistant")
return redirect("/users/")
if not request.user.has_perms(('cableur',)) and object_instance.user != request.user:
messages.error(request, "Vous ne pouvez pas afficher les whitelist d'un autre user que vous sans droit cableur")
return redirect("/users/profil/" + str(request.user.id))
elif object == 'school' and request.user.has_perms(('cableur',)):
try:
object_instance = School.objects.get(pk=id)
except School.DoesNotExist:
messages.error(request, "Ecole inexistante")
return redirect("/users/")
elif object == 'listright' and request.user.has_perms(('cableur',)):
try:
object_instance = ListRight.objects.get(pk=id)
except ListRight.DoesNotExist:
messages.error(request, "Droit inexistant")
return redirect("/users/")
else:
messages.error(request, "Objet inconnu")
return redirect("/users/")
2016-12-11 16:24:04 +00:00
reversions = Version.objects.get_for_object(object_instance)
2016-07-27 01:36:28 +00:00
paginator = Paginator(reversions, PAGINATION_NUMBER)
page = request.GET.get('page')
try:
reversions = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
reversions = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
reversions = paginator.page(paginator.num_pages)
return render(request, 're2o/history.html', {'reversions': reversions, 'object': object_instance})
@login_required
def mon_profil(request):
2016-11-21 00:49:45 +00:00
""" Lien vers profil, renvoie request.id à la fonction """
return redirect("/users/profil/" + str(request.user.id))
2016-07-08 10:35:53 +00:00
@login_required
def profil(request, userid):
2016-11-21 00:49:45 +00:00
""" Affiche un profil, self or cableur, prend un userid en argument """
try:
users = User.objects.get(pk=userid)
except User.DoesNotExist:
messages.error(request, "Utilisateur inexistant")
return redirect("/users/")
if not request.user.has_perms(('cableur',)) and users != request.user:
messages.error(request, "Vous ne pouvez pas afficher un autre user que vous sans droit cableur")
return redirect("/users/profil/" + str(request.user.id))
machines = Machine.objects.filter(user__pseudo=users)
factures = Facture.objects.filter(user__pseudo=users)
bans = Ban.objects.filter(user__pseudo=users)
whitelists = Whitelist.objects.filter(user__pseudo=users)
list_droits = Right.objects.filter(user=users)
return render(
request,
'users/profil.html',
{
'user': users,
'machines_list': machines,
'facture_list': factures,
'ban_list': bans,
'white_list': whitelists,
2016-07-09 15:16:44 +00:00
'list_droits': list_droits,
2016-07-08 01:40:04 +00:00
}
)
2016-07-03 16:09:58 +00:00
2016-07-20 10:06:33 +00:00
def reset_password(request):
2016-11-21 00:49:45 +00:00
""" Reintialisation du mot de passe si mdp oublié """
2016-07-20 10:06:33 +00:00
userform = ResetPasswordForm(request.POST or None)
if userform.is_valid():
try:
user = User.objects.get(pseudo=userform.cleaned_data['pseudo'],email=userform.cleaned_data['email'])
except User.DoesNotExist:
messages.error(request, "Cet utilisateur n'existe pas")
return form({'userform': userform}, 'users/user.html', request)
req = Request()
req.type = Request.PASSWD
req.user = user
req.save()
reset_passwd_mail(req, request)
messages.success(request, "Un mail pour l'initialisation du mot de passe a été envoyé")
redirect("/")
return form({'userform': userform}, 'users/user.html', request)
def process(request, token):
valid_reqs = Request.objects.filter(expires_at__gt=timezone.now())
req = get_object_or_404(valid_reqs, token=token)
if req.type == Request.PASSWD:
return process_passwd(request, req)
elif req.type == Request.EMAIL:
return process_email(request, req=req)
else:
2016-07-20 10:06:33 +00:00
messages.error(request, "Entrée incorrecte, contactez un admin")
redirect("/")
def process_passwd(request, req):
u_form = PassForm(request.POST or None)
user = req.user
if u_form.is_valid():
return password_change_action(u_form, user, request, req=req)
return form({'userform': u_form}, 'users/user.html', request)