Commit 42c364fb authored by LEVY-FALK Hugo's avatar LEVY-FALK Hugo Committed by root

Utilisation de reverse dans les vues.

parent 0efe0c27
......@@ -37,6 +37,7 @@ nombre d'objets par models, nombre d'actions par user, etc
from __future__ import unicode_literals
from django.urls import reverse
from django.shortcuts import render, redirect
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.contrib import messages
......@@ -169,7 +170,7 @@ def revert_action(request, revision_id):
if request.method == "POST":
revision.revert()
messages.success(request, "L'action a été supprimée")
return redirect("/logs/")
return redirect(reverse('logs:index'))
return form({
'objet': revision,
'objet_name': revision.__class__.__name__
......
This diff is collapsed.
......@@ -30,6 +30,7 @@ topologie, users, service...)
from __future__ import unicode_literals
from django.urls import reverse
from django.shortcuts import render, redirect
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.contrib import messages
......@@ -93,7 +94,7 @@ def edit_options(request, section):
)
)
messages.success(request, "Préférences modifiées")
return redirect("/preferences/")
return redirect(reverse('preferences:display-options'))
return form(
{'options': options},
'preferences/edit_preferences.html',
......@@ -101,7 +102,7 @@ def edit_options(request, section):
)
else:
messages.error(request, "Objet inconnu")
return redirect("/preferences/")
return redirect(reverse('preferences:display-options'))
@login_required
......@@ -115,7 +116,7 @@ def add_services(request):
reversion.set_user(request.user)
reversion.set_comment("Création")
messages.success(request, "Cet enregistrement ns a été ajouté")
return redirect("/preferences/")
return redirect(reverse('preferences:display-options'))
return form(
{'preferenceform': services},
'preferences/preferences.html',
......@@ -131,7 +132,7 @@ def edit_services(request, servicesid):
services_instance = Service.objects.get(pk=servicesid)
except Service.DoesNotExist:
messages.error(request, u"Entrée inexistante")
return redirect("/preferences/")
return redirect(reverse('preferences:display-options'))
services = ServiceForm(request.POST or None, instance=services_instance)
if services.is_valid():
with transaction.atomic(), reversion.create_revision():
......@@ -143,7 +144,7 @@ def edit_services(request, servicesid):
)
)
messages.success(request, "Service modifié")
return redirect("/preferences/")
return redirect(reverse('preferences:display-options'))
return form(
{'preferenceform': services},
'preferences/preferences.html',
......@@ -167,7 +168,7 @@ def del_services(request):
except ProtectedError:
messages.error(request, "Erreur le service\
suivant %s ne peut être supprimé" % services_del)
return redirect("/preferences/")
return redirect(reverse('preferences:display-options'))
return form(
{'preferenceform': services},
'preferences/preferences.html',
......@@ -185,7 +186,7 @@ def history(request, object_name, object_id):
object_instance = Service.objects.get(pk=object_id)
except Service.DoesNotExist:
messages.error(request, "Service inexistant")
return redirect("/preferences/")
return redirect(reverse('preferences:display-options'))
options, _created = GeneralOption.objects.get_or_create()
pagination_number = options.pagination_number
reversions = Version.objects.get_for_object(object_instance)
......
......@@ -35,6 +35,7 @@ coté models et forms de topologie
"""
from __future__ import unicode_literals
from django.urls import reverse
from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth.decorators import login_required, permission_required
......@@ -84,28 +85,28 @@ def history(request, object_name, object_id):
object_instance = Switch.objects.get(pk=object_id)
except Switch.DoesNotExist:
messages.error(request, "Switch inexistant")
return redirect("/topologie/")
return redirect(reverse('topologie:index'))
elif object_name == 'port':
try:
object_instance = Port.objects.get(pk=object_id)
except Port.DoesNotExist:
messages.error(request, "Port inexistant")
return redirect("/topologie/")
return redirect(reverse('topologie:index'))
elif object_name == 'room':
try:
object_instance = Room.objects.get(pk=object_id)
except Room.DoesNotExist:
messages.error(request, "Chambre inexistante")
return redirect("/topologie/")
return redirect(reverse('topologie:index'))
elif object_name == 'stack':
try:
object_instance = Stack.objects.get(pk=object_id)
except Room.DoesNotExist:
messages.error(request, "Stack inexistante")
return redirect("/topologie/")
return redirect(reverse('topologie:index'))
else:
messages.error(request, "Objet inconnu")
return redirect("/topologie/")
return redirect(reverse('topologie:index'))
options, _created = GeneralOption.objects.get_or_create()
pagination_number = options.pagination_number
reversions = Version.objects.get_for_object(object_instance)
......@@ -133,7 +134,7 @@ def index_port(request, switch_id):
switch = Switch.objects.get(pk=switch_id)
except Switch.DoesNotExist:
messages.error(request, u"Switch inexistant")
return redirect("/topologie/")
return redirect(reverse('topologie:index'))
port_list = Port.objects.filter(switch=switch)\
.select_related('room')\
.select_related('machine_interface__domain__extension')\
......@@ -206,7 +207,7 @@ def new_port(request, switch_id):
switch = Switch.objects.get(pk=switch_id)
except Switch.DoesNotExist:
messages.error(request, u"Switch inexistant")
return redirect("/topologie/")
return redirect(reverse('topologie:index'))
port = AddPortForm(request.POST or None)
if port.is_valid():
port = port.save(commit=False)
......@@ -219,7 +220,10 @@ def new_port(request, switch_id):
messages.success(request, "Port ajouté")
except IntegrityError:
messages.error(request, "Ce port existe déjà")
return redirect("/topologie/switch/" + switch_id)
return redirect(reverse(
'topologie:index-port',
kwargs={'switch_id':switch_id}
))
return form({'topoform': port}, 'topologie/topo.html', request)
......@@ -238,7 +242,7 @@ def edit_port(request, port_id):
.get(pk=port_id)
except Port.DoesNotExist:
messages.error(request, u"Port inexistant")
return redirect("/topologie/")
return redirect(reverse('topologie:index'))
port = EditPortForm(request.POST or None, instance=port_object)
if port.is_valid():
with transaction.atomic(), reversion.create_revision():
......@@ -248,7 +252,10 @@ def edit_port(request, port_id):
field for field in port.changed_data
))
messages.success(request, "Le port a bien été modifié")
return redirect("/topologie/switch/" + str(port_object.switch.id))
return redirect(reverse(
'topologie:index-port',
kwargs={'switch_id': str(port_object.switch.id)}
))
return form({'topoform': port}, 'topologie/topo.html', request)
......@@ -260,7 +267,7 @@ def del_port(request, port_id):
port = Port.objects.get(pk=port_id)
except Port.DoesNotExist:
messages.error(request, u"Port inexistant")
return redirect('/topologie/')
return redirect(reverse('topologie:index'))
if request.method == "POST":
try:
with transaction.atomic(), reversion.create_revision():
......@@ -271,7 +278,10 @@ def del_port(request, port_id):
except ProtectedError:
messages.error(request, "Le port %s est affecté à un autre objet,\
impossible de le supprimer" % port)
return redirect('/topologie/switch/' + str(port.switch.id))
return redirect(reverse(
'topologie:index-port',
kwargs={'switch_id':str(port.switch.id)
))
return form({'objet': port}, 'topologie/delete.html', request)
......@@ -297,7 +307,7 @@ def edit_stack(request, stack_id):
stack = Stack.objects.get(pk=stack_id)
except Stack.DoesNotExist:
messages.error(request, u"Stack inexistante")
return redirect('/topologie/index_stack/')
return redirect(reverse('topologie:index-stack'))
stack = StackForm(request.POST or None, instance=stack)
if stack.is_valid():
with transaction.atomic(), reversion.create_revision():
......@@ -308,7 +318,7 @@ def edit_stack(request, stack_id):
field for field in stack.changed_data
)
)
return redirect('/topologie/index_stack')
return redirect(reverse('topologie:index-stack'))
return form({'topoform': stack}, 'topologie/topo.html', request)
......@@ -320,7 +330,7 @@ def del_stack(request, stack_id):
stack = Stack.objects.get(pk=stack_id)
except Stack.DoesNotExist:
messages.error(request, u"Stack inexistante")
return redirect('/topologie/index_stack')
return redirect(reverse('topologie:index-stack'))
if request.method == "POST":
try:
with transaction.atomic(), reversion.create_revision():
......@@ -331,7 +341,7 @@ def del_stack(request, stack_id):
except ProtectedError:
messages.error(request, "La stack %s est affectée à un autre\
objet, impossible de la supprimer" % stack)
return redirect('/topologie/index_stack')
return redirect(reverse('topologie:index-stack'))
return form({'objet': stack}, 'topologie/delete.html', request)
......@@ -343,7 +353,7 @@ def edit_switchs_stack(request, stack_id):
stack = Stack.objects.get(pk=stack_id)
except Stack.DoesNotExist:
messages.error(request, u"Stack inexistante")
return redirect('/topologie/index_stack')
return redirect(reverse('topologie:index-stack'))
if request.method == "POST":
pass
else:
......@@ -373,7 +383,7 @@ def new_switch(request):
if not user:
messages.error(request, "L'user association n'existe pas encore,\
veuillez le créer ou le linker dans preferences")
return redirect("/topologie/")
return redirect(reverse('topologie:index'))
new_machine = machine.save(commit=False)
new_machine.user = user
new_interface = interface.save(commit=False)
......@@ -399,7 +409,7 @@ def new_switch(request):
reversion.set_user(request.user)
reversion.set_comment("Création")
messages.success(request, "Le switch a été créé")
return redirect("/topologie/")
return redirect(reverse('topologie:index'))
i_mbf_param = generate_ipv4_mbf_param( interface, False )
return form({
'topoform': switch,
......@@ -419,7 +429,7 @@ def edit_switch(request, switch_id):
switch = Switch.objects.get(pk=switch_id)
except Switch.DoesNotExist:
messages.error(request, u"Switch inexistant")
return redirect("/topologie/")
return redirect(reverse('topologie:index'))
switch_form = EditSwitchForm(request.POST or None, instance=switch)
machine_form = EditMachineForm(
request.POST or None,
......@@ -466,7 +476,7 @@ def edit_switch(request, switch_id):
field for field in switch_form.changed_data)
)
messages.success(request, "Le switch a bien été modifié")
return redirect("/topologie/")
return redirect(reverse('topologie'))
i_mbf_param = generate_ipv4_mbf_param( interface_form, False )
return form({
'topoform': switch_form,
......@@ -488,7 +498,7 @@ def new_room(request):
reversion.set_user(request.user)
reversion.set_comment("Création")
messages.success(request, "La chambre a été créé")
return redirect("/topologie/index_room/")
return redirect(reverse('topologie:index-room'))
return form({'topoform': room}, 'topologie/topo.html', request)
......@@ -500,7 +510,7 @@ def edit_room(request, room_id):
room = Room.objects.get(pk=room_id)
except Room.DoesNotExist:
messages.error(request, u"Chambre inexistante")
return redirect("/topologie/index_room/")
return redirect(reverse('topologie:index-room'))
room = EditRoomForm(request.POST or None, instance=room)
if room.is_valid():
with transaction.atomic(), reversion.create_revision():
......@@ -510,7 +520,7 @@ def edit_room(request, room_id):
field for field in room.changed_data)
)
messages.success(request, "La chambre a bien été modifiée")
return redirect("/topologie/index_room/")
return redirect(reverse('topologie:index-room'))
return form({'topoform': room}, 'topologie/topo.html', request)
......@@ -522,7 +532,7 @@ def del_room(request, room_id):
room = Room.objects.get(pk=room_id)
except Room.DoesNotExist:
messages.error(request, u"Chambre inexistante")
return redirect("/topologie/index_room/")
return redirect(reverse('topologie:index-room'))
if request.method == "POST":
try:
with transaction.atomic(), reversion.create_revision():
......@@ -533,7 +543,7 @@ def del_room(request, room_id):
except ProtectedError:
messages.error(request, "La chambre %s est affectée à un autre objet,\
impossible de la supprimer (switch ou user)" % room)
return redirect("/topologie/index_room/")
return redirect(reverse('topologie:index-room'))
return form({
'objet': room,
'objet_name': 'Chambre'
......
......@@ -35,6 +35,7 @@ des whitelist, des services users et des écoles
from __future__ import unicode_literals
from django.urls import reverse
from django.shortcuts import get_object_or_404, render, redirect
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.contrib import messages
......@@ -77,7 +78,10 @@ def password_change_action(u_form, user, request, req=False):
if req:
req.delete()
return redirect("/")
return redirect("/users/profil/" + str(user.id))
return redirect(reverse(
'users:profil',
kwargs={'userid':str(user.id)}
))
@login_required
......@@ -95,7 +99,10 @@ def new_user(request):
user.reset_passwd_mail(request)
messages.success(request, "L'utilisateur %s a été crée, un mail\
pour l'initialisation du mot de passe a été envoyé" % user.pseudo)
return redirect("/users/profil/" + str(user.id))
return redirect(reverse(
'users:profil',
kwargs={'userid':str(user.id)}
))
return form({'userform': user}, 'users/user.html', request)
......@@ -114,7 +121,10 @@ def new_club(request):
club.reset_passwd_mail(request)
messages.success(request, "L'utilisateur %s a été crée, un mail\
pour l'initialisation du mot de passe a été envoyé" % club.pseudo)
return redirect("/users/profil/" + str(club.id))
return redirect(reverse(
'users:profil',
kwargs={'userid':str(club.id)}
))
return form({'userform': club}, 'users/user.html', request)
......@@ -145,11 +155,14 @@ def edit_info(request, userid):
user = User.objects.get(pk=userid)
except User.DoesNotExist:
messages.error(request, "Utilisateur inexistant")
return redirect("/users/")
return redirect(reverse('users:index'))
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))
return redirect(reverse(
'users:profil',
kwargs={'userid':str(request.user.id)}
))
user = select_user_edit_form(request, user)
if user.is_valid():
with transaction.atomic(), reversion.create_revision():
......@@ -159,7 +172,10 @@ def edit_info(request, userid):
field for field in user.changed_data
))
messages.success(request, "L'user a bien été modifié")
return redirect("/users/profil/" + userid)
return redirect(reverse(
'users:profil',
kwargs={'userid':str(userid)}
))
return form({'userform': user}, 'users/user.html', request)
......@@ -172,7 +188,7 @@ def state(request, userid):
user = User.objects.get(pk=userid)
except User.DoesNotExist:
messages.error(request, "Utilisateur inexistant")
return redirect("/users/")
return redirect(reverse('users:index'))
state = StateForm(request.POST or None, instance=user)
if state.is_valid():
with transaction.atomic(), reversion.create_revision():
......@@ -188,7 +204,10 @@ def state(request, userid):
))
user.save()
messages.success(request, "Etat changé avec succès")
return redirect("/users/profil/" + userid)
return redirect(reverse(
'users:profil',
kwargs={'userid':str(userid)}
))
return form({'userform': state}, 'users/user.html', request)
......@@ -201,16 +220,22 @@ def password(request, userid):
user = User.objects.get(pk=userid)
except User.DoesNotExist:
messages.error(request, "Utilisateur inexistant")
return redirect("/users/")
return redirect(reverse('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))
return redirect(reverse(
'users:profil',
kwargs={'userid':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))
return redirect(reverse(
'users:profil',
kwargs={'userid':str(request.user.id)}
))
u_form = PassForm(request.POST or None)
if u_form.is_valid():
return password_change_action(u_form, user, request)
......@@ -233,7 +258,7 @@ def new_serviceuser(request):
request,
"L'utilisateur %s a été crée" % user_object.pseudo
)
return redirect("/users/index_serviceusers/")
return redirect(reverse('users:index-serviceusers'))
return form({'userform': user}, 'users/user.html', request)
......@@ -247,7 +272,7 @@ def edit_serviceuser(request, userid):
user = ServiceUser.objects.get(pk=userid)
except ServiceUser.DoesNotExist:
messages.error(request, "Utilisateur inexistant")
return redirect("/users/")
return redirect(reverse('users:index'))
user = EditServiceUserForm(request.POST or None, instance=user)
if user.is_valid():
user_object = user.save(commit=False)
......@@ -260,7 +285,7 @@ def edit_serviceuser(request, userid):
field for field in user.changed_data
))
messages.success(request, "L'user a bien été modifié")
return redirect("/users/index_serviceusers")
return redirect(reverse('users:index-serviceusers'))
return form({'userform': user}, 'users/user.html', request)
......@@ -272,13 +297,13 @@ def del_serviceuser(request, userid):
user = ServiceUser.objects.get(pk=userid)
except ServiceUser.DoesNotExist:
messages.error(request, u"Utilisateur inexistant")
return redirect("/users/")
return redirect(reverse('users:index'))
if request.method == "POST":
with transaction.atomic(), reversion.create_revision():
user.delete()
reversion.set_user(request.user)
messages.success(request, "L'user a été détruite")
return redirect("/users/index_serviceusers/")
return redirect(reverse('users:index-serviceusers'))
return form(
{'objet': user, 'objet_name': 'serviceuser'},
'users/delete.html',
......@@ -294,7 +319,7 @@ def add_right(request, userid):
user = User.objects.get(pk=userid)
except User.DoesNotExist:
messages.error(request, "Utilisateur inexistant")
return redirect("/users/")
return redirect(reverse('users:index'))
right = RightForm(request.POST or None)
if right.is_valid():
right = right.save(commit=False)
......@@ -307,7 +332,10 @@ def add_right(request, userid):
messages.success(request, "Droit ajouté")
except IntegrityError:
pass
return redirect("/users/profil/" + userid)
return redirect(reverse(
'users:profil',
kwargs={'userid':str(userid)}
))
return form({'userform': right}, 'users/user.html', request)
......@@ -328,7 +356,7 @@ def del_right(request):
))
right_del.delete()
messages.success(request, "Droit retiré avec succès")
return redirect("/users/")
return redirect(reverse('users:index'))
return form({'userform': user_right_list}, 'users/del_right.html', request)
......@@ -342,7 +370,7 @@ def add_ban(request, userid):
user = User.objects.get(pk=userid)
except User.DoesNotExist:
messages.error(request, "Utilisateur inexistant")
return redirect("/users/")
return redirect(reverse('users:index'))
ban_instance = Ban(user=user)
ban = BanForm(request.POST or None, instance=ban_instance)
if ban.is_valid():
......@@ -351,7 +379,10 @@ def add_ban(request, userid):
reversion.set_user(request.user)
reversion.set_comment("Création")
messages.success(request, "Bannissement ajouté")
return redirect("/users/profil/" + userid)
return redirect(reverse(
'users:profil',
kwargs={'userid':str(userid)}
))
if user.is_ban:
messages.error(
request,
......@@ -370,7 +401,7 @@ def edit_ban(request, banid):
ban_instance = Ban.objects.get(pk=banid)
except Ban.DoesNotExist:
messages.error(request, "Entrée inexistante")
return redirect("/users/")
return redirect(reverse('users:index'))
ban = BanForm(request.POST or None, instance=ban_instance)
if ban.is_valid():
with transaction.atomic(), reversion.create_revision():
......@@ -380,7 +411,7 @@ def edit_ban(request, banid):
field for field in ban.changed_data
))
messages.success(request, "Bannissement modifié")
return redirect("/users/")
return redirect(reverse('users:index'))
return form({'userform': ban}, 'users/user.html', request)
......@@ -395,7 +426,7 @@ def add_whitelist(request, userid):
user = User.objects.get(pk=userid)
except User.DoesNotExist:
messages.error(request, "Utilisateur inexistant")
return redirect("/users/")
return redirect(reverse('users:index'))
whitelist_instance = Whitelist(user=user)
whitelist = WhitelistForm(
request.POST or None,
......@@ -407,7 +438,10 @@ def add_whitelist(request, userid):
reversion.set_user(request.user)
reversion.set_comment("Création")
messages.success(request, "Accès à titre gracieux accordé")
return redirect("/users/profil/" + userid)
return redirect(reverse(
'users:profil',
kwargs={'userid':str(userid)}
))
if user.is_whitelisted:
messages.error(
request,
......@@ -427,7 +461,7 @@ def edit_whitelist(request, whitelistid):
whitelist_instance = Whitelist.objects.get(pk=whitelistid)
except Whitelist.DoesNotExist:
messages.error(request, "Entrée inexistante")
return redirect("/users/")
return redirect(reverse('users:index'))
whitelist = WhitelistForm(
request.POST or None,
instance=whitelist_instance
......@@ -440,7 +474,7 @@ def edit_whitelist(request, whitelistid):
field for field in whitelist.changed_data
))
messages.success(request, "Whitelist modifiée")
return redirect("/users/")
return redirect(reverse('users:index'))
return form({'userform': whitelist}, 'users/user.html', request)
......@@ -456,7 +490,7 @@ def add_school(request):
reversion.set_user(request.user)
reversion.set_comment("Création")
messages.success(request, "L'établissement a été ajouté")
return redirect("/users/index_school/")
return redirect(reverse('users:index-school'))
return form({'userform': school}, 'users/user.html', request)
......@@ -469,7 +503,7 @@ def edit_school(request, schoolid):
school_instance = School.objects.get(pk=schoolid)
except School.DoesNotExist:
messages.error(request, u"Entrée inexistante")
return redirect("/users/")
return redirect(reverse('users:index'))
school = SchoolForm(request.POST or None, instance=school_instance)
if school.is_valid():
with transaction.atomic(), reversion.create_revision():
......@@ -479,7 +513,7 @@ def edit_school(request, schoolid):
field for field in school.changed_data
))
messages.success(request, "Établissement modifié")
return redirect("/users/index_school/")
return redirect(reverse('users:index-school'))
return form({'userform': school}, 'users/user.html', request)
......@@ -504,7 +538,7 @@ def del_school(request):
request,
"L'établissement %s est affecté à au moins un user, \
vous ne pouvez pas le supprimer" % school_del)
return redirect("/users/index_school/")
return redirect(reverse('users:index-school'))
return form({'userform': school}, 'users/user.html', request)
......@@ -520,7 +554,7 @@ def add_listright(request):
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 redirect(reverse('users:index-listright'))
return form({'userform': listright}, 'users/user.html', request)
......@@ -533,7 +567,7 @@ def edit_listright(request, listrightid):
listright_instance = ListRight.objects.get(pk=listrightid)
except ListRight.DoesNotExist:
messages.error(request, u"Entrée inexistante")
return redirect("/users/")
return redirect(reverse('users:index'))
listright = ListRightForm(
request.POST or None,
instance=listright_instance
......@@ -546,7 +580,7 @@ def edit_listright(request, listrightid):
field for field in listright.changed_data
))
messages.success(request, "Droit modifié")
return redirect("/users/index_listright/")
return redirect(reverse('users:index-listright'))
return form({'userform': listright}, 'users/user.html', request)
......@@ -569,7 +603,7 @@ def del_listright(request):
request,
"L'établissement %s est affecté à au moins un user, \
vous ne pouvez pas le supprimer" % listright_del)
return redirect("/users/index_listright/")
return redirect(reverse('users:index-listright'))
return form({'userform': listright}, 'users/user.html', request)
......@@ -595,7 +629,7 @@ def mass_archive(request):
messages.success(request, "%s users ont été archivés" % len(
to_archive_list
))
return redirect("/users/")