8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-26 08:53:46 +00:00

Permet d'ajouter un type de machine

This commit is contained in:
Gabriel Detraz 2016-07-07 13:19:03 +02:00
parent 5cd7165a72
commit 26107ca6a3
7 changed files with 55 additions and 3 deletions

View file

@ -31,3 +31,8 @@ Ensuite, effectuer les migrations. Un squelette de base de donnée, via un mysql
## Mise en production avec apache ## Mise en production avec apache
re2o/wsgi.py permet de fonctionner avec apache2 en production re2o/wsgi.py permet de fonctionner avec apache2 en production
## Fonctionnement avec les services
Pour charger les objets django, il suffit de faire User.objects.all() pour tous les users par exemple.
Cependant, pour que les services fonctionnent de manière simple, des fonctions toutes prètes existent deja pour charger la liste des users autorisés à se connecter ( has_access(user)), etc. Ces fonctions sont personnalisables, et permettent un fonctionnement très simple des services.

View file

@ -66,7 +66,7 @@ def new_facture(request, userid):
messages.success(request, "La cotisation a été prolongée pour l'adhérent %s " % user.name ) messages.success(request, "La cotisation a été prolongée pour l'adhérent %s " % user.name )
else: else:
messages.success(request, "La facture a été crée") messages.success(request, "La facture a été crée")
return redirect("/cotisations/profil/" + userid) return redirect("/users/profil/" + userid)
return form({'factureform': facture_form}, 'cotisations/facture.html', request) return form({'factureform': facture_form}, 'cotisations/facture.html', request)
def edit_facture(request, factureid): def edit_facture(request, factureid):

View file

@ -1,5 +1,6 @@
from django.forms import ModelForm, Form, ValidationError from django.forms import ModelForm, Form, ValidationError
from .models import Machine, Interface from django import forms
from .models import Machine, Interface, MachineType
class EditMachineForm(ModelForm): class EditMachineForm(ModelForm):
class Meta: class Meta:
@ -34,3 +35,19 @@ class NewInterfaceForm(EditInterfaceForm):
class Meta(EditInterfaceForm.Meta): class Meta(EditInterfaceForm.Meta):
fields = ['mac_address','dns','details'] fields = ['mac_address','dns','details']
class MachineTypeForm(ModelForm):
class Meta:
model = MachineType
fields = ['type']
def __init__(self, *args, **kwargs):
super(MachineTypeForm, self).__init__(*args, **kwargs)
self.fields['type'].label = 'Type de machine à ajouter'
class DelMachineTypeForm(ModelForm):
machinetypes = forms.ModelMultipleChoiceField(queryset=MachineType.objects.all(), label="Types de machines actuelles", widget=forms.CheckboxSelectMultiple)
class Meta:
exclude = ['type']
model = MachineType

View file

@ -5,12 +5,16 @@
{% block content %} {% block content %}
{% bootstrap_form_errors machineform %} {% bootstrap_form_errors machineform %}
{% if interfaceform %}
{% bootstrap_form_errors interfaceform %} {% bootstrap_form_errors interfaceform %}
{% endif %}
<form class="form" method="post"> <form class="form" method="post">
{% csrf_token %} {% csrf_token %}
{% bootstrap_form machineform %} {% bootstrap_form machineform %}
{% if interfaceform %}
{% bootstrap_form interfaceform %} {% bootstrap_form interfaceform %}
{% endif %}
{% bootstrap_button "Créer ou modifier" button_type="submit" icon="star" %} {% bootstrap_button "Créer ou modifier" button_type="submit" icon="star" %}
</form> </form>
<br /> <br />

View file

@ -1,4 +1,6 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block sidebar %} {% block sidebar %}
<p><a href="{% url "machines:add-machinetype" %}">Ajouter un type de machine</a></p>
<p><a href="{% url "machines:del-machinetype" %}">Retirer un type de machine</a></p>
{% endblock %} {% endblock %}

View file

@ -6,5 +6,7 @@ urlpatterns = [
url(r'^new_machine/(?P<userid>[0-9]+)$', views.new_machine, name='new-machine'), 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'^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'^new_interface/(?P<machineid>[0-9]+)$', views.new_interface, name='new-interface'),
url(r'^add_machinetype/$', views.add_machinetype, name='add-machinetype'),
url(r'^del_machinetype/$', views.del_machinetype, name='del-machinetype'),
url(r'^$', views.index, name='index'), url(r'^$', views.index, name='index'),
] ]

View file

@ -6,8 +6,9 @@ from django.shortcuts import render_to_response, get_object_or_404
from django.core.context_processors import csrf from django.core.context_processors import csrf
from django.template import Context, RequestContext, loader from django.template import Context, RequestContext, loader
from django.contrib import messages from django.contrib import messages
from django.db.models import ProtectedError
from .forms import NewMachineForm, EditMachineForm, EditInterfaceForm, AddInterfaceForm, NewInterfaceForm from .forms import NewMachineForm, EditMachineForm, EditInterfaceForm, AddInterfaceForm, NewInterfaceForm, MachineTypeForm, DelMachineTypeForm
from .models import Machine, Interface, IpList from .models import Machine, Interface, IpList
from users.models import User from users.models import User
@ -101,6 +102,27 @@ def new_interface(request, machineid):
return redirect("/machines/") return redirect("/machines/")
return form({'machineform': machine_form, 'interfaceform': interface_form}, 'machines/machine.html', request) return form({'machineform': machine_form, 'interfaceform': interface_form}, 'machines/machine.html', request)
def add_machinetype(request):
machinetype = MachineTypeForm(request.POST or None)
if machinetype.is_valid():
machinetype.save()
messages.success(request, "Ce type de machine a été ajouté")
return redirect("/machines/")
return form({'machineform': machinetype, 'interfaceform': None}, 'machines/machine.html', request)
def del_machinetype(request):
machinetype = DelMachineTypeForm(request.POST or None)
if machinetype.is_valid():
machinetype_dels = machinetype.cleaned_data['machinetypes']
for machinetype_del in machinetype_dels:
try:
machinetype_del.delete()
messages.success(request, "Le type de machine a été supprimé")
except ProtectedError:
messages.error(request, "Le type de machine %s est affectée à au moins une machine, vous ne pouvez pas le supprimer" % machinetype_del)
return redirect("/machines/")
return form({'machineform': machinetype, 'interfaceform': None}, 'machines/machine.html', request)
def index(request): def index(request):
machine_list = Interface.objects.order_by('pk') machine_list = Interface.objects.order_by('pk')
return render(request, 'machines/index.html', {'machine_list': machine_list}) return render(request, 'machines/index.html', {'machine_list': machine_list})