From d90ebbe6e625ebe1da6b637c3225ce9eb45ad184 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Tue, 19 Dec 2017 21:11:18 +0100 Subject: [PATCH] Premier draft d'une app de controle --- logs/templates/logs/aff_summary.html | 1 + logs/templates/logs/index.html | 3 + logs/templates/logs/sidebar.html | 2 +- preferences/forms.py | 29 +++++- preferences/migrations/0027_jauge.py | 28 ++++++ .../migrations/0028_jauge_direction.py | 20 ++++ preferences/models.py | 48 +++++++++- .../templates/preferences/aff_jauge.html | 54 +++++++++++ .../preferences/display_preferences.html | 6 ++ preferences/urls.py | 12 +++ preferences/views.py | 93 ++++++++++++++++++- 11 files changed, 288 insertions(+), 8 deletions(-) create mode 100644 preferences/migrations/0027_jauge.py create mode 100644 preferences/migrations/0028_jauge_direction.py create mode 100644 preferences/templates/preferences/aff_jauge.html diff --git a/logs/templates/logs/aff_summary.html b/logs/templates/logs/aff_summary.html index 65f71aca..f69061d4 100644 --- a/logs/templates/logs/aff_summary.html +++ b/logs/templates/logs/aff_summary.html @@ -28,6 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc., {% load logs_extra %} + diff --git a/logs/templates/logs/index.html b/logs/templates/logs/index.html index a120a531..2d9582ef 100644 --- a/logs/templates/logs/index.html +++ b/logs/templates/logs/index.html @@ -28,6 +28,9 @@ with this program; if not, write to the Free Software Foundation, Inc., {% block title %}Statistiques{% endblock %} {% block content %} +

Centre de contrôle

+ Aucune jauge objet +

Actions effectuées

{% include "logs/aff_summary.html" with versions_list=versions_list %}
diff --git a/logs/templates/logs/sidebar.html b/logs/templates/logs/sidebar.html index 4137741f..bfa7ac09 100644 --- a/logs/templates/logs/sidebar.html +++ b/logs/templates/logs/sidebar.html @@ -28,7 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc., {% if is_cableur %} - Résumé + Centre de contrôle diff --git a/preferences/forms.py b/preferences/forms.py index 51cbb885..894ed52b 100644 --- a/preferences/forms.py +++ b/preferences/forms.py @@ -28,8 +28,13 @@ from __future__ import unicode_literals from django.forms import ModelForm, Form from django import forms from .models import OptionalUser, OptionalMachine, OptionalTopologie -from .models import GeneralOption, AssoOption, MailMessageOption, Service - +from .models import ( + GeneralOption, + AssoOption, + MailMessageOption, + Service, + Jauge +) class EditOptionalUserForm(ModelForm): """Formulaire d'édition des options de l'user. (solde, telephone..)""" @@ -177,3 +182,23 @@ class DelServiceForm(Form): label="Enregistrements service actuels", widget=forms.CheckboxSelectMultiple ) + + +class JaugeForm(ModelForm): + """Edition, ajout de jauges""" + class Meta: + model = Jauge + fields = '__all__' + + def __init__(self, *args, **kwargs): + prefix = kwargs.pop('prefix', self.Meta.model.__name__) + super(JaugeForm, self).__init__(*args, prefix=prefix, **kwargs) + + +class DelJaugeForm(Form): + """Suppression de services sur la page d'accueil""" + services = forms.ModelMultipleChoiceField( + queryset=Jauge.objects.all(), + label="Enregistrements jauges actuels", + widget=forms.CheckboxSelectMultiple + ) diff --git a/preferences/migrations/0027_jauge.py b/preferences/migrations/0027_jauge.py new file mode 100644 index 00000000..c3321e64 --- /dev/null +++ b/preferences/migrations/0027_jauge.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2017-12-16 04:33 +from __future__ import unicode_literals + +import django.core.validators +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('contenttypes', '0002_remove_content_type_name'), + ('preferences', '0026_auto_20171216_0401'), + ] + + operations = [ + migrations.CreateModel( + name='Jauge', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('level_value', models.IntegerField(blank=True, null=True)), + ('level_percentage', models.IntegerField(blank=True, null=True, validators=[django.core.validators.MaxValueValidator(100)])), + ('comment', models.CharField(max_length=255)), + ('objet', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType')), + ], + ), + ] diff --git a/preferences/migrations/0028_jauge_direction.py b/preferences/migrations/0028_jauge_direction.py new file mode 100644 index 00000000..4eec2b5c --- /dev/null +++ b/preferences/migrations/0028_jauge_direction.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2017-12-16 05:06 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('preferences', '0027_jauge'), + ] + + operations = [ + migrations.AddField( + model_name='jauge', + name='direction', + field=models.CharField(choices=[('croissant', 'croissant'), ('decroissant', 'decroissant')], default='croissant', max_length=32), + ), + ] diff --git a/preferences/models.py b/preferences/models.py index 6a793a24..8b798b5b 100644 --- a/preferences/models.py +++ b/preferences/models.py @@ -24,8 +24,13 @@ Reglages généraux, machines, utilisateurs, mail, general pour l'application. """ from __future__ import unicode_literals - +import django.apps +from django.contrib.contenttypes.models import ContentType +from django.core.validators import MaxValueValidator from django.db import models +from django.utils.functional import cached_property +from django.apps import apps + from cotisations.models import Paiement @@ -150,3 +155,44 @@ class MailMessageOption(models.Model): welcome_mail_fr = models.TextField(default="") welcome_mail_en = models.TextField(default="") + + +class Jauge(models.Model): + """Jauge de contrôle + Possibilité de classer en pourcentage ou valeur brute / + croissant ou décroissant + Exemple : nombre de facture non controlées""" + + DIRECTION_CHOICES = ( + ('croissant', 'croissant'), + ('decroissant', 'decroissant'), + ) + + objet = models.ForeignKey(ContentType) + level_value = models.IntegerField(blank=True, null=True) + level_percentage = models.IntegerField( + blank=True, + null=True, + validators=[MaxValueValidator(100)] + ) + direction = models.CharField( + max_length=32, + choices=DIRECTION_CHOICES, + default='croissant' + ) + comment = models.CharField(max_length=255) + + @cached_property + def jauge_state(self): + all_object = apps.get_model(self.objet.app_label, self.objet.name).objects.all().count() + if self.direction == "croissant": + if self.level_value: + if all_object > self.level_value: + return False + #if self.level_percentage: + # if all_objet + else: + if self.level_value: + if all_object < self.level_value: + return False + return True diff --git a/preferences/templates/preferences/aff_jauge.html b/preferences/templates/preferences/aff_jauge.html new file mode 100644 index 00000000..0c24a6a9 --- /dev/null +++ b/preferences/templates/preferences/aff_jauge.html @@ -0,0 +1,54 @@ +{% comment %} +Re2o est un logiciel d'administration développé initiallement au rezometz. Il +se veut agnostique au réseau considéré, de manière à être installable en +quelques clics. + +Copyright © 2017 Gabriel Détraz +Copyright © 2017 Goulven Kermarec +Copyright © 2017 Augustin Lemesle + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +{% endcomment %} + +
+ + + + + + + + + + + + {% for jauge in jauge_list %} + + + + + + + + {% endfor %} +
ObjetJauge en pourcentageJauge en valeurSensValeur recherchéeEtat
{{ jauge.objet }}{{ jauge.level_percentage }}%{{ jauge.level_value }}{{ jauge.direction }} + {{ jauge.comment }}{{ jauge.jauge_state }} + + {% if is_admin %} + {% include 'buttons/edit.html' with href='preferences:edit-jauge' id=jauge.id %} + {% endif %} + {% include 'buttons/history.html' with href='preferences:history' name='jauge' id=jauge.id %} +
+ diff --git a/preferences/templates/preferences/display_preferences.html b/preferences/templates/preferences/display_preferences.html index 4858e414..890a42a2 100644 --- a/preferences/templates/preferences/display_preferences.html +++ b/preferences/templates/preferences/display_preferences.html @@ -191,6 +191,12 @@ with this program; if not, write to the Free Software Foundation, Inc., Supprimer un ou plusieurs service {% endif %} {% include "preferences/aff_service.html" with service_list=service_list %} +

Gestion des jauges

+ {% if is_infra %} + Ajouter une jauge + Supprimer une jauge + {% endif %} + {% include "preferences/aff_jauge.html" with jauge_list=jauge_list %}


diff --git a/preferences/urls.py b/preferences/urls.py index f10d25a0..ce3054a2 100644 --- a/preferences/urls.py +++ b/preferences/urls.py @@ -73,5 +73,17 @@ urlpatterns = [ views.history, name='history' ), + url(r'^add_jauge/$', views.add_jauge, name='add-jauge'), + url( + r'^edit_jauge/(?P[0-9]+)$', + views.edit_jauge, + name='edit-jauge' + ), + url(r'^del_jauge/$', views.del_jauge, name='del-jauge'), + url( + r'^history/(?Pjauge)/(?P[0-9]+)$', + views.history, + name='history' + ), url(r'^$', views.display_options, name='display-options'), ] diff --git a/preferences/views.py b/preferences/views.py index 493c1d14..d413c921 100644 --- a/preferences/views.py +++ b/preferences/views.py @@ -42,9 +42,22 @@ from reversion.models import Version from reversion import revisions as reversion from re2o.views import form -from .forms import ServiceForm, DelServiceForm -from .models import Service, OptionalUser, OptionalMachine, AssoOption -from .models import MailMessageOption, GeneralOption, OptionalTopologie +from .forms import ( + ServiceForm, + DelServiceForm, + JaugeForm, + DelJaugeForm +) +from .models import ( + Service, + OptionalUser, + OptionalMachine, + AssoOption, + MailMessageOption, + GeneralOption, + OptionalTopologie, + Jauge +) from . import models from . import forms @@ -61,6 +74,7 @@ def display_options(request): assooptions, _created = AssoOption.objects.get_or_create() mailmessageoptions, _created = MailMessageOption.objects.get_or_create() service_list = Service.objects.all() + jauge_list = Jauge.objects.all() return form({ 'useroptions': useroptions, 'machineoptions': machineoptions, @@ -68,7 +82,8 @@ def display_options(request): 'generaloptions': generaloptions, 'assooptions': assooptions, 'mailmessageoptions': mailmessageoptions, - 'service_list': service_list + 'service_list': service_list, + 'jauge_list': jauge_list, }, 'preferences/display_preferences.html', request) @@ -175,6 +190,76 @@ def del_services(request): request ) +@login_required +@permission_required('admin') +def add_jauge(request): + """Ajout d'un service de la page d'accueil""" + jauge = JaugeForm(request.POST or None) + if jauge.is_valid(): + with transaction.atomic(), reversion.create_revision(): + jauge.save() + reversion.set_user(request.user) + reversion.set_comment("Création") + messages.success(request, "Cette jauge a été ajoutée") + return redirect(reverse('preferences:display-options')) + return form( + {'preferenceform': jauge}, + 'preferences/preferences.html', + request + ) + + +@login_required +@permission_required('admin') +def edit_jauge(request, jaugeid): + """Edition des jauge affichés sur la page d'accueil""" + try: + jauge_instance = Jauge.objects.get(pk=jaugeid) + except Jauge.DoesNotExist: + messages.error(request, u"Entrée inexistante") + return redirect(reverse('preferences:display-options')) + jauge = JaugeForm(request.POST or None, instance=jauge_instance) + if jauge.is_valid(): + with transaction.atomic(), reversion.create_revision(): + jauge.save() + reversion.set_user(request.user) + reversion.set_comment( + "Champs modifié(s) : %s" % ', '.join( + field for field in jauge.changed_data + ) + ) + messages.success(request, "Jauge modifié") + return redirect(reverse('preferences:display-options')) + return form( + {'preferenceform': jauge}, + 'preferences/preferences.html', + request + ) + + +@login_required +@permission_required('admin') +def del_jauge(request): + """Suppression d'un service de la page d'accueil""" + jauge = DelJaugeForm(request.POST or None) + if jauge.is_valid(): + jauge_dels = jauge.cleaned_data['jauge'] + for jauge_del in jauge_dels: + try: + with transaction.atomic(), reversion.create_revision(): + jauge_del.delete() + reversion.set_user(request.user) + messages.success(request, "Le jauge a été supprimée") + except ProtectedError: + messages.error(request, "Erreur la jauge\ + suivant %s ne peut être supprimé" % jauge_del) + return redirect(reverse('preferences:display-options')) + return form( + {'preferenceform': jauge}, + 'preferences/preferences.html', + request + ) + @login_required @permission_required('cableur')