From fc176a60c148ac9c71295811384fee57c218e819 Mon Sep 17 00:00:00 2001 From: Charlie Jacomme Date: Mon, 25 Jun 2018 16:50:45 +0200 Subject: [PATCH] New DNAME model --- machines/admin.py | 6 ++ machines/forms.py | 29 +++++++++ machines/models.py | 32 +++++++++- machines/templates/machines/aff_dname.html | 50 +++++++++++++++ .../templates/machines/index_extension.html | 6 ++ machines/templates/machines/machine.html | 7 ++ machines/urls.py | 3 + machines/views.py | 64 ++++++++++++++++++- re2o/templatetags/acl.py | 1 + 9 files changed, 196 insertions(+), 2 deletions(-) create mode 100644 machines/templates/machines/aff_dname.html diff --git a/machines/admin.py b/machines/admin.py index 0f85007c..eb765748 100644 --- a/machines/admin.py +++ b/machines/admin.py @@ -37,6 +37,7 @@ from .models import ( Ns, Vlan, Txt, + DName, Srv, Nas, Service, @@ -95,6 +96,10 @@ class TxtAdmin(VersionAdmin): """ Admin view of a TXT object """ pass +class DNameAdmin(VersionAdmin): + """ Admin view of a DName object """ + pass + class SrvAdmin(VersionAdmin): """ Admin view of a SRV object """ @@ -144,6 +149,7 @@ admin.site.register(SOA, SOAAdmin) admin.site.register(Mx, MxAdmin) admin.site.register(Ns, NsAdmin) admin.site.register(Txt, TxtAdmin) +admin.site.register(DName, DNameAdmin) admin.site.register(Srv, SrvAdmin) admin.site.register(IpList, IpListAdmin) admin.site.register(Interface, InterfaceAdmin) diff --git a/machines/forms.py b/machines/forms.py index 91df33d4..8ca13311 100644 --- a/machines/forms.py +++ b/machines/forms.py @@ -51,6 +51,7 @@ from .models import ( SOA, Mx, Txt, + DName, Ns, Service, Vlan, @@ -409,6 +410,34 @@ class DelTxtForm(FormRevMixin, Form): else: self.fields['txt'].queryset = Txt.objects.all() +class DNameForm(FormRevMixin, ModelForm): + """Ajout d'un DName pour une zone""" + class Meta: + model = DName + fields = '__all__' + + def __init__(self, *args, **kwargs): + prefix = kwargs.pop('prefix', self.Meta.model.__name__) + super(DNameForm, self).__init__(*args, prefix=prefix, **kwargs) + + +class DelDNameForm(FormRevMixin, Form): + """Suppression d'un ou plusieurs DName""" + dnames = forms.ModelMultipleChoiceField( + queryset=Txt.objects.none(), + label="Enregistrements DName actuels", + widget=forms.CheckboxSelectMultiple + ) + + def __init__(self, *args, **kwargs): + instances = kwargs.pop('instances', None) + super(DelDNameForm, self).__init__(*args, **kwargs) + if instances: + self.fields['dnames'].queryset = instances + else: + self.fields['dnames'].queryset = DName.objects.all() + + class SrvForm(FormRevMixin, ModelForm): """Ajout d'un srv pour une zone""" diff --git a/machines/models.py b/machines/models.py index fd4999d6..e73c1fb6 100644 --- a/machines/models.py +++ b/machines/models.py @@ -670,6 +670,27 @@ class Txt(RevMixin, AclMixin, models.Model): return str(self.field1).ljust(15) + " IN TXT " + str(self.field2) +class DName(RevMixin, AclMixin, models.Model): + """ Un enregistrement DName, qui crée un alias depuis la zone spécifié vers l'extension""" + PRETTY_NAME = "Enregistrement DName" + + zone = models.ForeignKey('Extension', on_delete=models.PROTECT) + alias = models.CharField(max_length=255) + + class Meta: + permissions = ( + ("view_dname", "Peut voir un objet dname"), + ) + + def __str__(self): + return str(self.zone) + " : " + str(self.alias) + + @cached_property + def dns_entry(self): + """Renvoie l'enregisterment DNAME complet pour le fichier de zone""" + return str(self.alias) + " IN DNAME " + str(self.zone) + + class Srv(RevMixin, AclMixin, models.Model): """ A SRV record """ PRETTY_NAME = "Enregistrement Srv" @@ -1680,12 +1701,21 @@ def text_post_save(**_kwargs): """Regeneration dns après modification d'un TXT""" regen('dns') - @receiver(post_delete, sender=Txt) def text_post_delete(**_kwargs): """Regeneration dns après modification d'un TX""" regen('dns') +@receiver(post_save, sender=DName) +def dname_post_save(**_kwargs): + """Regeneration dns après modification d'un DName""" + regen('dns') + +@receiver(post_delete, sender=DName) +def DName_post_delete(**_kwargs): + """Regeneration dns après modification d'un DName""" + regen('dns') + @receiver(post_save, sender=Srv) def srv_post_save(**_kwargs): diff --git a/machines/templates/machines/aff_dname.html b/machines/templates/machines/aff_dname.html new file mode 100644 index 00000000..de51aad8 --- /dev/null +++ b/machines/templates/machines/aff_dname.html @@ -0,0 +1,50 @@ +{% comment %} +Re2o est un logiciel d'administration développé initiallement au rezometz. Il +se veut agnostique au réseau considéré, de manière à être installable en +quelques clics. + +Copyright © 2017 Gabriel Détraz +Copyright © 2017 Goulven Kermarec +Copyright © 2017 Augustin Lemesle + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +{% endcomment %} + +{% load acl %} + + + + + + + + + + + {% for dname in dname_list %} + + + + + + {% endfor %} +
Zone concernéeEnregistrement
{{ dname.zone }}{{ dname.dns_entry }} + {% can_edit dname %} + {% include 'buttons/edit.html' with href='machines:edit-dname' id=dname.id %} + {% acl_end %} + {% include 'buttons/history.html' with href='machines:history' name='dname' id=dname.id %} +
+ + diff --git a/machines/templates/machines/index_extension.html b/machines/templates/machines/index_extension.html index 6ee1bdff..3ec25767 100644 --- a/machines/templates/machines/index_extension.html +++ b/machines/templates/machines/index_extension.html @@ -61,6 +61,12 @@ with this program; if not, write to the Free Software Foundation, Inc., {% acl_end %} Supprimer un enregistrement TXT {% include "machines/aff_txt.html" with txt_list=txt_list %} +

Liste des enregistrements DNAME

+ {% can_create DName %} + Ajouter un enregistrement DNAME + {% acl_end %} + Supprimer un enregistrement DNAME + {% include "machines/aff_dname.html" with dname_list=dname_list %}

Liste des enregistrements SRV

{% can_create Srv %} Ajouter un enregistrement SRV diff --git a/machines/templates/machines/machine.html b/machines/templates/machines/machine.html index 0e7082f8..20dee58a 100644 --- a/machines/templates/machines/machine.html +++ b/machines/templates/machines/machine.html @@ -54,6 +54,9 @@ with this program; if not, write to the Free Software Foundation, Inc., {% if nsform %} {% bootstrap_form_errors nsform %} {% endif %} +{% if dnameform %} + {% bootstrap_form_errors dnameform %} +{% endif %} {% if txtform %} {% bootstrap_form_errors txtform %} {% endif %} @@ -122,6 +125,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,

Enregistrement TXT

{% bootstrap_form txtform %} {% endif %} + {% if dnameform %} +

Enregistrement DName

+ {% bootstrap_form dnameform %} + {% endif %} {% if srvform %}

Enregistrement SRV

{% massive_bootstrap_form srvform 'target' %} diff --git a/machines/urls.py b/machines/urls.py index 670c3ce6..bf3d63d8 100644 --- a/machines/urls.py +++ b/machines/urls.py @@ -73,6 +73,9 @@ urlpatterns = [ url(r'^add_txt/$', views.add_txt, name='add-txt'), url(r'^edit_txt/(?P[0-9]+)$', views.edit_txt, name='edit-txt'), url(r'^del_txt/$', views.del_txt, name='del-txt'), + url(r'^add_dname/$', views.add_dname, name='add-dname'), + url(r'^edit_dname/(?P[0-9]+)$', views.edit_dname, name='edit-dname'), + url(r'^del_dname/$', views.del_dname, name='del-dname'), 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'), diff --git a/machines/views.py b/machines/views.py index 75c2f483..70274356 100644 --- a/machines/views.py +++ b/machines/views.py @@ -93,6 +93,8 @@ from .forms import ( DelNsForm, TxtForm, DelTxtForm, + DNameForm, + DelDNameForm, MxForm, DelMxForm, VlanForm, @@ -122,6 +124,7 @@ from .models import ( Vlan, Nas, Txt, + DName, Srv, OuverturePortList, OuverturePort, @@ -815,6 +818,63 @@ def del_ns(request, instances): request ) +@login_required +@can_create(DName) +def add_dname(request): + """ View used to add a DName object """ + dname = DNameForm(request.POST or None) + if dname.is_valid(): + dname.save() + messages.success(request, "Cet enregistrement DName a été ajouté") + return redirect(reverse('machines:index-extension')) + return form( + {'dnameform': dname, 'action_name': 'Créer'}, + 'machines/machine.html', + request + ) + + +@login_required +@can_edit(DName) +def edit_dname(request, dname_instance, **_kwargs): + """ View used to edit a DName object """ + dname = DNameForm(request.POST or None, instance=dname_instance) + if dname.is_valid(): + if dname.changed_data: + dname.save() + messages.success(request, "DName modifié") + return redirect(reverse('machines:index-extension')) + return form( + {'dnameform': dname, 'action_name': 'Editer'}, + 'machines/machine.html', + request + ) + + +@login_required +@can_delete_set(DName) +def del_dname(request, instances): + """ View used to delete a DName object """ + dname = DelDNameForm(request.POST or None, instances=instances) + if dname.is_valid(): + dname_dels = dname.cleaned_data['dname'] + for dname_del in dname_dels: + try: + dname_del.delete() + messages.success(request, "Le dname a été supprimé") + except ProtectedError: + messages.error( + request, + ("Erreur le dname suivant %s ne peut être supprimé" + % dname_del) + ) + return redirect(reverse('machines:index-extension')) + return form( + {'dnameform': dname, 'action_name': 'Supprimer'}, + 'machines/machine.html', + request + ) + @login_required @can_create(Txt) @@ -1272,7 +1332,7 @@ def index_nas(request): @login_required -@can_view_all(SOA, Mx, Ns, Txt, Srv, Extension) +@can_view_all(SOA, Mx, Ns, Txt, DName, Srv, Extension) def index_extension(request): """ View displaying the list of existing extensions, the list of existing SOA records, the list of existing MX records , the list of @@ -1292,6 +1352,7 @@ def index_extension(request): .select_related('zone') .select_related('ns__extension')) txt_list = Txt.objects.all().select_related('zone') + dname_list = DName.objects.all().select_related('zone') srv_list = (Srv.objects .all() .select_related('extension') @@ -1305,6 +1366,7 @@ def index_extension(request): 'mx_list': mx_list, 'ns_list': ns_list, 'txt_list': txt_list, + 'dname_list': dname_list, 'srv_list': srv_list } ) diff --git a/re2o/templatetags/acl.py b/re2o/templatetags/acl.py index fccbea1d..d36bdfa7 100644 --- a/re2o/templatetags/acl.py +++ b/re2o/templatetags/acl.py @@ -102,6 +102,7 @@ MODEL_NAME = { 'Mx': machines.models.Mx, 'Ns': machines.models.Ns, 'Txt': machines.models.Txt, + 'DName': machines.models.DName, 'Srv': machines.models.Srv, 'Interface': machines.models.Interface, 'Domain': machines.models.Domain,