8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-27 07:02:26 +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')
class MachineTypeAdmin(VersionAdmin):
list_display = ('type','extension')
list_display = ('type','extension','need_infra')
class ExtensionAdmin(VersionAdmin):
list_display = ('name',)
class IpListAdmin(VersionAdmin):
list_display = ('ipv4',)
list_display = ('ipv4','ip_type')
class InterfaceAdmin(VersionAdmin):
list_display = ('machine','type','dns','mac_address','ipv4','details')

View file

@ -1,6 +1,6 @@
from django.forms import ModelForm, Form, ValidationError
from django import forms
from .models import Machine, Interface, MachineType, Extension
from .models import Machine, Interface, IpList, MachineType, Extension
class EditMachineForm(ModelForm):
class Meta:
@ -36,8 +36,12 @@ class AddInterfaceForm(EditInterfaceForm):
fields = ['ipv4','mac_address','dns','type','details']
def __init__(self, *args, **kwargs):
infra = kwargs.pop('infra')
super(AddInterfaceForm, self).__init__(*args, **kwargs)
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 Meta(EditInterfaceForm.Meta):
@ -47,14 +51,18 @@ class BaseEditInterfaceForm(EditInterfaceForm):
class Meta(EditInterfaceForm.Meta):
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)
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 Meta:
model = MachineType
fields = ['type','extension']
fields = ['type','extension','need_infra']
def __init__(self, *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):
type = models.CharField(max_length=255)
extension = models.ForeignKey('Extension', on_delete=models.PROTECT)
need_infra = models.BooleanField(default=False)
def __str__(self):
return self.type
@ -48,6 +49,7 @@ class Interface(models.Model):
class IpList(models.Model):
ipv4 = models.GenericIPAddressField(protocol='IPv4', unique=True)
ip_type = models.ForeignKey('MachineType', on_delete=models.PROTECT)
def __str__(self):
return self.ipv4

View file

@ -3,6 +3,7 @@
<tr>
<th>Type de machine</th>
<th>Extension</th>
<th>Nécessite l'autorisation infra</th>
<th></th>
<th></th>
</tr>
@ -11,6 +12,7 @@
<tr>
<td>{{ type.type }}</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><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>

View file

@ -61,13 +61,13 @@ def assign_ips(user):
interface.save()
return
def free_ip():
def free_ip(type):
""" 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):
""" Assigne une ip à l'interface """
free_ips = free_ip()
free_ips = free_ip(interface.type)
if free_ips:
interface.ipv4 = free_ips[0]
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")
return redirect("/users/profil/" + str(request.user.id))
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():
new_machine = machine.save(commit=False)
new_machine.user = user
@ -105,7 +105,7 @@ def new_machine(request, userid):
reversion.set_user(request.user)
reversion.set_comment("Création")
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)
elif not new_interface.ipv4:
messages.error(request, u"Il n'y a plus d'ip disponibles")
@ -124,15 +124,15 @@ def edit_interface(request, interfaceid):
except Interface.DoesNotExist:
messages.error(request, u"Interface inexistante" )
return redirect("/machines")
if not request.user.has_perms(('cableur',)):
if not request.user.has_perms(('infra',)):
if 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))
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:
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():
new_interface = interface_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:
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))
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():
new_interface = interface_form.save(commit=False)
new_interface.machine = machine
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)
elif not new_interface.ipv4:
messages.error(request, u"Il n'y a plus d'ip disponibles")