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

Ajoute la recherche et l'edition des machines

This commit is contained in:
chirac 2016-07-04 02:48:24 +02:00
parent 0fe552f029
commit 30536f6aee
18 changed files with 332 additions and 26 deletions

View file

@ -3,7 +3,7 @@ from django.contrib import admin
from .models import Machine, MachineType, IpList, Interface from .models import Machine, MachineType, IpList, Interface
class MachineAdmin(admin.ModelAdmin): class MachineAdmin(admin.ModelAdmin):
list_display = ('user','type') list_display = ('user','name','type')
class MachineTypeAdmin(admin.ModelAdmin): class MachineTypeAdmin(admin.ModelAdmin):
list_display = ('type',) list_display = ('type',)
@ -12,7 +12,7 @@ class IpListAdmin(admin.ModelAdmin):
list_display = ('ipv4',) list_display = ('ipv4',)
class InterfaceAdmin(admin.ModelAdmin): 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(Machine, MachineAdmin)
admin.site.register(MachineType, MachineTypeAdmin) admin.site.register(MachineType, MachineTypeAdmin)

View file

@ -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),
),
]

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', '0003_auto_20160703_1450'),
]
operations = [
migrations.AlterField(
model_name='iplist',
name='ipv4',
field=models.GenericIPAddressField(protocol='IPv4', unique=True),
),
]

View file

@ -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'),
),
]

View file

@ -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),
),
]

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', '0006_auto_20160703_1813'),
]
operations = [
migrations.AlterField(
model_name='interface',
name='ipv6',
field=models.GenericIPAddressField(null=True, protocol='IPv6'),
),
]

View file

@ -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',
),
]

View file

@ -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),
),
]

View file

@ -1,4 +1,5 @@
from django.db import models from django.db import models
from django.forms import ModelForm, Form
from macaddress.fields import MACAddressField from macaddress.fields import MACAddressField
from users.models import User from users.models import User
@ -6,9 +7,10 @@ from users.models import User
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) type = models.ForeignKey('MachineType', on_delete=models.PROTECT)
name = models.CharField(max_length=255, help_text="Optionnel", unique=True, blank=True)
def __str__(self): def __str__(self):
return self.type return str(self.user) + ' - ' + str(self.id) + ' - ' + str(self.name)
class MachineType(models.Model): class MachineType(models.Model):
type = models.CharField(max_length=255) type = models.CharField(max_length=255)
@ -19,17 +21,49 @@ class MachineType(models.Model):
class Interface(models.Model): class Interface(models.Model):
ipv4 = models.OneToOneField('IpList', on_delete=models.PROTECT, blank=True, null=True) ipv4 = models.OneToOneField('IpList', on_delete=models.PROTECT, blank=True, null=True)
ipv6 = models.GenericIPAddressField(protocol='IPv6') #ipv6 = models.GenericIPAddressField(protocol='IPv6', null=True)
mac_address = MACAddressField() mac_address = MACAddressField(integer=False, unique=True)
machine = models.ForeignKey('Machine', on_delete=models.PROTECT) machine = models.ForeignKey('Machine', on_delete=models.PROTECT)
details = models.CharField(max_length=255) details = models.CharField(max_length=255, blank=True)
name = models.CharField(max_length=255, unique=True, blank=True) dns = models.CharField(max_length=255, unique=True)
def __str__(self): def __str__(self):
return self.name return self.dns
class IpList(models.Model): class IpList(models.Model):
ipv4 = models.GenericIPAddressField(protocol='IPv4') ipv4 = models.GenericIPAddressField(protocol='IPv4', unique=True)
def __str__(self): 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']

View file

@ -0,0 +1,23 @@
<table class="table table-striped">
<thead>
<tr>
<th>Nom dns</th>
<th>Proprietaire</th>
<th>Type</th>
<th>Mac</th>
<th>Ipv4</th>
<th></th>
</tr>
</thead>
{% for machine in machine_list %}
<tr>
<td>{{ machine.dns }}</td>
<td>{{ machine.machine.user }}</td>
<td>{{ machine.machine.type }}</td>
<td>{{ machine.mac_address }}</td>
<td>{{ machine.ipv4 }}</td>
<td><a class="btn btn-primary btn-sm" role="button" href="{% url 'machines:edit-machine' machine.id %}"><i class="glyphicon glyphicon-tree-conifer"></i> Editer</a></td>
</tr>
{% endfor %}
</table>

View file

@ -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 %}
<br />
<br />
<br />
{% endblock %}

View file

@ -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 %}
<form class="form" method="post">
{% csrf_token %}
{% bootstrap_form machineform %}
{% bootstrap_form interfaceform %}
{% bootstrap_button "Créer ou modifier" button_type="submit" icon="star" %}
</form>
<br />
<br />
<br />
{% endblock %}

View file

@ -0,0 +1,5 @@
{% extends "base.html" %}
{% block sidebar %}
<p><a href="{% url "search:search" %}">Nouvelle machine</a></p>
{% endblock %}

10
machines/urls.py Normal file
View file

@ -0,0 +1,10 @@
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^new_machine/(?P<userid>[0-9]+)$', views.new_machine, name='new-machine'),
url(r'^edit_machine/(?P<interfaceid>[0-9]+)$', views.edit_machine, name='edit-machine'),
url(r'^new_interface/(?P<machineid>[0-9]+)$', views.new_interface, name='new-interface'),
url(r'^$', views.index, name='index'),
]

View file

@ -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})

View file

@ -23,5 +23,6 @@ urlpatterns = [
url(r'^users/', include('users.urls', namespace='users')), url(r'^users/', include('users.urls', namespace='users')),
url(r'^search/', include('search.urls', namespace='search')), url(r'^search/', include('search.urls', namespace='search')),
url(r'^cotisations/', include('cotisations.urls', namespace='cotisations')), url(r'^cotisations/', include('cotisations.urls', namespace='cotisations')),
url(r'^machines/', include('machines.urls', namespace='machines')),
#url(r'^logs/', include('logs.urls', namespace='logs')), #url(r'^logs/', include('logs.urls', namespace='logs')),
] ]

View file

@ -10,18 +10,7 @@
{% endif%} {% endif%}
{% if machine_list %} {% if machine_list %}
<h2>Résultats dans les machines : </h2> <h2>Résultats dans les machines : </h2>
<table class="table table-striped"> {% include "machines/aff_machines.html" with machine_list=machine_list %}
<thead>
<tr>
<th>Nom</th>
</tr>
</thead>
{% for machine in machine_list %}
<tr>
<td>{{ machine.name }}</td>
</tr>
{% endfor %}
</table>
{% endif %} {% endif %}
{% if facture_list %} {% if facture_list %}
<h2>Résultats dans les factures : </h2> <h2>Résultats dans les factures : </h2>

View file

@ -8,7 +8,7 @@ from django.template import Context, RequestContext, loader
from django.db.models import Q from django.db.models import Q
from users.models import User, Ban from users.models import User, Ban
from machines.models import Machine from machines.models import Machine, Interface
from cotisations.models import Facture from cotisations.models import Facture
from search.models import SearchForm from search.models import SearchForm
from users.views import has_access from users.views import has_access
@ -31,8 +31,8 @@ def search(request):
connexion = [] connexion = []
for user in users: for user in users:
connexion.append([user, has_access(user)]) connexion.append([user, has_access(user)])
machines = None
query = Q(user__pseudo__icontains = search) | Q(user__name__icontains = search) | Q(user__surname__icontains = search) 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) factures = Facture.objects.filter(query)
bans = Ban.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) return form({'users_list': connexion, 'machine_list' : machines, 'facture_list' : factures, 'ban_list' : bans}, 'search/index.html',request)