8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-24 16:03:47 +00:00

Type est un attribut de interface

This commit is contained in:
chirac 2016-07-18 19:14:48 +02:00
parent acd1c89d38
commit 60b741834f
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
class MachineAdmin(admin.ModelAdmin):
list_display = ('user','name','type','active')
list_display = ('user','name','active')
class MachineTypeAdmin(admin.ModelAdmin):
list_display = ('type','extension')
@ -15,7 +15,7 @@ class IpListAdmin(admin.ModelAdmin):
list_display = ('ipv4',)
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(MachineType, MachineTypeAdmin)

View file

@ -10,16 +10,14 @@ class EditMachineForm(ModelForm):
def __init__(self, *args, **kwargs):
super(EditMachineForm, self).__init__(*args, **kwargs)
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 Meta(EditMachineForm.Meta):
fields = ['type','name']
fields = ['name']
class BaseEditMachineForm(EditMachineForm):
class Meta(EditMachineForm.Meta):
fields = ['type','name','active']
fields = ['name','active']
class EditInterfaceForm(ModelForm):
class Meta:
@ -30,10 +28,12 @@ class EditInterfaceForm(ModelForm):
super(EditInterfaceForm, self).__init__(*args, **kwargs)
self.fields['dns'].label = 'Nom dns de la machine'
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 Meta(EditInterfaceForm.Meta):
fields = ['ipv4','mac_address','dns','details']
fields = ['ipv4','mac_address','dns','type','details']
def __init__(self, *args, **kwargs):
super(AddInterfaceForm, self).__init__(*args, **kwargs)
@ -41,11 +41,11 @@ class AddInterfaceForm(EditInterfaceForm):
class NewInterfaceForm(EditInterfaceForm):
class Meta(EditInterfaceForm.Meta):
fields = ['mac_address','dns','details']
fields = ['mac_address','dns','type','details']
class BaseEditInterfaceForm(EditInterfaceForm):
class Meta(EditInterfaceForm.Meta):
fields = ['ipv4','mac_address','dns','details']
fields = ['ipv4','mac_address','dns','type','details']
def __init__(self, *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):
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)
active = models.BooleanField(default=True)
@ -35,6 +34,7 @@ class Interface(models.Model):
#ipv6 = models.GenericIPAddressField(protocol='IPv6', null=True)
mac_address = MACAddressField(integer=False, unique=True)
machine = models.ForeignKey('Machine', on_delete=models.CASCADE)
type = models.ForeignKey('MachineType', on_delete=models.PROTECT)
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)

View file

@ -2,20 +2,19 @@
<thead>
<tr>
<th>Proprietaire</th>
<th>Type</th>
<th>Interfaces</th>
</tr>
</thead>
{% for machine in machines_list %}
<tr>
<td>{{ machine.user }}</td>
<td>{{ machine.type }}
<td><p>{{ machine.user }}</p>
<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>
<td><table class="table table-striped">
<thead>
<tr>
<th>Nom dns</th>
<th>Type</th>
<th>Mac</th>
<th>Ipv4</th>
<th></th>
@ -24,6 +23,7 @@
{% for interface in machine.interface_set.all %}
<tr>
<td>{{ interface.dns }}</td>
<td>{{ interface.type }}</td>
<td>{{ interface.mac_address }}</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>

View file

@ -11,15 +11,15 @@ from django.db.models import ProtectedError
from django.forms import ValidationError
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 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 """
HOSTNAME_LABEL_PATTERN = re.compile("(?!-)[A-Z\d-]+(?<!-)$", re.IGNORECASE)
dns = interface.dns.lower()
allowed_extension = machine.type.extension.name
allowed_extension = interface.type.extension.name
if not dns.endswith(allowed_extension):
messages.error(request,
"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.user = user
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_interface.machine = new_machine
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():
new_interface = interface_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_interface.save()
messages.success(request, "La machine a été modifiée")
@ -156,14 +156,14 @@ def new_interface(request, machineid):
if interface_form.is_valid():
new_interface = interface_form.save(commit=False)
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:
new_interface = assign_ipv4(new_interface)
elif not new_interface.ipv4:
messages.error(request, u"Il n'y a plus d'ip disponibles")
new_interface.save()
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)
@login_required