diff --git a/machines/forms.py b/machines/forms.py index 0b2ceb87..8ea1b469 100644 --- a/machines/forms.py +++ b/machines/forms.py @@ -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'] diff --git a/machines/models.py b/machines/models.py index 2602c86d..e094460c 100644 --- a/machines/models.py +++ b/machines/models.py @@ -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) diff --git a/machines/templates/machines/aff_alias.html b/machines/templates/machines/aff_alias.html new file mode 100644 index 00000000..14b5da20 --- /dev/null +++ b/machines/templates/machines/aff_alias.html @@ -0,0 +1,18 @@ + + + + + + + + {% for alias in alias_list %} + + + + + {% endfor %} +
Alias
{{ alias }} + {% 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 %} +
+ diff --git a/machines/templates/machines/aff_machines.html b/machines/templates/machines/aff_machines.html index 75011282..e1c11fe3 100644 --- a/machines/templates/machines/aff_machines.html +++ b/machines/templates/machines/aff_machines.html @@ -63,6 +63,11 @@
  • + + Gerer les alias + +
  • +
  • Historique diff --git a/machines/templates/machines/index_alias.html b/machines/templates/machines/index_alias.html new file mode 100644 index 00000000..1f38fac3 --- /dev/null +++ b/machines/templates/machines/index_alias.html @@ -0,0 +1,15 @@ +{% extends "machines/sidebar.html" %} +{% load bootstrap3 %} + +{% block title %}Machines{% endblock %} + +{% block content %} +

    Liste des alias de l'interface

    + Ajouter un alias + Supprimer un ou plusieurs alias + {% include "machines/aff_alias.html" with alias_list=alias_list %} +
    +
    +
    +{% endblock %} + diff --git a/machines/urls.py b/machines/urls.py index b93c0373..b52f83ff 100644 --- a/machines/urls.py +++ b/machines/urls.py @@ -8,7 +8,6 @@ urlpatterns = [ url(r'^del_machine/(?P[0-9]+)$', views.del_machine, name='del-machine'), url(r'^new_interface/(?P[0-9]+)$', views.new_interface, name='new-interface'), url(r'^del_interface/(?P[0-9]+)$', views.del_interface, name='del-interface'), - url(r'^manage_alias/(?P[0-9]+)$', views.manage_alias, name='manage-alias'), url(r'^add_machinetype/$', views.add_machinetype, name='add-machinetype'), url(r'^edit_machinetype/(?P[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[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[0-9]+)$', views.add_alias, name='add-alias'), + url(r'^edit_alias/(?P[0-9]+)$', views.edit_alias, name='edit-alias'), + url(r'^del_alias/(?P[0-9]+)$', views.del_alias, name='del-alias'), + url(r'^index_alias/(?P[0-9]+)$', views.index_alias, name='index-alias'), 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'), @@ -34,6 +37,7 @@ urlpatterns = [ 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'^history/(?Palias)/(?P[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'), diff --git a/machines/views.py b/machines/views.py index 3e5a25aa..279fea8c 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, 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)