From 3c98885d7bebd3d566f5e9e28a9cc5019ee5c9b0 Mon Sep 17 00:00:00 2001 From: chirac Date: Thu, 7 Jul 2016 13:19:03 +0200 Subject: [PATCH] Permet d'ajouter un type de machine --- README.md | 5 +++++ cotisations/views.py | 2 +- machines/forms.py | 19 ++++++++++++++++++- machines/templates/machines/machine.html | 4 ++++ machines/templates/machines/sidebar.html | 2 ++ machines/urls.py | 2 ++ machines/views.py | 24 +++++++++++++++++++++++- 7 files changed, 55 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9af7453d..735c9083 100644 --- a/README.md +++ b/README.md @@ -31,3 +31,8 @@ Ensuite, effectuer les migrations. Un squelette de base de donnée, via un mysql ## Mise en production avec apache 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. diff --git a/cotisations/views.py b/cotisations/views.py index 6b07aecc..31ebaddc 100644 --- a/cotisations/views.py +++ b/cotisations/views.py @@ -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 ) else: 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) def edit_facture(request, factureid): diff --git a/machines/forms.py b/machines/forms.py index 941a4ba5..badb3197 100644 --- a/machines/forms.py +++ b/machines/forms.py @@ -1,5 +1,6 @@ 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 Meta: @@ -34,3 +35,19 @@ class NewInterfaceForm(EditInterfaceForm): class Meta(EditInterfaceForm.Meta): 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 diff --git a/machines/templates/machines/machine.html b/machines/templates/machines/machine.html index 2ed48537..420a37c2 100644 --- a/machines/templates/machines/machine.html +++ b/machines/templates/machines/machine.html @@ -5,12 +5,16 @@ {% block content %} {% bootstrap_form_errors machineform %} +{% if interfaceform %} {% bootstrap_form_errors interfaceform %} +{% endif %}
{% csrf_token %} {% bootstrap_form machineform %} + {% if interfaceform %} {% bootstrap_form interfaceform %} + {% endif %} {% bootstrap_button "Créer ou modifier" button_type="submit" icon="star" %}

diff --git a/machines/templates/machines/sidebar.html b/machines/templates/machines/sidebar.html index 2d6342fc..1ab05807 100644 --- a/machines/templates/machines/sidebar.html +++ b/machines/templates/machines/sidebar.html @@ -1,4 +1,6 @@ {% extends "base.html" %} {% block sidebar %} +

Ajouter un type de machine

+

Retirer un type de machine

{% endblock %} diff --git a/machines/urls.py b/machines/urls.py index 72351b0a..20966c5a 100644 --- a/machines/urls.py +++ b/machines/urls.py @@ -6,5 +6,7 @@ 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'^add_machinetype/$', views.add_machinetype, name='add-machinetype'), + url(r'^del_machinetype/$', views.del_machinetype, name='del-machinetype'), url(r'^$', views.index, name='index'), ] diff --git a/machines/views.py b/machines/views.py index d47cb518..4a71606b 100644 --- a/machines/views.py +++ b/machines/views.py @@ -6,8 +6,9 @@ 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 +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 users.models import User @@ -101,6 +102,27 @@ def new_interface(request, machineid): return redirect("/machines/") 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): machine_list = Interface.objects.order_by('pk') return render(request, 'machines/index.html', {'machine_list': machine_list})