mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-11-22 03:13:12 +00:00
Gestion des alias avec un menu dans re2o
This commit is contained in:
parent
145c64691c
commit
5c2ee98b8a
7 changed files with 142 additions and 23 deletions
|
@ -59,17 +59,18 @@ class BaseEditInterfaceForm(EditInterfaceForm):
|
|||
self.fields['type'].queryset = MachineType.objects.filter(ip_type=IpType.objects.filter(need_infra=False))
|
||||
self.fields['ipv4'].queryset = IpList.objects.filter(ip_type=IpType.objects.filter(need_infra=False))
|
||||
|
||||
class NewAliasForm(ModelForm):
|
||||
class AliasForm(ModelForm):
|
||||
class Meta:
|
||||
model = Alias
|
||||
fields = ['alias','extension']
|
||||
|
||||
class EditAliasForm(NewAliasForm):
|
||||
class Meta(NewAliasForm.Meta):
|
||||
fields = ['alias','extension']
|
||||
|
||||
class DelAliasForm(ModelForm):
|
||||
del_alias = forms.ModelMultipleChoiceField(queryset=Alias.objects.all(), label="Alias actuels", widget=forms.CheckboxSelectMultiple)
|
||||
alias = forms.ModelMultipleChoiceField(queryset=Alias.objects.all(), label="Alias actuels", widget=forms.CheckboxSelectMultiple)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
interface = kwargs.pop('interface')
|
||||
super(DelAliasForm, self).__init__(*args, **kwargs)
|
||||
self.fields['alias'].queryset = Alias.objects.filter(interface_parent=interface)
|
||||
|
||||
class Meta:
|
||||
exclude = ['interface_parent', 'alias', 'extension']
|
||||
|
|
|
@ -108,8 +108,9 @@ class Alias(models.Model):
|
|||
unique_together = ("alias", "extension")
|
||||
|
||||
def clean(self, *args, **kwargs):
|
||||
if Interface.objects.filter(dns=self.alias).filter(ipv4=IpList.objects.filter(ip_type=IpType.objects.filter(extension=self.extension))):
|
||||
raise ValidationError("Impossible d'ajouter l'alias, déjà utilisé par une machine")
|
||||
if hasattr(self, 'alias') and hasattr(self, 'extension'):
|
||||
if Interface.objects.filter(dns=self.alias).filter(ipv4=IpList.objects.filter(ip_type=IpType.objects.filter(extension=self.extension))):
|
||||
raise ValidationError("Impossible d'ajouter l'alias, déjà utilisé par une machine")
|
||||
|
||||
def __str__(self):
|
||||
return str(self.alias) + str(self.extension)
|
||||
|
|
18
machines/templates/machines/aff_alias.html
Normal file
18
machines/templates/machines/aff_alias.html
Normal file
|
@ -0,0 +1,18 @@
|
|||
<table class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Alias</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
{% for alias in alias_list %}
|
||||
<tr>
|
||||
<td>{{ alias }}</td>
|
||||
<td class="text-right">
|
||||
{% include 'buttons/edit.html' with href='machines:edit-alias' id=alias.id %}
|
||||
{% include 'buttons/history.html' with href='machines:history' name='alias' id=alias.id %}
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
|
|
@ -63,6 +63,11 @@
|
|||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="{% url 'machines:index-alias' interface.id %}">
|
||||
<i class="glyphicon glyphicon-edit"></i> Gerer les alias
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="{% url 'machines:history' 'interface' interface.id %}">
|
||||
<i class="glyphicon glyphicon-time"></i> Historique
|
||||
</a>
|
||||
|
|
15
machines/templates/machines/index_alias.html
Normal file
15
machines/templates/machines/index_alias.html
Normal file
|
@ -0,0 +1,15 @@
|
|||
{% extends "machines/sidebar.html" %}
|
||||
{% load bootstrap3 %}
|
||||
|
||||
{% block title %}Machines{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h2>Liste des alias de l'interface</h2>
|
||||
<a class="btn btn-primary btn-sm" role="button" href="{% url 'machines:add-alias' interface_id %}"><i class="glyphicon glyphicon-plus"></i> Ajouter un alias</a>
|
||||
<a class="btn btn-danger btn-sm" role="button" href="{% url 'machines:del-alias' interface_id %}"><i class="glyphicon glyphicon-trash"></i> Supprimer un ou plusieurs alias</a>
|
||||
{% include "machines/aff_alias.html" with alias_list=alias_list %}
|
||||
<br />
|
||||
<br />
|
||||
<br />
|
||||
{% endblock %}
|
||||
|
|
@ -8,7 +8,6 @@ urlpatterns = [
|
|||
url(r'^del_machine/(?P<machineid>[0-9]+)$', views.del_machine, name='del-machine'),
|
||||
url(r'^new_interface/(?P<machineid>[0-9]+)$', views.new_interface, name='new-interface'),
|
||||
url(r'^del_interface/(?P<interfaceid>[0-9]+)$', views.del_interface, name='del-interface'),
|
||||
url(r'^manage_alias/(?P<interfaceid>[0-9]+)$', views.manage_alias, name='manage-alias'),
|
||||
url(r'^add_machinetype/$', views.add_machinetype, name='add-machinetype'),
|
||||
url(r'^edit_machinetype/(?P<machinetypeid>[0-9]+)$', views.edit_machinetype, name='edit-machinetype'),
|
||||
url(r'^del_machinetype/$', views.del_machinetype, name='del-machinetype'),
|
||||
|
@ -27,6 +26,10 @@ urlpatterns = [
|
|||
url(r'^edit_ns/(?P<nsid>[0-9]+)$', views.edit_ns, name='edit-ns'),
|
||||
url(r'^del_ns/$', views.del_ns, name='del-ns'),
|
||||
url(r'^index_extension/$', views.index_extension, name='index-extension'),
|
||||
url(r'^add_alias/(?P<interfaceid>[0-9]+)$', views.add_alias, name='add-alias'),
|
||||
url(r'^edit_alias/(?P<aliasid>[0-9]+)$', views.edit_alias, name='edit-alias'),
|
||||
url(r'^del_alias/(?P<interfaceid>[0-9]+)$', views.del_alias, name='del-alias'),
|
||||
url(r'^index_alias/(?P<interfaceid>[0-9]+)$', views.index_alias, name='index-alias'),
|
||||
url(r'^history/(?P<object>machine)/(?P<id>[0-9]+)$', views.history, name='history'),
|
||||
url(r'^history/(?P<object>interface)/(?P<id>[0-9]+)$', views.history, name='history'),
|
||||
url(r'^history/(?P<object>machinetype)/(?P<id>[0-9]+)$', views.history, name='history'),
|
||||
|
@ -34,6 +37,7 @@ urlpatterns = [
|
|||
url(r'^history/(?P<object>mx)/(?P<id>[0-9]+)$', views.history, name='history'),
|
||||
url(r'^history/(?P<object>ns)/(?P<id>[0-9]+)$', views.history, name='history'),
|
||||
url(r'^history/(?P<object>iptype)/(?P<id>[0-9]+)$', views.history, name='history'),
|
||||
url(r'^history/(?P<object>alias)/(?P<id>[0-9]+)$', views.history, name='history'),
|
||||
url(r'^$', views.index, name='index'),
|
||||
url(r'^rest/mac-ip/$', views.mac_ip, name='mac-ip'),
|
||||
url(r'^rest/login/$', views.login_user, name='login'),
|
||||
|
|
|
@ -22,7 +22,7 @@ from reversion import revisions as reversion
|
|||
|
||||
import re
|
||||
from .forms import NewMachineForm, EditMachineForm, EditInterfaceForm, AddInterfaceForm, MachineTypeForm, DelMachineTypeForm, ExtensionForm, DelExtensionForm, BaseEditInterfaceForm, BaseEditMachineForm, Alias
|
||||
from .forms import IpTypeForm, DelIpTypeForm, NewAliasForm, EditAliasForm, NsForm, DelNsForm, MxForm, DelMxForm
|
||||
from .forms import IpTypeForm, DelIpTypeForm, AliasForm, DelAliasForm, NsForm, DelNsForm, MxForm, DelMxForm
|
||||
from .models import IpType, Machine, Interface, IpList, MachineType, Extension, Mx, Ns
|
||||
from users.models import User
|
||||
from re2o.settings import PAGINATION_NUMBER, PAGINATION_LARGE_NUMBER
|
||||
|
@ -146,19 +146,6 @@ def edit_interface(request, interfaceid):
|
|||
return redirect("/users/profil/" + str(interface.machine.user.id))
|
||||
return form({'machineform': machine_form, 'interfaceform': interface_form}, 'machines/machine.html', request)
|
||||
|
||||
@login_required
|
||||
def manage_alias(request, interfaceid):
|
||||
try:
|
||||
interface = Interface.objects.get(pk=interfaceid)
|
||||
except Interface.DoesNotExist:
|
||||
messages.error(request, u"Interface inexistante" )
|
||||
return redirect("/machines")
|
||||
if not request.user.has_perms(('infra',)):
|
||||
if not request.user.has_perms(('cableur',)) and interface.machine.user != request.user:
|
||||
messages.error(request, "Vous ne pouvez pas éditer une machine d'un autre user que vous sans droit")
|
||||
return redirect("/users/profil/" + str(request.user.id))
|
||||
|
||||
|
||||
@login_required
|
||||
def del_machine(request, machineid):
|
||||
try:
|
||||
|
@ -465,6 +452,72 @@ def del_ns(request):
|
|||
return redirect("/machines/index_extension")
|
||||
return form({'machineform': ns, 'interfaceform': None}, 'machines/machine.html', request)
|
||||
|
||||
@login_required
|
||||
def add_alias(request, interfaceid):
|
||||
try:
|
||||
interface = Interface.objects.get(pk=interfaceid)
|
||||
except Interface.DoesNotExist:
|
||||
messages.error(request, u"Interface inexistante" )
|
||||
return redirect("/machines")
|
||||
if not request.user.has_perms(('cableur',)) and interface.machine.user != request.user:
|
||||
messages.error(request, "Vous ne pouvez pas ajouter un alias à une machine d'un autre user que vous sans droit")
|
||||
return redirect("/users/profil/" + str(request.user.id))
|
||||
alias = AliasForm(request.POST or None)
|
||||
if alias.is_valid():
|
||||
alias = alias.save(commit=False)
|
||||
alias.interface_parent = interface
|
||||
with transaction.atomic(), reversion.create_revision():
|
||||
alias.save()
|
||||
reversion.set_user(request.user)
|
||||
reversion.set_comment("Création")
|
||||
messages.success(request, "Cet alias a été ajouté")
|
||||
return redirect("/machines/index_alias/" + str(interfaceid))
|
||||
return form({'machineform': alias, 'interfaceform': None}, 'machines/machine.html', request)
|
||||
|
||||
@login_required
|
||||
def edit_alias(request, aliasid):
|
||||
try:
|
||||
alias_instance = Alias.objects.get(pk=aliasid)
|
||||
except Alias.DoesNotExist:
|
||||
messages.error(request, u"Entrée inexistante" )
|
||||
return redirect("/machines/index_extension/")
|
||||
if not request.user.has_perms(('cableur',)) and alias_instance.interface_parent.machine.user != request.user:
|
||||
messages.error(request, "Vous ne pouvez pas ajouter un alias à une machine d'un autre user que vous sans droit")
|
||||
return redirect("/users/profil/" + str(request.user.id))
|
||||
alias = AliasForm(request.POST or None, instance=alias_instance)
|
||||
if alias.is_valid():
|
||||
with transaction.atomic(), reversion.create_revision():
|
||||
alias_instance = alias.save()
|
||||
reversion.set_user(request.user)
|
||||
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in alias.changed_data))
|
||||
messages.success(request, "Alias modifié")
|
||||
return redirect("/machines/index_alias/" + str(alias_instance.interface_parent.id))
|
||||
return form({'machineform': alias}, 'machines/machine.html', request)
|
||||
|
||||
@login_required
|
||||
def del_alias(request, interfaceid):
|
||||
try:
|
||||
interface = Interface.objects.get(pk=interfaceid)
|
||||
except Interface.DoesNotExist:
|
||||
messages.error(request, u"Interface inexistante" )
|
||||
return redirect("/machines")
|
||||
if not request.user.has_perms(('cableur',)) and interface.machine.user != request.user:
|
||||
messages.error(request, "Vous ne pouvez pas ajouter un alias à une machine d'un autre user que vous sans droit")
|
||||
return redirect("/users/profil/" + str(request.user.id))
|
||||
alias = DelAliasForm(request.POST or None, interface=interface)
|
||||
if alias.is_valid():
|
||||
alias_dels = alias.cleaned_data['alias']
|
||||
for alias_del in alias_dels:
|
||||
try:
|
||||
with transaction.atomic(), reversion.create_revision():
|
||||
alias_del.delete()
|
||||
reversion.set_user(request.user)
|
||||
messages.success(request, "L'alias %s a été supprimé" % alias_del)
|
||||
except ProtectedError:
|
||||
messages.error(request, "Erreur l'alias suivant %s ne peut être supprimé" % alias_del)
|
||||
return redirect("/machines/index_alias/" + str(interfaceid))
|
||||
return form({'machineform': alias, 'interfaceform': None}, 'machines/machine.html', request)
|
||||
|
||||
@login_required
|
||||
@permission_required('cableur')
|
||||
def index(request):
|
||||
|
@ -501,6 +554,19 @@ def index_extension(request):
|
|||
ns_list = Ns.objects.order_by('zone')
|
||||
return render(request, 'machines/index_extension.html', {'extension_list':extension_list, 'mx_list': mx_list, 'ns_list': ns_list})
|
||||
|
||||
@login_required
|
||||
def index_alias(request, interfaceid):
|
||||
try:
|
||||
interface = Interface.objects.get(pk=interfaceid)
|
||||
except Interface.DoesNotExist:
|
||||
messages.error(request, u"Interface inexistante" )
|
||||
return redirect("/machines")
|
||||
if not request.user.has_perms(('cableur',)) and interface.machine.user != request.user:
|
||||
messages.error(request, "Vous ne pouvez pas éditer une machine d'un autre user que vous sans droit")
|
||||
return redirect("/users/profil/" + str(request.user.id))
|
||||
alias_list = Alias.objects.filter(interface_parent=interface).order_by('alias')
|
||||
return render(request, 'machines/index_alias.html', {'alias_list':alias_list, 'interface_id': interfaceid})
|
||||
|
||||
@login_required
|
||||
def history(request, object, id):
|
||||
if object == 'machine':
|
||||
|
@ -521,6 +587,15 @@ def history(request, object, id):
|
|||
if not request.user.has_perms(('cableur',)) and object_instance.machine.user != request.user:
|
||||
messages.error(request, "Vous ne pouvez pas afficher l'historique d'une interface d'un autre user que vous sans droit cableur")
|
||||
return redirect("/users/profil/" + str(request.user.id))
|
||||
elif object == 'alias':
|
||||
try:
|
||||
object_instance = Alias.objects.get(pk=id)
|
||||
except Alias.DoesNotExist:
|
||||
messages.error(request, "Alias inexistant")
|
||||
return redirect("/machines/")
|
||||
if not request.user.has_perms(('cableur',)) and object_instance.interface_parent.machine.user != request.user:
|
||||
messages.error(request, "Vous ne pouvez pas afficher l'historique d'un alias d'un autre user que vous sans droit cableur")
|
||||
return redirect("/users/profil/" + str(request.user.id))
|
||||
elif object == 'machinetype' and request.user.has_perms(('cableur',)):
|
||||
try:
|
||||
object_instance = MachineType.objects.get(pk=id)
|
||||
|
|
Loading…
Reference in a new issue