8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-22 19:33:11 +00:00

Lie le type de machine avec les acl infra, lie les / d'ip et type de machines

This commit is contained in:
Gabriel Detraz 2016-10-13 02:11:23 +02:00 committed by root
parent b0c48b36e3
commit 98e67b9b5f
7 changed files with 67 additions and 15 deletions

View file

@ -7,13 +7,13 @@ class MachineAdmin(VersionAdmin):
list_display = ('user','name','active') list_display = ('user','name','active')
class MachineTypeAdmin(VersionAdmin): class MachineTypeAdmin(VersionAdmin):
list_display = ('type','extension') list_display = ('type','extension','need_infra')
class ExtensionAdmin(VersionAdmin): class ExtensionAdmin(VersionAdmin):
list_display = ('name',) list_display = ('name',)
class IpListAdmin(VersionAdmin): class IpListAdmin(VersionAdmin):
list_display = ('ipv4',) list_display = ('ipv4','ip_type')
class InterfaceAdmin(VersionAdmin): class InterfaceAdmin(VersionAdmin):
list_display = ('machine','type','dns','mac_address','ipv4','details') list_display = ('machine','type','dns','mac_address','ipv4','details')

View file

@ -1,6 +1,6 @@
from django.forms import ModelForm, Form, ValidationError from django.forms import ModelForm, Form, ValidationError
from django import forms from django import forms
from .models import Machine, Interface, MachineType, Extension from .models import Machine, Interface, IpList, MachineType, Extension
class EditMachineForm(ModelForm): class EditMachineForm(ModelForm):
class Meta: class Meta:
@ -36,8 +36,12 @@ class AddInterfaceForm(EditInterfaceForm):
fields = ['ipv4','mac_address','dns','type','details'] fields = ['ipv4','mac_address','dns','type','details']
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
infra = kwargs.pop('infra')
super(AddInterfaceForm, self).__init__(*args, **kwargs) super(AddInterfaceForm, self).__init__(*args, **kwargs)
self.fields['ipv4'].empty_label = "Assignation automatique de l'ipv4" self.fields['ipv4'].empty_label = "Assignation automatique de l'ipv4"
if not infra:
self.fields['type'].queryset = MachineType.objects.filter(need_infra=False)
self.fields['ipv4'].queryset = IpList.objects.filter(ip_type=MachineType.objects.filter(need_infra=False))
class NewInterfaceForm(EditInterfaceForm): class NewInterfaceForm(EditInterfaceForm):
class Meta(EditInterfaceForm.Meta): class Meta(EditInterfaceForm.Meta):
@ -47,14 +51,18 @@ class BaseEditInterfaceForm(EditInterfaceForm):
class Meta(EditInterfaceForm.Meta): class Meta(EditInterfaceForm.Meta):
fields = ['ipv4','mac_address','dns','type','details'] fields = ['ipv4','mac_address','dns','type','details']
def __init__(self, *args, **kwargs): def __init__(self, infra=False, *args, **kwargs):
infra = kwargs.pop('infra')
super(BaseEditInterfaceForm, self).__init__(*args, **kwargs) super(BaseEditInterfaceForm, self).__init__(*args, **kwargs)
self.fields['ipv4'].empty_label = "Assignation automatique de l'ipv4" self.fields['ipv4'].empty_label = "Assignation automatique de l'ipv4"
if not infra:
self.fields['type'].queryset = MachineType.objects.filter(need_infra=False)
self.fields['ipv4'].queryset = IpList.objects.filter(ip_type=MachineType.objects.filter(need_infra=False))
class MachineTypeForm(ModelForm): class MachineTypeForm(ModelForm):
class Meta: class Meta:
model = MachineType model = MachineType
fields = ['type','extension'] fields = ['type','extension','need_infra']
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(MachineTypeForm, self).__init__(*args, **kwargs) super(MachineTypeForm, self).__init__(*args, **kwargs)

View file

@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('machines', '0022_auto_20161011_1829'),
]
operations = [
migrations.AddField(
model_name='iplist',
name='ip_type',
field=models.ForeignKey(to='machines.MachineType', on_delete=django.db.models.deletion.PROTECT, default=1),
preserve_default=False,
),
]

View file

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('machines', '0023_iplist_ip_type'),
]
operations = [
migrations.AddField(
model_name='machinetype',
name='need_infra',
field=models.BooleanField(default=False),
),
]

View file

@ -18,6 +18,7 @@ class Machine(models.Model):
class MachineType(models.Model): class MachineType(models.Model):
type = models.CharField(max_length=255) type = models.CharField(max_length=255)
extension = models.ForeignKey('Extension', on_delete=models.PROTECT) extension = models.ForeignKey('Extension', on_delete=models.PROTECT)
need_infra = models.BooleanField(default=False)
def __str__(self): def __str__(self):
return self.type return self.type
@ -48,6 +49,7 @@ class Interface(models.Model):
class IpList(models.Model): class IpList(models.Model):
ipv4 = models.GenericIPAddressField(protocol='IPv4', unique=True) ipv4 = models.GenericIPAddressField(protocol='IPv4', unique=True)
ip_type = models.ForeignKey('MachineType', on_delete=models.PROTECT)
def __str__(self): def __str__(self):
return self.ipv4 return self.ipv4

View file

@ -3,6 +3,7 @@
<tr> <tr>
<th>Type de machine</th> <th>Type de machine</th>
<th>Extension</th> <th>Extension</th>
<th>Nécessite l'autorisation infra</th>
<th></th> <th></th>
<th></th> <th></th>
</tr> </tr>
@ -11,6 +12,7 @@
<tr> <tr>
<td>{{ type.type }}</td> <td>{{ type.type }}</td>
<td>{{ type.extension }}</td> <td>{{ type.extension }}</td>
<td>{{ type.need_infra }}</td>
<td>{% if is_infra %}<a class="btn btn-primary btn-sm" role="button" href="{% url 'machines:edit-machinetype' type.id %}"><i class="glyphicon glyphicon-pushpin"></i> Editer</a>{% endif %}</td> <td>{% if is_infra %}<a class="btn btn-primary btn-sm" role="button" href="{% url 'machines:edit-machinetype' type.id %}"><i class="glyphicon glyphicon-pushpin"></i> Editer</a>{% endif %}</td>
<td><a class="btn btn-info btn-sm" role="button" href="{% url 'machines:history' 'machinetype' type.id %}"><i class="glyphicon glyphicon-repeat"></i> Historique</a></td> <td><a class="btn btn-info btn-sm" role="button" href="{% url 'machines:history' 'machinetype' type.id %}"><i class="glyphicon glyphicon-repeat"></i> Historique</a></td>
</tr> </tr>

View file

@ -61,13 +61,13 @@ def assign_ips(user):
interface.save() interface.save()
return return
def free_ip(): def free_ip(type):
""" Renvoie la liste des ip disponibles """ """ Renvoie la liste des ip disponibles """
return IpList.objects.filter(interface__isnull=True) return IpList.objects.filter(interface__isnull=True).filter(ip_type=type)
def assign_ipv4(interface): def assign_ipv4(interface):
""" Assigne une ip à l'interface """ """ Assigne une ip à l'interface """
free_ips = free_ip() free_ips = free_ip(interface.type)
if free_ips: if free_ips:
interface.ipv4 = free_ips[0] interface.ipv4 = free_ips[0]
return interface return interface
@ -94,7 +94,7 @@ def new_machine(request, userid):
messages.error(request, "Vous ne pouvez pas ajouter une machine à un autre user que vous sans droit") messages.error(request, "Vous ne pouvez pas ajouter une machine à un autre user que vous sans droit")
return redirect("/users/profil/" + str(request.user.id)) return redirect("/users/profil/" + str(request.user.id))
machine = NewMachineForm(request.POST or None) machine = NewMachineForm(request.POST or None)
interface = AddInterfaceForm(request.POST or None) interface = AddInterfaceForm(request.POST or None, infra=request.user.has_perms(('infra',)))
if machine.is_valid() and interface.is_valid(): if machine.is_valid() and interface.is_valid():
new_machine = machine.save(commit=False) new_machine = machine.save(commit=False)
new_machine.user = user new_machine.user = user
@ -105,7 +105,7 @@ def new_machine(request, userid):
reversion.set_user(request.user) reversion.set_user(request.user)
reversion.set_comment("Création") reversion.set_comment("Création")
new_interface.machine = new_machine new_interface.machine = new_machine
if free_ip() and not new_interface.ipv4: if free_ip(new_interface.type) and not new_interface.ipv4:
new_interface = assign_ipv4(new_interface) new_interface = assign_ipv4(new_interface)
elif not new_interface.ipv4: elif not new_interface.ipv4:
messages.error(request, u"Il n'y a plus d'ip disponibles") messages.error(request, u"Il n'y a plus d'ip disponibles")
@ -124,15 +124,15 @@ def edit_interface(request, interfaceid):
except Interface.DoesNotExist: except Interface.DoesNotExist:
messages.error(request, u"Interface inexistante" ) messages.error(request, u"Interface inexistante" )
return redirect("/machines") return redirect("/machines")
if not request.user.has_perms(('cableur',)): if not request.user.has_perms(('infra',)):
if interface.machine.user != request.user: if interface.machine.user != request.user:
messages.error(request, "Vous ne pouvez pas éditer une machine d'un autre user que vous sans droit") 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)) return redirect("/users/profil/" + str(request.user.id))
machine_form = BaseEditMachineForm(request.POST or None, instance=interface.machine) machine_form = BaseEditMachineForm(request.POST or None, instance=interface.machine)
interface_form = BaseEditInterfaceForm(request.POST or None, instance=interface) interface_form = BaseEditInterfaceForm(request.POST or None, instance=interface, infra=False)
else: else:
machine_form = EditMachineForm(request.POST or None, instance=interface.machine) machine_form = EditMachineForm(request.POST or None, instance=interface.machine)
interface_form = EditInterfaceForm(request.POST or None, instance=interface) interface_form = EditInterfaceForm(request.POST or None, instance=interface, infra=True)
if machine_form.is_valid() and interface_form.is_valid(): if machine_form.is_valid() and interface_form.is_valid():
new_interface = interface_form.save(commit=False) new_interface = interface_form.save(commit=False)
new_machine = machine_form.save(commit=False) new_machine = machine_form.save(commit=False)
@ -179,12 +179,12 @@ def new_interface(request, machineid):
if machine.user != request.user: if machine.user != request.user:
messages.error(request, "Vous ne pouvez pas ajouter une interface à une machine d'un autre user que vous sans droit") messages.error(request, "Vous ne pouvez pas ajouter une interface à une machine d'un autre user que vous sans droit")
return redirect("/users/profil/" + str(request.user.id)) return redirect("/users/profil/" + str(request.user.id))
interface_form = AddInterfaceForm(request.POST or None) interface_form = AddInterfaceForm(request.POST or None, infra=request.user.has_perms(('infra',)))
if interface_form.is_valid(): if interface_form.is_valid():
new_interface = interface_form.save(commit=False) new_interface = interface_form.save(commit=False)
new_interface.machine = machine new_interface.machine = machine
if full_domain_validator(request, new_interface): if full_domain_validator(request, new_interface):
if free_ip() and not new_interface.ipv4: if free_ip(new_interface.type) and not new_interface.ipv4:
new_interface = assign_ipv4(new_interface) new_interface = assign_ipv4(new_interface)
elif not new_interface.ipv4: elif not new_interface.ipv4:
messages.error(request, u"Il n'y a plus d'ip disponibles") messages.error(request, u"Il n'y a plus d'ip disponibles")