8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-27 01:13:46 +00:00

Type est un attribut de interface

This commit is contained in:
Gabriel Detraz 2016-07-18 19:14:48 +02:00
parent e93f1a5e8c
commit d44103051e
6 changed files with 45 additions and 20 deletions

View file

@ -3,7 +3,7 @@ from django.contrib import admin
from .models import Machine, MachineType, IpList, Interface, Extension from .models import Machine, MachineType, IpList, Interface, Extension
class MachineAdmin(admin.ModelAdmin): class MachineAdmin(admin.ModelAdmin):
list_display = ('user','name','type','active') list_display = ('user','name','active')
class MachineTypeAdmin(admin.ModelAdmin): class MachineTypeAdmin(admin.ModelAdmin):
list_display = ('type','extension') list_display = ('type','extension')
@ -15,7 +15,7 @@ class IpListAdmin(admin.ModelAdmin):
list_display = ('ipv4',) list_display = ('ipv4',)
class InterfaceAdmin(admin.ModelAdmin): class InterfaceAdmin(admin.ModelAdmin):
list_display = ('machine','dns','mac_address','ipv4','details') list_display = ('machine','type','dns','mac_address','ipv4','details')
admin.site.register(Machine, MachineAdmin) admin.site.register(Machine, MachineAdmin)
admin.site.register(MachineType, MachineTypeAdmin) admin.site.register(MachineType, MachineTypeAdmin)

View file

@ -10,16 +10,14 @@ class EditMachineForm(ModelForm):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(EditMachineForm, self).__init__(*args, **kwargs) super(EditMachineForm, self).__init__(*args, **kwargs)
self.fields['name'].label = 'Nom de la machine' self.fields['name'].label = 'Nom de la machine'
self.fields['type'].label = 'Type de machine'
self.fields['type'].empty_label = "Séléctionner un type de machine"
class NewMachineForm(EditMachineForm): class NewMachineForm(EditMachineForm):
class Meta(EditMachineForm.Meta): class Meta(EditMachineForm.Meta):
fields = ['type','name'] fields = ['name']
class BaseEditMachineForm(EditMachineForm): class BaseEditMachineForm(EditMachineForm):
class Meta(EditMachineForm.Meta): class Meta(EditMachineForm.Meta):
fields = ['type','name','active'] fields = ['name','active']
class EditInterfaceForm(ModelForm): class EditInterfaceForm(ModelForm):
class Meta: class Meta:
@ -30,10 +28,12 @@ class EditInterfaceForm(ModelForm):
super(EditInterfaceForm, self).__init__(*args, **kwargs) super(EditInterfaceForm, self).__init__(*args, **kwargs)
self.fields['dns'].label = 'Nom dns de la machine' self.fields['dns'].label = 'Nom dns de la machine'
self.fields['mac_address'].label = 'Adresse mac' self.fields['mac_address'].label = 'Adresse mac'
self.fields['type'].label = 'Type de machine'
self.fields['type'].empty_label = "Séléctionner un type de machine"
class AddInterfaceForm(EditInterfaceForm): class AddInterfaceForm(EditInterfaceForm):
class Meta(EditInterfaceForm.Meta): class Meta(EditInterfaceForm.Meta):
fields = ['ipv4','mac_address','dns','details'] fields = ['ipv4','mac_address','dns','type','details']
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(AddInterfaceForm, self).__init__(*args, **kwargs) super(AddInterfaceForm, self).__init__(*args, **kwargs)
@ -41,11 +41,11 @@ class AddInterfaceForm(EditInterfaceForm):
class NewInterfaceForm(EditInterfaceForm): class NewInterfaceForm(EditInterfaceForm):
class Meta(EditInterfaceForm.Meta): class Meta(EditInterfaceForm.Meta):
fields = ['mac_address','dns','details'] fields = ['mac_address','dns','type','details']
class BaseEditInterfaceForm(EditInterfaceForm): class BaseEditInterfaceForm(EditInterfaceForm):
class Meta(EditInterfaceForm.Meta): class Meta(EditInterfaceForm.Meta):
fields = ['ipv4','mac_address','dns','details'] fields = ['ipv4','mac_address','dns','type','details']
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(BaseEditInterfaceForm, self).__init__(*args, **kwargs) super(BaseEditInterfaceForm, self).__init__(*args, **kwargs)

View file

@ -0,0 +1,25 @@
# -*- 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', '0019_auto_20160718_1141'),
]
operations = [
migrations.RemoveField(
model_name='machine',
name='type',
),
migrations.AddField(
model_name='interface',
name='type',
field=models.ForeignKey(to='machines.MachineType', default=1, on_delete=django.db.models.deletion.PROTECT),
preserve_default=False,
),
]

View file

@ -4,7 +4,6 @@ from macaddress.fields import MACAddressField
class Machine(models.Model): class Machine(models.Model):
user = models.ForeignKey('users.User', on_delete=models.PROTECT) user = models.ForeignKey('users.User', on_delete=models.PROTECT)
type = models.ForeignKey('MachineType', on_delete=models.PROTECT)
name = models.CharField(max_length=255, help_text="Optionnel", blank=True, null=True) name = models.CharField(max_length=255, help_text="Optionnel", blank=True, null=True)
active = models.BooleanField(default=True) active = models.BooleanField(default=True)
@ -35,6 +34,7 @@ class Interface(models.Model):
#ipv6 = models.GenericIPAddressField(protocol='IPv6', null=True) #ipv6 = models.GenericIPAddressField(protocol='IPv6', null=True)
mac_address = MACAddressField(integer=False, unique=True) mac_address = MACAddressField(integer=False, unique=True)
machine = models.ForeignKey('Machine', on_delete=models.CASCADE) machine = models.ForeignKey('Machine', on_delete=models.CASCADE)
type = models.ForeignKey('MachineType', on_delete=models.PROTECT)
details = models.CharField(max_length=255, blank=True) details = models.CharField(max_length=255, blank=True)
dns = models.CharField(help_text="Obligatoire et unique, doit se terminer en %s et ne pas comporter d'autres points" % ", ".join(Extension.objects.values_list('name', flat=True)), max_length=255, unique=True) dns = models.CharField(help_text="Obligatoire et unique, doit se terminer en %s et ne pas comporter d'autres points" % ", ".join(Extension.objects.values_list('name', flat=True)), max_length=255, unique=True)

View file

@ -2,20 +2,19 @@
<thead> <thead>
<tr> <tr>
<th>Proprietaire</th> <th>Proprietaire</th>
<th>Type</th>
<th>Interfaces</th> <th>Interfaces</th>
</tr> </tr>
</thead> </thead>
{% for machine in machines_list %} {% for machine in machines_list %}
<tr> <tr>
<td>{{ machine.user }}</td> <td><p>{{ machine.user }}</p>
<td>{{ machine.type }}
<p><a class="btn btn-primary btn-sm" role="button" href="{% url 'machines:new-interface' machine.id %}"><i class="glyphicon glyphicon-plus"></i> Ajouter une interface</p></a> <p><a class="btn btn-primary btn-sm" role="button" href="{% url 'machines:new-interface' machine.id %}"><i class="glyphicon glyphicon-plus"></i> Ajouter une interface</p></a>
<p><a class="btn btn-danger btn-sm" role="button" href="{% url 'machines:del-machine' machine.id %}"><i class="glyphicon glyphicon-trash"></i> Supprimer la machine</a></p></td> <p><a class="btn btn-danger btn-sm" role="button" href="{% url 'machines:del-machine' machine.id %}"><i class="glyphicon glyphicon-trash"></i> Supprimer la machine</a></p></td>
<td><table class="table table-striped"> <td><table class="table table-striped">
<thead> <thead>
<tr> <tr>
<th>Nom dns</th> <th>Nom dns</th>
<th>Type</th>
<th>Mac</th> <th>Mac</th>
<th>Ipv4</th> <th>Ipv4</th>
<th></th> <th></th>
@ -24,6 +23,7 @@
{% for interface in machine.interface_set.all %} {% for interface in machine.interface_set.all %}
<tr> <tr>
<td>{{ interface.dns }}</td> <td>{{ interface.dns }}</td>
<td>{{ interface.type }}</td>
<td>{{ interface.mac_address }}</td> <td>{{ interface.mac_address }}</td>
<td>{{ interface.ipv4 }}</td> <td>{{ interface.ipv4 }}</td>
<td><a class="btn btn-primary btn-sm" role="button" href="{% url 'machines:edit-interface' interface.id %}"><i class="glyphicon glyphicon-hdd"></i> Editer</a> <td><a class="btn btn-primary btn-sm" role="button" href="{% url 'machines:edit-interface' interface.id %}"><i class="glyphicon glyphicon-hdd"></i> Editer</a>

View file

@ -11,15 +11,15 @@ from django.db.models import ProtectedError
from django.forms import ValidationError from django.forms import ValidationError
import re import re
from .forms import NewMachineForm, EditMachineForm, EditInterfaceForm, AddInterfaceForm, NewInterfaceForm, MachineTypeForm, DelMachineTypeForm, ExtensionForm, DelExtensionForm, BaseEditInterfaceForm, BaseEditMachineForm from .forms import NewMachineForm, EditMachineForm, EditInterfaceForm, AddInterfaceForm, MachineTypeForm, DelMachineTypeForm, ExtensionForm, DelExtensionForm, BaseEditInterfaceForm, BaseEditMachineForm
from .models import Machine, Interface, IpList, MachineType, Extension from .models import Machine, Interface, IpList, MachineType, Extension
from users.models import User from users.models import User
def full_domain_validator(request, interface, machine): def full_domain_validator(request, interface):
""" Validation du nom de domaine, extensions dans type de machine, prefixe pas plus long que 63 caractères """ """ Validation du nom de domaine, extensions dans type de machine, prefixe pas plus long que 63 caractères """
HOSTNAME_LABEL_PATTERN = re.compile("(?!-)[A-Z\d-]+(?<!-)$", re.IGNORECASE) HOSTNAME_LABEL_PATTERN = re.compile("(?!-)[A-Z\d-]+(?<!-)$", re.IGNORECASE)
dns = interface.dns.lower() dns = interface.dns.lower()
allowed_extension = machine.type.extension.name allowed_extension = interface.type.extension.name
if not dns.endswith(allowed_extension): if not dns.endswith(allowed_extension):
messages.error(request, messages.error(request,
"Le nom de domaine %s doit comporter une extension valide en %s" % (dns, allowed_extension) ) "Le nom de domaine %s doit comporter une extension valide en %s" % (dns, allowed_extension) )
@ -86,7 +86,7 @@ def new_machine(request, userid):
new_machine = machine.save(commit=False) new_machine = machine.save(commit=False)
new_machine.user = user new_machine.user = user
new_interface = interface.save(commit=False) new_interface = interface.save(commit=False)
if full_domain_validator(request, new_interface, new_machine): if full_domain_validator(request, new_interface):
new_machine.save() new_machine.save()
new_interface.machine = new_machine new_interface.machine = new_machine
if free_ip() and not new_interface.ipv4: if free_ip() and not new_interface.ipv4:
@ -117,7 +117,7 @@ def edit_interface(request, interfaceid):
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)
if full_domain_validator(request, new_interface, new_machine): if full_domain_validator(request, new_interface):
new_machine.save() new_machine.save()
new_interface.save() new_interface.save()
messages.success(request, "La machine a été modifiée") messages.success(request, "La machine a été modifiée")
@ -156,14 +156,14 @@ def new_interface(request, machineid):
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, machine): if full_domain_validator(request, new_interface):
if free_ip() and not new_interface.ipv4: if free_ip() 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")
new_interface.save() new_interface.save()
messages.success(request, "L'interface a été ajoutée") messages.success(request, "L'interface a été ajoutée")
return redirect("/machines/") return redirect("/users/profil/" + str(request.user.id))
return form({'interfaceform': interface_form}, 'machines/machine.html', request) return form({'interfaceform': interface_form}, 'machines/machine.html', request)
@login_required @login_required