From a50afbdeac4f8b0e13d881064f0a9a8aee07d132 Mon Sep 17 00:00:00 2001 From: Charlie Jacomme Date: Sat, 23 Jun 2018 16:39:03 +0200 Subject: [PATCH] Add machines.models.Role --- machines/admin.py | 7 ++ machines/forms.py | 35 ++++++++++ machines/models.py | 20 ++++++ machines/templates/machines/aff_role.html | 49 +++++++++++++ machines/templates/machines/index_role.html | 41 +++++++++++ machines/templates/machines/machine.html | 7 ++ machines/templates/machines/sidebar.html | 6 ++ machines/urls.py | 8 ++- machines/views.py | 77 +++++++++++++++++++++ re2o/templatetags/acl.py | 1 + re2o/views.py | 1 + 11 files changed, 251 insertions(+), 1 deletion(-) create mode 100644 machines/templates/machines/aff_role.html create mode 100644 machines/templates/machines/index_role.html diff --git a/machines/admin.py b/machines/admin.py index 0f85007c..afcd088a 100644 --- a/machines/admin.py +++ b/machines/admin.py @@ -40,6 +40,7 @@ from .models import ( Srv, Nas, Service, + Role, OuverturePort, Ipv6List, OuverturePortList @@ -135,6 +136,11 @@ class ServiceAdmin(VersionAdmin): """ Admin view of a ServiceAdmin object """ list_display = ('service_type', 'min_time_regen', 'regular_time_regen') +class RoleAdmin(VersionAdmin): + """ Admin view of a RoleAdmin object """ + list_display = ('role_type') + + admin.site.register(Machine, MachineAdmin) admin.site.register(MachineType, MachineTypeAdmin) @@ -149,6 +155,7 @@ admin.site.register(IpList, IpListAdmin) admin.site.register(Interface, InterfaceAdmin) admin.site.register(Domain, DomainAdmin) admin.site.register(Service, ServiceAdmin) +admin.site.register(Role, RoleAdmin) admin.site.register(Vlan, VlanAdmin) admin.site.register(Ipv6List, Ipv6ListAdmin) admin.site.register(Nas, NasAdmin) diff --git a/machines/forms.py b/machines/forms.py index 91df33d4..4b5a0d44 100644 --- a/machines/forms.py +++ b/machines/forms.py @@ -52,6 +52,7 @@ from .models import ( Mx, Txt, Ns, + Role, Service, Vlan, Srv, @@ -467,6 +468,40 @@ class DelNasForm(FormRevMixin, Form): self.fields['nas'].queryset = Nas.objects.all() +class RoleForm(FormRevMixin, ModelForm): + """Ajout et edition d'un role""" + class Meta: + model = Role + fields = '__all__' + + def __init__(self, *args, **kwargs): + prefix = kwargs.pop('prefix', self.Meta.model.__name__) + super(RoleForm, self).__init__(*args, prefix=prefix, **kwargs) + self.fields['servers'].queryset = (Interface.objects.all() + .select_related( + 'domain__extension' + )) + + +class DelRoleForm(FormRevMixin, Form): + """Suppression d'un ou plusieurs service""" + role = forms.ModelMultipleChoiceField( + queryset=Role.objects.none(), + label="Roles actuels", + widget=forms.CheckboxSelectMultiple + ) + + def __init__(self, *args, **kwargs): + instances = kwargs.pop('instances', None) + super(DelRoleForm, self).__init__(*args, **kwargs) + if instances: + self.fields['role'].queryset = instances + else: + self.fields['role'].queryset = role.objects.all() + + + + class ServiceForm(FormRevMixin, ModelForm): """Ajout et edition d'une classe de service : dns, dhcp, etc""" class Meta: diff --git a/machines/models.py b/machines/models.py index b6d4c282..034a4f74 100644 --- a/machines/models.py +++ b/machines/models.py @@ -1344,6 +1344,26 @@ class IpList(RevMixin, AclMixin, models.Model): return self.ipv4 + +class Role(RevMixin, AclMixin, models.Model): + """ Definition d'un role (routeur principal, routeur de backkup)""" + """ Sert à la génération automatique de la conf des serveurs""" + PRETTY_NAME = "Roles des serveurs" + + role_type = models.CharField(max_length=255, unique=True) + servers = models.ManyToManyField('Interface') + + class Meta: + permissions = ( + ("view_role", "Peut voir un objet service"), + ) + + def save(self, *args, **kwargs): + super(Role, self).save(*args, **kwargs) + + def __str__(self): + return str(self.role_type) + class Service(RevMixin, AclMixin, models.Model): """ Definition d'un service (dhcp, dns, etc)""" PRETTY_NAME = "Services à générer (dhcp, dns, etc)" diff --git a/machines/templates/machines/aff_role.html b/machines/templates/machines/aff_role.html new file mode 100644 index 00000000..f914cd24 --- /dev/null +++ b/machines/templates/machines/aff_role.html @@ -0,0 +1,49 @@ +{% 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 role in role_list %} + + + + + + {% endfor %} +
Nom du roleServeurs inclus
{{ role.role_type }}{% for serv in role.servers.all %}{{ serv }}, {% endfor %} + {% can_edit role %} + {% include 'buttons/edit.html' with href='machines:edit-role' id=role.id %} + {% acl_end %} + {% include 'buttons/history.html' with href='machines:history' name='role' id=role.id %} +
+ diff --git a/machines/templates/machines/index_role.html b/machines/templates/machines/index_role.html new file mode 100644 index 00000000..93a99577 --- /dev/null +++ b/machines/templates/machines/index_role.html @@ -0,0 +1,41 @@ +{% extends "machines/sidebar.html" %} +{% 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 bootstrap3 %} +{% load acl %} + +{% block title %}Machines{% endblock %} + +{% block content %} +

Liste des roles

+ {% can_create Role %} + Ajouter un role + {% acl_end %} + Supprimer un ou plusieurs role + {% include "machines/aff_role.html" with role_list=role_list %} +
+
+{% endblock %} + diff --git a/machines/templates/machines/machine.html b/machines/templates/machines/machine.html index 0e7082f8..db0fa0ea 100644 --- a/machines/templates/machines/machine.html +++ b/machines/templates/machines/machine.html @@ -66,6 +66,9 @@ with this program; if not, write to the Free Software Foundation, Inc., {% if serviceform %} {% bootstrap_form_errors serviceform %} {% endif %} +{% if roleform %} + {% bootstrap_form_errors roleform %} +{% endif %} {% if vlanform %} {% bootstrap_form_errors vlanform %} {% endif %} @@ -134,6 +137,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,

Service

{% massive_bootstrap_form serviceform 'servers' %} {% endif %} + {% if roleform %} +

Role

+ {% massive_bootstrap_form roleform 'servers' %} + {% endif %} {% if vlanform %}

Vlan

{% bootstrap_form vlanform %} diff --git a/machines/templates/machines/sidebar.html b/machines/templates/machines/sidebar.html index 5a0f975d..68031f29 100644 --- a/machines/templates/machines/sidebar.html +++ b/machines/templates/machines/sidebar.html @@ -68,6 +68,12 @@ with this program; if not, write to the Free Software Foundation, Inc., Services (dhcp, dns...) {% acl_end %} + {% can_view_all Role %} + + + Roles des serveurs + + {% acl_end %} {% can_view_all OuverturePortList %} diff --git a/machines/urls.py b/machines/urls.py index 9a5fa25e..b9e3e79f 100644 --- a/machines/urls.py +++ b/machines/urls.py @@ -21,7 +21,7 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. """machines.urls -The defined URLs for the Cotisations app +The defined URLs for the Machines app """ from __future__ import unicode_literals @@ -111,6 +111,12 @@ urlpatterns = [ name='edit-service'), url(r'^del_service/$', views.del_service, name='del-service'), url(r'^index_service/$', views.index_service, name='index-service'), + url(r'^add_role/$', views.add_role, name='add-role'), + url(r'^edit_role/(?P[0-9]+)$', + views.edit_role, + name='edit-role'), + url(r'^del_role/$', views.del_role, name='del-role'), + url(r'^index_role/$', views.index_role, name='index-role'), url(r'^add_vlan/$', views.add_vlan, name='add-vlan'), url(r'^edit_vlan/(?P[0-9]+)$', views.edit_vlan, name='edit-vlan'), url(r'^del_vlan/$', views.del_vlan, name='del-vlan'), diff --git a/machines/views.py b/machines/views.py index 75c2f483..75bfb3f6 100644 --- a/machines/views.py +++ b/machines/views.py @@ -97,6 +97,8 @@ from .forms import ( DelMxForm, VlanForm, DelVlanForm, + RoleForm, + DelRoleForm, ServiceForm, DelServiceForm, NasForm, @@ -117,6 +119,7 @@ from .models import ( Mx, Ns, Domain, + Role, Service, Service_link, Vlan, @@ -1011,6 +1014,65 @@ def del_alias(request, interface, interfaceid): ) +@login_required +@can_create(Role) +def add_role(request): + """ View used to add a Role object """ + role = RoleForm(request.POST or None) + if role.is_valid(): + role.save() + messages.success(request, "Cet enregistrement role a été ajouté") + return redirect(reverse('machines:index-role')) + return form( + {'roleform': role, 'action_name': 'Créer'}, + 'machines/machine.html', + request + ) + + +@login_required +@can_edit(Role) +def edit_role(request, role_instance, **_kwargs): + """ View used to edit a Role object """ + role = RoleForm(request.POST or None, instance=role_instance) + if role.is_valid(): + if role.changed_data: + role.save() + messages.success(request, "Role modifié") + return redirect(reverse('machines:index-role')) + return form( + {'roleform': role, 'action_name': 'Editer'}, + 'machines/machine.html', + request + ) + + +@login_required +@can_delete_set(Role) +def del_role(request, instances): + """ View used to delete a Service object """ + role = DelRoleForm(request.POST or None, instances=instances) + if role.is_valid(): + role_dels = role.cleaned_data['role'] + for role_del in role_dels: + try: + role_del.delete() + messages.success(request, "Le role a été supprimée") + except ProtectedError: + messages.error( + request, + ("Erreur le role suivant %s ne peut être supprimé" + % role_del) + ) + return redirect(reverse('machines:index-role')) + return form( + {'roleform': role, 'action_name': 'Supprimer'}, + 'machines/machine.html', + request + ) + + + @login_required @can_create(Service) def add_service(request): @@ -1336,6 +1398,21 @@ def index_ipv6(request, interface, interfaceid): ) +@login_required +@can_view_all(Role) +def index_role(request): + """ View used to display the list of existing roles """ + role_list = (Role.objects + .prefetch_related( + 'servers__domain__extension' + ).all()) + return render( + request, + 'machines/index_role.html', + {'role_list': role_list} + ) + + @login_required @can_view_all(Service) def index_service(request): diff --git a/re2o/templatetags/acl.py b/re2o/templatetags/acl.py index fccbea1d..d01836dd 100644 --- a/re2o/templatetags/acl.py +++ b/re2o/templatetags/acl.py @@ -109,6 +109,7 @@ MODEL_NAME = { 'Ipv6List': machines.models.Ipv6List, 'machines.Service': machines.models.Service, 'Service_link': machines.models.Service_link, + 'Role': machines.models.Role, 'OuverturePortList': machines.models.OuverturePortList, 'OuverturePort': machines.models.OuverturePort, # preferences diff --git a/re2o/views.py b/re2o/views.py index 3c5cde09..ba524d25 100644 --- a/re2o/views.py +++ b/re2o/views.py @@ -125,6 +125,7 @@ HISTORY_BIND = { 'srv': machines.models.Srv, 'ns': machines.models.Ns, 'service': machines.models.Service, + 'role': machines.models.Role, 'vlan': machines.models.Vlan, 'nas': machines.models.Nas, 'ipv6list': machines.models.Ipv6List,