diff --git a/machines/admin.py b/machines/admin.py
index a3d480ac..5931c99d 100644
--- a/machines/admin.py
+++ b/machines/admin.py
@@ -3,7 +3,7 @@ from django.contrib import admin
from .models import Machine, MachineType, IpList, Interface
class MachineAdmin(admin.ModelAdmin):
- list_display = ('user','type')
+ list_display = ('user','name','type')
class MachineTypeAdmin(admin.ModelAdmin):
list_display = ('type',)
@@ -12,7 +12,7 @@ class IpListAdmin(admin.ModelAdmin):
list_display = ('ipv4',)
class InterfaceAdmin(admin.ModelAdmin):
- list_display = ('machine','name','mac_address','ipv4','details')
+ list_display = ('machine','dns','mac_address','ipv4','details')
admin.site.register(Machine, MachineAdmin)
admin.site.register(MachineType, MachineTypeAdmin)
diff --git a/machines/migrations/0003_auto_20160703_1450.py b/machines/migrations/0003_auto_20160703_1450.py
new file mode 100644
index 00000000..82566119
--- /dev/null
+++ b/machines/migrations/0003_auto_20160703_1450.py
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+import macaddress.fields
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('machines', '0002_auto_20160703_1444'),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name='interface',
+ name='mac_address',
+ field=macaddress.fields.MACAddressField(integer=True, unique=True),
+ ),
+ ]
diff --git a/machines/migrations/0004_auto_20160703_1451.py b/machines/migrations/0004_auto_20160703_1451.py
new file mode 100644
index 00000000..892d07bb
--- /dev/null
+++ b/machines/migrations/0004_auto_20160703_1451.py
@@ -0,0 +1,19 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('machines', '0003_auto_20160703_1450'),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name='iplist',
+ name='ipv4',
+ field=models.GenericIPAddressField(protocol='IPv4', unique=True),
+ ),
+ ]
diff --git a/machines/migrations/0005_auto_20160703_1523.py b/machines/migrations/0005_auto_20160703_1523.py
new file mode 100644
index 00000000..508653b0
--- /dev/null
+++ b/machines/migrations/0005_auto_20160703_1523.py
@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('machines', '0004_auto_20160703_1451'),
+ ]
+
+ operations = [
+ migrations.RenameField(
+ model_name='interface',
+ old_name='name',
+ new_name='dns',
+ ),
+ migrations.AddField(
+ model_name='machine',
+ name='name',
+ field=models.CharField(blank=True, unique=True, max_length=255, help_text='Optionnel'),
+ ),
+ ]
diff --git a/machines/migrations/0006_auto_20160703_1813.py b/machines/migrations/0006_auto_20160703_1813.py
new file mode 100644
index 00000000..3ea3f6d9
--- /dev/null
+++ b/machines/migrations/0006_auto_20160703_1813.py
@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('machines', '0005_auto_20160703_1523'),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name='interface',
+ name='details',
+ field=models.CharField(max_length=255, blank=True),
+ ),
+ migrations.AlterField(
+ model_name='interface',
+ name='dns',
+ field=models.CharField(max_length=255, unique=True),
+ ),
+ ]
diff --git a/machines/migrations/0007_auto_20160703_1816.py b/machines/migrations/0007_auto_20160703_1816.py
new file mode 100644
index 00000000..443490cc
--- /dev/null
+++ b/machines/migrations/0007_auto_20160703_1816.py
@@ -0,0 +1,19 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('machines', '0006_auto_20160703_1813'),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name='interface',
+ name='ipv6',
+ field=models.GenericIPAddressField(null=True, protocol='IPv6'),
+ ),
+ ]
diff --git a/machines/migrations/0008_remove_interface_ipv6.py b/machines/migrations/0008_remove_interface_ipv6.py
new file mode 100644
index 00000000..ed706a26
--- /dev/null
+++ b/machines/migrations/0008_remove_interface_ipv6.py
@@ -0,0 +1,18 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('machines', '0007_auto_20160703_1816'),
+ ]
+
+ operations = [
+ migrations.RemoveField(
+ model_name='interface',
+ name='ipv6',
+ ),
+ ]
diff --git a/machines/migrations/0009_auto_20160703_2358.py b/machines/migrations/0009_auto_20160703_2358.py
new file mode 100644
index 00000000..125c5f02
--- /dev/null
+++ b/machines/migrations/0009_auto_20160703_2358.py
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+import macaddress.fields
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('machines', '0008_remove_interface_ipv6'),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name='interface',
+ name='mac_address',
+ field=macaddress.fields.MACAddressField(integer=False, max_length=17, unique=True),
+ ),
+ ]
diff --git a/machines/models.py b/machines/models.py
index 38c5af50..86e928b2 100644
--- a/machines/models.py
+++ b/machines/models.py
@@ -1,4 +1,5 @@
from django.db import models
+from django.forms import ModelForm, Form
from macaddress.fields import MACAddressField
from users.models import User
@@ -6,9 +7,10 @@ from users.models import User
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", unique=True, blank=True)
def __str__(self):
- return self.type
+ return str(self.user) + ' - ' + str(self.id) + ' - ' + str(self.name)
class MachineType(models.Model):
type = models.CharField(max_length=255)
@@ -19,17 +21,49 @@ class MachineType(models.Model):
class Interface(models.Model):
ipv4 = models.OneToOneField('IpList', on_delete=models.PROTECT, blank=True, null=True)
- ipv6 = models.GenericIPAddressField(protocol='IPv6')
- mac_address = MACAddressField()
+ #ipv6 = models.GenericIPAddressField(protocol='IPv6', null=True)
+ mac_address = MACAddressField(integer=False, unique=True)
machine = models.ForeignKey('Machine', on_delete=models.PROTECT)
- details = models.CharField(max_length=255)
- name = models.CharField(max_length=255, unique=True, blank=True)
+ details = models.CharField(max_length=255, blank=True)
+ dns = models.CharField(max_length=255, unique=True)
def __str__(self):
- return self.name
+ return self.dns
class IpList(models.Model):
- ipv4 = models.GenericIPAddressField(protocol='IPv4')
+ ipv4 = models.GenericIPAddressField(protocol='IPv4', unique=True)
def __str__(self):
- return self.type
+ return self.ipv4
+
+class EditMachineForm(ModelForm):
+ class Meta:
+ model = Machine
+ fields = '__all__'
+
+ def __init__(self, *args, **kwargs):
+ super(EditMachineForm, self).__init__(*args, **kwargs)
+ self.fields['name'].label = 'Nom de la machine (optionnel)'
+ self.fields['type'].label = 'Type de machine'
+
+class NewMachineForm(EditMachineForm):
+ class Meta(EditMachineForm.Meta):
+ fields = ['type','name']
+
+class EditInterfaceForm(ModelForm):
+ class Meta:
+ model = Interface
+ fields = '__all__'
+
+ def __init__(self, *args, **kwargs):
+ super(EditInterfaceForm, self).__init__(*args, **kwargs)
+ self.fields['dns'].label = 'Nom dns de la machine (unique)'
+ self.fields['mac_address'].label = 'Adresse mac'
+
+class AddInterfaceForm(EditInterfaceForm):
+ class Meta(EditInterfaceForm.Meta):
+ fields = ['ipv4','mac_address','dns','details']
+
+class NewInterfaceForm(EditInterfaceForm):
+ class Meta(EditInterfaceForm.Meta):
+ fields = ['mac_address','dns','details']
diff --git a/machines/templates/machines/aff_machines.html b/machines/templates/machines/aff_machines.html
new file mode 100644
index 00000000..8840a7b7
--- /dev/null
+++ b/machines/templates/machines/aff_machines.html
@@ -0,0 +1,23 @@
+
+
+
+ Nom dns |
+ Proprietaire |
+ Type |
+ Mac |
+ Ipv4 |
+ |
+
+
+ {% for machine in machine_list %}
+
+ {{ machine.dns }} |
+ {{ machine.machine.user }} |
+ {{ machine.machine.type }} |
+ {{ machine.mac_address }} |
+ {{ machine.ipv4 }} |
+ Editer |
+
+ {% endfor %}
+
+
diff --git a/machines/templates/machines/index.html b/machines/templates/machines/index.html
new file mode 100644
index 00000000..e9039ced
--- /dev/null
+++ b/machines/templates/machines/index.html
@@ -0,0 +1,12 @@
+{% extends "machines/sidebar.html" %}
+{% load bootstrap3 %}
+
+{% block title %}Machines{% endblock %}
+
+{% block content %}
+ {% include "machines/aff_machines.html" with machines_list=machines_list %}
+
+
+
+{% endblock %}
+
diff --git a/machines/templates/machines/machine.html b/machines/templates/machines/machine.html
new file mode 100644
index 00000000..2ed48537
--- /dev/null
+++ b/machines/templates/machines/machine.html
@@ -0,0 +1,19 @@
+{% extends "machines/sidebar.html" %}
+{% load bootstrap3 %}
+
+{% block title %}Création et modification de machines{% endblock %}
+
+{% block content %}
+{% bootstrap_form_errors machineform %}
+{% bootstrap_form_errors interfaceform %}
+
+
+
+
+
+{% endblock %}
diff --git a/machines/templates/machines/sidebar.html b/machines/templates/machines/sidebar.html
new file mode 100644
index 00000000..a7464bb5
--- /dev/null
+++ b/machines/templates/machines/sidebar.html
@@ -0,0 +1,5 @@
+{% extends "base.html" %}
+
+{% block sidebar %}
+ Nouvelle machine
+{% endblock %}
diff --git a/machines/urls.py b/machines/urls.py
new file mode 100644
index 00000000..72351b0a
--- /dev/null
+++ b/machines/urls.py
@@ -0,0 +1,10 @@
+from django.conf.urls import url
+
+from . import views
+
+urlpatterns = [
+ url(r'^new_machine/(?P[0-9]+)$', views.new_machine, name='new-machine'),
+ url(r'^edit_machine/(?P[0-9]+)$', views.edit_machine, name='edit-machine'),
+ url(r'^new_interface/(?P[0-9]+)$', views.new_interface, name='new-interface'),
+ url(r'^$', views.index, name='index'),
+]
diff --git a/machines/views.py b/machines/views.py
index 91ea44a2..7abb8182 100644
--- a/machines/views.py
+++ b/machines/views.py
@@ -1,3 +1,72 @@
-from django.shortcuts import render
+# App de gestion des machines pour re2o
+# Gabriel Détraz
+# Gplv2
+from django.shortcuts import render, redirect
+from django.shortcuts import render_to_response, get_object_or_404
+from django.core.context_processors import csrf
+from django.template import Context, RequestContext, loader
+from django.contrib import messages
-# Create your views here.
+from .models import NewMachineForm, EditMachineForm, EditInterfaceForm, AddInterfaceForm, NewInterfaceForm
+from .models import Machine, Interface
+from users.models import User
+
+def form(ctx, template, request):
+ c = ctx
+ c.update(csrf(request))
+ return render_to_response(template, c, context_instance=RequestContext(request))
+
+def new_machine(request, userid):
+ try:
+ user = User.objects.get(pk=userid)
+ except User.DoesNotExist:
+ messages.error(request, u"Utilisateur inexistant" )
+ return redirect("/machines/")
+ machine = NewMachineForm(request.POST or None)
+ interface = NewInterfaceForm(request.POST or None)
+ if machine.is_valid() and interface.is_valid():
+ new_machine = machine.save(commit=False)
+ new_machine.user = user
+ new_machine.save()
+ new_interface = interface.save(commit=False)
+ new_interface.machine = new_machine
+ new_interface.save()
+ messages.success(request, "La machine a été crée")
+ return redirect("/users/")
+ return form({'machineform': machine, 'interfaceform': interface}, 'machines/machine.html', request)
+
+def edit_machine(request, interfaceid):
+ try:
+ interface = Interface.objects.get(pk=interfaceid)
+ except Interface.DoesNotExist:
+ messages.error(request, u"Interface inexistante" )
+ return redirect("/machines")
+ machine_form = EditMachineForm(request.POST or None, instance=interface.machine)
+ interface_form = EditInterfaceForm(request.POST or None, instance=interface)
+ if machine_form.is_valid() and interface_form.is_valid():
+ machine_form.save()
+ interface_form.save()
+ messages.success(request, "La machine a été modifiée")
+ return redirect("/users/")
+ return form({'machineform': machine_form, 'interfaceform': interface_form}, 'machines/machine.html', request)
+
+def new_interface(request, machineid):
+ try:
+ machine = Machine.objects.get(pk=machineid)
+ except Machine.DoesNotExist:
+ messages.error(request, u"Machine inexistante" )
+ return redirect("/machines")
+ interface_form = AddInterfaceForm(request.POST or None)
+ machine_form = EditMachineForm(request.POST or None, instance=machine)
+ if interface_form.is_valid() and machine_form.is_valid():
+ machine_form.save()
+ new_interface = interface_form.save(commit=False)
+ new_interface.machine = machine
+ new_interface.save()
+ messages.success(request, "L'interface a été ajoutée")
+ return redirect("/users/")
+ return form({'machineform': machine_form, 'interfaceform': interface_form}, 'machines/machine.html', request)
+
+def index(request):
+ machine_list = Interface.objects.order_by('pk')
+ return render(request, 'machines/index.html', {'machine_list': machine_list})
diff --git a/re2o/urls.py b/re2o/urls.py
index 89fd34cd..96ae98be 100644
--- a/re2o/urls.py
+++ b/re2o/urls.py
@@ -23,5 +23,6 @@ urlpatterns = [
url(r'^users/', include('users.urls', namespace='users')),
url(r'^search/', include('search.urls', namespace='search')),
url(r'^cotisations/', include('cotisations.urls', namespace='cotisations')),
+ url(r'^machines/', include('machines.urls', namespace='machines')),
#url(r'^logs/', include('logs.urls', namespace='logs')),
]
diff --git a/search/templates/search/index.html b/search/templates/search/index.html
index 30b9f8e6..79dc6f20 100644
--- a/search/templates/search/index.html
+++ b/search/templates/search/index.html
@@ -10,18 +10,7 @@
{% endif%}
{% if machine_list %}
Résultats dans les machines :
-
-
-
- Nom |
-
-
- {% for machine in machine_list %}
-
- {{ machine.name }} |
-
- {% endfor %}
-
+ {% include "machines/aff_machines.html" with machine_list=machine_list %}
{% endif %}
{% if facture_list %}
Résultats dans les factures :
diff --git a/search/views.py b/search/views.py
index f4fb9537..b15a2ecd 100644
--- a/search/views.py
+++ b/search/views.py
@@ -8,7 +8,7 @@ from django.template import Context, RequestContext, loader
from django.db.models import Q
from users.models import User, Ban
-from machines.models import Machine
+from machines.models import Machine, Interface
from cotisations.models import Facture
from search.models import SearchForm
from users.views import has_access
@@ -31,8 +31,8 @@ def search(request):
connexion = []
for user in users:
connexion.append([user, has_access(user)])
- machines = None
query = Q(user__pseudo__icontains = search) | Q(user__name__icontains = search) | Q(user__surname__icontains = search)
+ machines = Interface.objects.filter(machine=Machine.objects.filter(query)) | Interface.objects.filter(Q(dns__icontains = search))
factures = Facture.objects.filter(query)
bans = Ban.objects.filter(query)
return form({'users_list': connexion, 'machine_list' : machines, 'facture_list' : factures, 'ban_list' : bans}, 'search/index.html',request)