diff --git a/machines/forms.py b/machines/forms.py index 6b3d905e..d54427e1 100644 --- a/machines/forms.py +++ b/machines/forms.py @@ -137,3 +137,14 @@ class DelMxForm(ModelForm): exclude = ['zone', 'priority', 'name'] model = Mx +class NsForm(ModelForm): + class Meta: + model = Ns + fields = ['zone', 'interface'] + +class DelNsForm(ModelForm): + ns = forms.ModelMultipleChoiceField(queryset=Ns.objects.all(), label="Enregistrements NS actuels", widget=forms.CheckboxSelectMultiple) + + class Meta: + exclude = ['zone', 'interface'] + model = Ns diff --git a/machines/models.py b/machines/models.py index bf1eca2e..0ff636c2 100644 --- a/machines/models.py +++ b/machines/models.py @@ -64,6 +64,9 @@ class Ns(models.Model): zone = models.ForeignKey('Extension', on_delete=models.PROTECT) interface = models.OneToOneField('Interface', on_delete=models.PROTECT) + def __str__(self): + return str(self.zone) + ' ' + str(self.interface) + class Interface(models.Model): PRETTY_NAME = "Interface" diff --git a/machines/templates/machines/aff_ns.html b/machines/templates/machines/aff_ns.html new file mode 100644 index 00000000..56ba3b3e --- /dev/null +++ b/machines/templates/machines/aff_ns.html @@ -0,0 +1,24 @@ + + + + + + + + + + {% for ns in ns_list %} + + + + + + {% endfor %} +
Zone concernéeInterface autoritaire de la zone
{{ ns.zone }}{{ ns.interface }} + {% if is_infra %} + {% include 'buttons/edit.html' with href='machines:edit-ns' id=ns.id %} + {% endif %} + {% include 'buttons/history.html' with href='machines:history' name='ns' id=ns.id %} +
+ + diff --git a/machines/templates/machines/index_extension.html b/machines/templates/machines/index_extension.html index d058f805..f47b05d6 100644 --- a/machines/templates/machines/index_extension.html +++ b/machines/templates/machines/index_extension.html @@ -17,7 +17,13 @@ Supprimer un enregistrement MX {% endif %} {% include "machines/aff_mx.html" with mx_list=mx_list %} -
+

Liste des enregistrements NS

+ {% if is_infra %} + Ajouter un enregistrement NS + Supprimer un enregistrement NS + {% endif %} + {% include "machines/aff_ns.html" with ns_list=ns_list %} +


{% endblock %} diff --git a/machines/urls.py b/machines/urls.py index e3f47578..b93c0373 100644 --- a/machines/urls.py +++ b/machines/urls.py @@ -23,12 +23,16 @@ urlpatterns = [ url(r'^add_mx/$', views.add_mx, name='add-mx'), url(r'^edit_mx/(?P[0-9]+)$', views.edit_mx, name='edit-mx'), url(r'^del_mx/$', views.del_mx, name='del-mx'), + url(r'^add_ns/$', views.add_ns, name='add-ns'), + url(r'^edit_ns/(?P[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'^history/(?Pmachine)/(?P[0-9]+)$', views.history, name='history'), url(r'^history/(?Pinterface)/(?P[0-9]+)$', views.history, name='history'), url(r'^history/(?Pmachinetype)/(?P[0-9]+)$', views.history, name='history'), url(r'^history/(?Pextension)/(?P[0-9]+)$', views.history, name='history'), url(r'^history/(?Pmx)/(?P[0-9]+)$', views.history, name='history'), + url(r'^history/(?Pns)/(?P[0-9]+)$', views.history, name='history'), url(r'^history/(?Piptype)/(?P[0-9]+)$', views.history, name='history'), url(r'^$', views.index, name='index'), url(r'^rest/mac-ip/$', views.mac_ip, name='mac-ip'), diff --git a/machines/views.py b/machines/views.py index 8dd15711..3e5a25aa 100644 --- a/machines/views.py +++ b/machines/views.py @@ -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, MxForm, DelMxForm +from .forms import IpTypeForm, DelIpTypeForm, NewAliasForm, EditAliasForm, 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 @@ -417,6 +417,54 @@ def del_mx(request): return redirect("/machines/index_extension") return form({'machineform': mx, 'interfaceform': None}, 'machines/machine.html', request) +@login_required +@permission_required('infra') +def add_ns(request): + ns = NsForm(request.POST or None) + if ns.is_valid(): + with transaction.atomic(), reversion.create_revision(): + ns.save() + reversion.set_user(request.user) + reversion.set_comment("Création") + messages.success(request, "Cet enregistrement ns a été ajouté") + return redirect("/machines/index_extension") + return form({'machineform': ns, 'interfaceform': None}, 'machines/machine.html', request) + +@login_required +@permission_required('infra') +def edit_ns(request, nsid): + try: + ns_instance = Ns.objects.get(pk=nsid) + except Ns.DoesNotExist: + messages.error(request, u"Entrée inexistante" ) + return redirect("/machines/index_extension/") + ns = NsForm(request.POST or None, instance=ns_instance) + if ns.is_valid(): + with transaction.atomic(), reversion.create_revision(): + ns.save() + reversion.set_user(request.user) + reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in ns.changed_data)) + messages.success(request, "Ns modifié") + return redirect("/machines/index_extension/") + return form({'machineform': ns}, 'machines/machine.html', request) + +@login_required +@permission_required('infra') +def del_ns(request): + ns = DelNsForm(request.POST or None) + if ns.is_valid(): + ns_dels = ns.cleaned_data['ns'] + for ns_del in ns_dels: + try: + with transaction.atomic(), reversion.create_revision(): + ns_del.delete() + reversion.set_user(request.user) + messages.success(request, "Le ns a été supprimée") + except ProtectedError: + messages.error(request, "Erreur le Ns suivant %s ne peut être supprimé" % ns_del) + return redirect("/machines/index_extension") + return form({'machineform': ns, 'interfaceform': None}, 'machines/machine.html', request) + @login_required @permission_required('cableur') def index(request): @@ -497,6 +545,12 @@ def history(request, object, id): except Mx.DoesNotExist: messages.error(request, "Mx inexistant") return redirect("/machines/") + elif object == 'ns' and request.user.has_perms(('cableur',)): + try: + object_instance = Ns.objects.get(pk=id) + except Ns.DoesNotExist: + messages.error(request, "Ns inexistant") + return redirect("/machines/") else: messages.error(request, "Objet inconnu") return redirect("/machines/")