8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-23 20:03:11 +00:00

Omnibus, Gestion des MX par re2o , creation d'un model Ns

This commit is contained in:
chirac 2016-11-19 17:44:43 +01:00
parent 52dce2d3f0
commit b0c1bacc0f
9 changed files with 211 additions and 17 deletions

View file

@ -1,7 +1,7 @@
from django.contrib import admin
from reversion.admin import VersionAdmin
from .models import IpType, Machine, MachineType, Alias, IpList, Interface, Extension
from .models import IpType, Machine, MachineType, Alias, IpList, Interface, Extension, Mx, Ns
class MachineAdmin(VersionAdmin):
list_display = ('user','name','active')
@ -16,6 +16,12 @@ class MachineTypeAdmin(VersionAdmin):
class ExtensionAdmin(VersionAdmin):
list_display = ('name',)
class MxAdmin(VersionAdmin):
list_display = ('zone', 'priority', 'name')
class NsAdmin(VersionAdmin):
list_display = ('zone', 'interface')
class IpListAdmin(VersionAdmin):
list_display = ('ipv4','ip_type')
@ -29,6 +35,8 @@ admin.site.register(Machine, MachineAdmin)
admin.site.register(MachineType, MachineTypeAdmin)
admin.site.register(IpType, IpTypeAdmin)
admin.site.register(Extension, ExtensionAdmin)
admin.site.register(Mx, MxAdmin)
admin.site.register(Ns, NsAdmin)
admin.site.register(IpList, IpListAdmin)
admin.site.register(Interface, InterfaceAdmin)
admin.site.register(Alias, AliasAdmin)

View file

@ -1,6 +1,6 @@
from django.forms import ModelForm, Form, ValidationError
from django import forms
from .models import Alias, Machine, Interface, IpList, MachineType, Extension, IpType
from .models import Alias, Machine, Interface, IpList, MachineType, Extension, Mx, Ns, IpType
class EditMachineForm(ModelForm):
class Meta:
@ -62,11 +62,18 @@ class BaseEditInterfaceForm(EditInterfaceForm):
class NewAliasForm(ModelForm):
class Meta:
model = Alias
fields = ['alias']
fields = ['alias','extension']
class EditAliasFullForm(NewAliasForm):
class EditAliasForm(NewAliasForm):
class Meta(NewAliasForm.Meta):
fields = '__all__'
fields = ['alias','extension']
class DelAliasForm(ModelForm):
del_alias = forms.ModelMultipleChoiceField(queryset=Alias.objects.all(), label="Alias actuels", widget=forms.CheckboxSelectMultiple)
class Meta:
exclude = ['interface_parent', 'alias', 'extension']
model = Alias
class MachineTypeForm(ModelForm):
class Meta:
@ -117,3 +124,16 @@ class DelExtensionForm(ModelForm):
class Meta:
exclude = ['name']
model = Extension
class MxForm(ModelForm):
class Meta:
model = Mx
fields = ['zone', 'priority', 'name']
class DelMxForm(ModelForm):
mx = forms.ModelMultipleChoiceField(queryset=Mx.objects.all(), label="MX actuels", widget=forms.CheckboxSelectMultiple)
class Meta:
exclude = ['zone', 'priority', 'name']
model = Mx

View file

@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
import django.core.validators
class Migration(migrations.Migration):
dependencies = [
('machines', '0030_auto_20161118_1730'),
]
operations = [
migrations.CreateModel(
name='Mx',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, verbose_name='ID', serialize=False)),
('priority', models.IntegerField(unique=True)),
('name', models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, to='machines.Alias')),
('zone', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='machines.Extension')),
],
),
migrations.CreateModel(
name='Ns',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, verbose_name='ID', serialize=False)),
('interface', models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, to='machines.Interface')),
('zone', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='machines.Extension')),
],
),
migrations.AlterField(
model_name='iptype',
name='domaine_ip',
field=models.GenericIPAddressField(protocol='IPv4'),
),
migrations.AlterField(
model_name='iptype',
name='domaine_range',
field=models.IntegerField(validators=[django.core.validators.MinValueValidator(8), django.core.validators.MaxValueValidator(32)]),
),
]

View file

@ -34,8 +34,8 @@ class IpType(models.Model):
type = models.CharField(max_length=255)
extension = models.ForeignKey('Extension', on_delete=models.PROTECT)
need_infra = models.BooleanField(default=False)
domaine_ip = models.GenericIPAddressField(protocol='IPv4', unique=True, blank=True, null=True)
domaine_range = models.IntegerField(blank=True, null=True, validators=[MinValueValidator(8), MaxValueValidator(32)])
domaine_ip = models.GenericIPAddressField(protocol='IPv4')
domaine_range = models.IntegerField(validators=[MinValueValidator(8), MaxValueValidator(32)])
def __str__(self):
return self.type
@ -48,6 +48,22 @@ class Extension(models.Model):
def __str__(self):
return self.name
class Mx(models.Model):
PRETTY_NAME = "Enregistrements MX"
zone = models.ForeignKey('Extension', on_delete=models.PROTECT)
priority = models.IntegerField(unique=True)
name = models.OneToOneField('Alias', on_delete=models.PROTECT)
def __str__(self):
return str(self.zone) + ' ' + str(self.priority) + ' ' + str(self.name)
class Ns(models.Model):
PRETTY_NAME = "Enregistrements NS"
zone = models.ForeignKey('Extension', on_delete=models.PROTECT)
interface = models.OneToOneField('Interface', on_delete=models.PROTECT)
class Interface(models.Model):
PRETTY_NAME = "Interface"
@ -92,7 +108,7 @@ class Alias(models.Model):
raise ValidationError("Impossible d'ajouter l'alias, déjà utilisé par une machine")
def __str__(self):
return self.alias
return str(self.alias) + str(self.extension)
class IpList(models.Model):
PRETTY_NAME = "Addresses ipv4"

View file

@ -36,8 +36,8 @@
<ul class="dropdown-menu" aria-labelledby="editioninterface">
{% for al in interface.alias_set.all %}
<li>
<a href="http://{{ al }}{{ al.extension }}">
{{ al }}{{ al.extension }}
<a href="http://{{ al }}">
{{ al }}
<i class="glyphicon glyphicon-share-alt"></i>
</a>
</li>

View file

@ -0,0 +1,26 @@
<table class="table table-striped">
<thead>
<tr>
<th>Zone concernée</th>
<th>Priorité</th>
<th>Enregistrement</th>
<th></th>
<th></th>
</tr>
</thead>
{% for mx in mx_list %}
<tr>
<td>{{ mx.zone }}</td>
<td>{{ mx.priority }}</td>
<td>{{ mx.name }}</td>
<td class="text-right">
{% if is_infra %}
{% include 'buttons/edit.html' with href='machines:edit-mx' id=mx.id %}
{% endif %}
{% include 'buttons/history.html' with href='machines:history' name='mx' id=mx.id %}
</td>
</tr>
{% endfor %}
</table>

View file

@ -10,8 +10,15 @@
<a class="btn btn-danger btn-sm" role="button" href="{% url 'machines:del-extension' %}"><i class="glyphicon glyphicon-trash"></i> Supprimer une ou plusieurs extensions</a>
{% endif %}
{% include "machines/aff_extension.html" with extension_list=extension_list %}
<br />
<br />
<br />
{% endblock %}
<h2>Liste des enregistrements MX</h2>
{% if is_infra %}
<a class="btn btn-primary btn-sm" role="button" href="{% url 'machines:add-mx' %}"><i class="glyphicon glyphicon-plus"></i> Ajouter un enregistrement MX</a>
<a class="btn btn-danger btn-sm" role="button" href="{% url 'machines:del-mx' %}"><i class="glyphicon glyphicon-trash"></i> Supprimer un enregistrement MX</a>
{% endif %}
{% include "machines/aff_mx.html" with mx_list=mx_list %}
<br />
<br />
<br />
{% endblock %}

View file

@ -8,6 +8,7 @@ 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'),
@ -19,11 +20,15 @@ urlpatterns = [
url(r'^add_extension/$', views.add_extension, name='add-extension'),
url(r'^edit_extension/(?P<extensionid>[0-9]+)$', views.edit_extension, name='edit-extension'),
url(r'^del_extension/$', views.del_extension, name='del-extension'),
url(r'^add_mx/$', views.add_mx, name='add-mx'),
url(r'^edit_mx/(?P<mxid>[0-9]+)$', views.edit_mx, name='edit-mx'),
url(r'^del_mx/$', views.del_mx, name='del-mx'),
url(r'^index_extension/$', views.index_extension, name='index-extension'),
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'),
url(r'^history/(?P<object>extension)/(?P<id>[0-9]+)$', views.history, name='history'),
url(r'^history/(?P<object>mx)/(?P<id>[0-9]+)$', views.history, name='history'),
url(r'^history/(?P<object>iptype)/(?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'),

View file

@ -22,8 +22,8 @@ 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, EditAliasFullForm
from .models import IpType, Machine, Interface, IpList, MachineType, Extension
from .forms import IpTypeForm, DelIpTypeForm, NewAliasForm, EditAliasForm, 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,6 +146,19 @@ 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:
@ -356,6 +369,54 @@ def del_extension(request):
return redirect("/machines/index_extension")
return form({'machineform': extension, 'interfaceform': None}, 'machines/machine.html', request)
@login_required
@permission_required('infra')
def add_mx(request):
mx = MxForm(request.POST or None)
if mx.is_valid():
with transaction.atomic(), reversion.create_revision():
mx.save()
reversion.set_user(request.user)
reversion.set_comment("Création")
messages.success(request, "Cet enregistrement mx a été ajouté")
return redirect("/machines/index_extension")
return form({'machineform': mx, 'interfaceform': None}, 'machines/machine.html', request)
@login_required
@permission_required('infra')
def edit_mx(request, mxid):
try:
mx_instance = Mx.objects.get(pk=mxid)
except Mx.DoesNotExist:
messages.error(request, u"Entrée inexistante" )
return redirect("/machines/index_extension/")
mx = MxForm(request.POST or None, instance=mx_instance)
if mx.is_valid():
with transaction.atomic(), reversion.create_revision():
mx.save()
reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in mx.changed_data))
messages.success(request, "Mx modifié")
return redirect("/machines/index_extension/")
return form({'machineform': mx}, 'machines/machine.html', request)
@login_required
@permission_required('infra')
def del_mx(request):
mx = DelMxForm(request.POST or None)
if mx.is_valid():
mx_dels = mx.cleaned_data['mx']
for mx_del in mx_dels:
try:
with transaction.atomic(), reversion.create_revision():
mx_del.delete()
reversion.set_user(request.user)
messages.success(request, "L'mx a été supprimée")
except ProtectedError:
messages.error(request, "Erreur le Mx suivant %s ne peut être supprimé" % mx_del)
return redirect("/machines/index_extension")
return form({'machineform': mx, 'interfaceform': None}, 'machines/machine.html', request)
@login_required
@permission_required('cableur')
def index(request):
@ -388,7 +449,9 @@ def index_machinetype(request):
@permission_required('cableur')
def index_extension(request):
extension_list = Extension.objects.order_by('name')
return render(request, 'machines/index_extension.html', {'extension_list':extension_list})
mx_list = Mx.objects.order_by('zone')
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 history(request, object, id):
@ -428,6 +491,12 @@ def history(request, object, id):
except Extension.DoesNotExist:
messages.error(request, "Extension inexistante")
return redirect("/machines/")
elif object == 'mx' and request.user.has_perms(('cableur',)):
try:
object_instance = Mx.objects.get(pk=id)
except Mx.DoesNotExist:
messages.error(request, "Mx inexistant")
return redirect("/machines/")
else:
messages.error(request, "Objet inconnu")
return redirect("/machines/")