From 5c51a568819ed0cb2fedefc35321f1e7748ce537 Mon Sep 17 00:00:00 2001 From: Med Date: Wed, 5 Jul 2017 00:47:05 +0200 Subject: [PATCH] Ajoute la gestion des adhesions dans la bdd --- users/admin.py | 6 +- users/migrations/0007_adhesion.py | 25 ++++++ users/migrations/0008_auto_20170705_0001.py | 21 +++++ users/models.py | 14 +++ users/templates/users/aff_adhesion.html | 46 ++++++++++ users/templates/users/aff_users.html | 7 +- users/templates/users/index_adhesion.html | 39 ++++++++ users/templates/users/profil.html | 12 +++ users/templates/users/sidebar.html | 8 ++ users/urls.py | 7 ++ users/views.py | 98 ++++++++++++++++++++- 11 files changed, 279 insertions(+), 4 deletions(-) create mode 100644 users/migrations/0007_adhesion.py create mode 100644 users/migrations/0008_auto_20170705_0001.py create mode 100644 users/templates/users/aff_adhesion.html create mode 100644 users/templates/users/index_adhesion.html diff --git a/users/admin.py b/users/admin.py index 3281d87..a50c430 100644 --- a/users/admin.py +++ b/users/admin.py @@ -25,7 +25,7 @@ from django.contrib.auth.models import Group from django.contrib.auth.admin import UserAdmin as BaseUserAdmin from reversion.admin import VersionAdmin -from .models import User, Right, ListRight, Clef, Request +from .models import User, Right, Adhesion, ListRight, Clef, Request from .forms import UserChangeForm, UserCreationForm @@ -48,6 +48,9 @@ class RightAdmin(VersionAdmin): class ClefAdmin(VersionAdmin): list_display = ('proprio', 'nom') +class AdhesionAdmin(VersionAdmin): + list_display = ('annee_debut', 'annee_fin') + class ListRightAdmin(VersionAdmin): list_display = ('listright',) @@ -82,6 +85,7 @@ admin.site.register(User, UserAdmin) admin.site.register(Request, RequestAdmin) admin.site.register(ListRight, ListRightAdmin) admin.site.register(Right, RightAdmin) +admin.site.register(Adhesion, AdhesionAdmin) admin.site.register(Clef, ClefAdmin) # Now register the new UserAdmin... admin.site.unregister(User) diff --git a/users/migrations/0007_adhesion.py b/users/migrations/0007_adhesion.py new file mode 100644 index 0000000..c1f6e8e --- /dev/null +++ b/users/migrations/0007_adhesion.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.2 on 2017-07-04 22:00 +from __future__ import unicode_literals + +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0006_clef_commentaire'), + ] + + operations = [ + migrations.CreateModel( + name='Adhesion', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('annee_debut', models.IntegerField(unique=True)), + ('annee_fin', models.IntegerField(unique=True)), + ('adherent', models.ManyToManyField(blank=True, null=True, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/users/migrations/0008_auto_20170705_0001.py b/users/migrations/0008_auto_20170705_0001.py new file mode 100644 index 0000000..808baf8 --- /dev/null +++ b/users/migrations/0008_auto_20170705_0001.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.2 on 2017-07-04 22:01 +from __future__ import unicode_literals + +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0007_adhesion'), + ] + + operations = [ + migrations.AlterField( + model_name='adhesion', + name='adherent', + field=models.ManyToManyField(blank=True, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/users/models.py b/users/models.py index ad1d267..55cea2b 100644 --- a/users/models.py +++ b/users/models.py @@ -146,6 +146,10 @@ class User(AbstractBaseUser): # Simplest version again return True + @property + def is_adherent(self): + return self in Adhesion.objects.all().order_by('annee_debut').reverse().first().adherent.all() + def get_admin_right(self): admin, created = ListRight.objects.get_or_create(listright="admin") return admin @@ -213,6 +217,11 @@ class Clef(models.Model): proprio = models.ForeignKey('User', on_delete=models.PROTECT, blank=True, null=True) commentaire = models.CharField(max_length=255, null=True, blank=True) +class Adhesion(models.Model): + annee_debut = models.IntegerField(unique=True) + annee_fin = models.IntegerField(unique=True) + adherent = models.ManyToManyField('User', blank=True) + class BaseInfoForm(ModelForm): def __init__(self, *args, **kwargs): super(BaseInfoForm, self).__init__(*args, **kwargs) @@ -259,6 +268,11 @@ class ClefForm(ModelForm): model = Clef fields = '__all__' +class AdhesionForm(ModelForm): + class Meta: + model = Adhesion + fields = ['annee_debut', 'annee_fin'] + class RightForm(ModelForm): def __init__(self, *args, **kwargs): super(RightForm, self).__init__(*args, **kwargs) diff --git a/users/templates/users/aff_adhesion.html b/users/templates/users/aff_adhesion.html new file mode 100644 index 0000000..ac4f627 --- /dev/null +++ b/users/templates/users/aff_adhesion.html @@ -0,0 +1,46 @@ +{% 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 adhesion in adhesion_list %} + + + + + + {% endfor %} +
Année débutAnnée fin
{{ adhesion.annee_debut }}{{ adhesion.annee_fin }} + {% if is_bureau %} + {% include 'buttons/edit.html' with href='users:edit-adhesion' id=adhesion.id %} + {% include 'buttons/suppr.html' with href='users:del-adhesion' id=adhesion.id %} + {% endif %} + {% include 'buttons/history.html' with href='users:history' name='adhesion' id=adhesion.id %} +
diff --git a/users/templates/users/aff_users.html b/users/templates/users/aff_users.html index bd40e11..5a2a3c8 100644 --- a/users/templates/users/aff_users.html +++ b/users/templates/users/aff_users.html @@ -34,6 +34,7 @@ with this program; if not, write to the Free Software Foundation, Inc., Pseudo Mail Max emprunts + Adhérent Profil @@ -44,7 +45,11 @@ with this program; if not, write to the Free Software Foundation, Inc., {{ user.pseudo }} {{ user.email }} {{ user.maxemprunt }} - + {% if user.is_adherent %} + Oui + {% else %} + Non + {% endif %} diff --git a/users/templates/users/index_adhesion.html b/users/templates/users/index_adhesion.html new file mode 100644 index 0000000..876730e --- /dev/null +++ b/users/templates/users/index_adhesion.html @@ -0,0 +1,39 @@ +{% extends "users/sidebar.html" %} +{% 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 %} + +{% load bootstrap3 %} + +{% block title %}Adhesion{% endblock %} + +{% block content %} +

Liste des adhesion

+ {% if is_bureau %} + Ajouter une adhesion + {% endif %} + {% include "users/aff_adhesion.html" with adhesion_list=adhesion_list %} +
+
+
+{% endblock %} diff --git a/users/templates/users/profil.html b/users/templates/users/profil.html index 18d6fce..861bb0c 100644 --- a/users/templates/users/profil.html +++ b/users/templates/users/profil.html @@ -103,6 +103,18 @@ with this program; if not, write to the Free Software Foundation, Inc., Dernière connexion {{ user.last_login }} + + Adherent pour l'année en cours + {% if user.is_adherent %} + Oui + {% else %} + Non + {% endif %} + {% if not user.is_adherent and is_bureau %} + + Adhérer + {% endif %} +

Emprunts

{% if is_perm or is_bureau %} diff --git a/users/templates/users/sidebar.html b/users/templates/users/sidebar.html index 543d87e..8e7f115 100644 --- a/users/templates/users/sidebar.html +++ b/users/templates/users/sidebar.html @@ -33,9 +33,17 @@ with this program; if not, write to the Free Software Foundation, Inc., {% endif %} {% if is_perm %} + + Utilisateurs dans la base + + Adhérents + + + Adhesions + Droits diff --git a/users/urls.py b/users/urls.py index b47f14a..c924679 100644 --- a/users/urls.py +++ b/users/urls.py @@ -31,6 +31,7 @@ urlpatterns = [ url(r'^state/(?P[0-9]+)$', views.state, name='state'), url(r'^password/(?P[0-9]+)$', views.password, name='password'), url(r'^profil/(?P[0-9]+)$', views.profil, name='profil'), + url(r'^adherer/(?P[0-9]+)$', views.adherer, name='adherer'), url(r'^mon_profil/$', views.mon_profil, name='mon-profil'), url(r'^add_listright/$', views.add_listright, name='add-listright'), url(r'^edit_listright/(?P[0-9]+)$', views.edit_listright, name='edit-listright'), @@ -41,6 +42,11 @@ urlpatterns = [ url(r'^del_clef/(?P[0-9]+)$', views.del_clef, name='del-clef'), url(r'^index_clef/$', views.index_clef, name='index-clef'), url(r'^history/(?Pclef)/(?P[0-9]+)$', views.history, name='history'), + url(r'^add_adhesion/$', views.add_adhesion, name='add-adhesion'), + url(r'^edit_adhesion/(?P[0-9]+)$', views.edit_adhesion, name='edit-adhesion'), + url(r'^del_adhesion/(?P[0-9]+)$', views.del_adhesion, name='del-adhesion'), + url(r'^index_adhesion/$', views.index_adhesion, name='index-adhesion'), + url(r'^history/(?Padhesion)/(?P[0-9]+)$', views.history, name='history'), url(r'^add_right/(?P[0-9]+)$', views.add_right, name='add-right'), url(r'^del_right/$', views.del_right, name='del-right'), url(r'^process/(?P[a-z0-9]{32})/$', views.process, name='process'), @@ -48,6 +54,7 @@ urlpatterns = [ url(r'^history/(?Puser)/(?P[0-9]+)$', views.history, name='history'), url(r'^history/(?Plistright)/(?P[0-9]+)$', views.history, name='history'), url(r'^$', views.index, name='index'), + url(r'^index_ajour/$', views.index_ajour, name='index-ajour'), ] diff --git a/users/views.py b/users/views.py index 3ce234e..ff7094a 100644 --- a/users/views.py +++ b/users/views.py @@ -40,7 +40,7 @@ from django.db import transaction from reversion.models import Version from reversion import revisions as reversion from users.models import User, Request, ListRight, Right, DelListRightForm, NewListRightForm, ListRightForm, RightForm, DelRightForm -from users.models import InfoForm, BaseInfoForm, StateForm, Clef, ClefForm +from users.models import InfoForm, BaseInfoForm, StateForm, Clef, ClefForm, Adhesion, AdhesionForm from users.forms import PassForm, ResetPasswordForm from media.models import Emprunt @@ -308,7 +308,7 @@ def del_clef(request, clefid): with transaction.atomic(), reversion.create_revision(): clef_instance.delete() reversion.set_user(request.user) - messages.success(request, "La clef a été détruit") + messages.success(request, "La clef a été détruite") return redirect("/users/index_clef") return form({'objet': clef_instance, 'objet_name': 'clef'}, 'users/delete.html', request) @@ -317,6 +317,59 @@ def index_clef(request): clef_list = Clef.objects.all().order_by('nom') return render(request, 'users/index_clef.html', {'clef_list':clef_list}) + +@login_required +@permission_required('bureau') +def add_adhesion(request): + adhesion = AdhesionForm(request.POST or None) + if adhesion.is_valid(): + with transaction.atomic(), reversion.create_revision(): + adhesion.save() + reversion.set_user(request.user) + reversion.set_comment("Création") + messages.success(request, "L'adhesion a été ajouté") + return redirect("/users/index_adhesion/") + return form({'userform': adhesion}, 'users/user.html', request) + +@login_required +@permission_required('bureau') +def edit_adhesion(request, adhesionid): + try: + adhesion_instance = Adhesion.objects.get(pk=adhesionid) + except Adhesion.DoesNotExist: + messages.error(request, u"Entrée inexistante" ) + return redirect("/users/index_adhesion/") + adhesion = AdhesionForm(request.POST or None, instance=adhesion_instance) + if adhesion.is_valid(): + with transaction.atomic(), reversion.create_revision(): + adhesion.save() + reversion.set_user(request.user) + reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in adhesion.changed_data)) + messages.success(request, "Adhesion modifiée") + return redirect("/users/index_adhesion/") + return form({'userform': adhesion}, 'users/user.html', request) + +@login_required +@permission_required('bureau') +def del_adhesion(request, adhesionid): + try: + adhesion_instance = Adhesion.objects.get(pk=adhesionid) + except Adhesion.DoesNotExist: + messages.error(request, u"Entrée inexistante" ) + return redirect("/users/index_adhesion/") + if request.method == "POST": + with transaction.atomic(), reversion.create_revision(): + adhesion_instance.delete() + reversion.set_user(request.user) + messages.success(request, "La adhesion a été détruit") + return redirect("/users/index_adhesion") + return form({'objet': adhesion_instance, 'objet_name': 'adhesion'}, 'users/delete.html', request) + +@login_required +def index_adhesion(request): + adhesion_list = Adhesion.objects.all() + return render(request, 'users/index_adhesion.html', {'adhesion_list':adhesion_list}) + @login_required @permission_required('perm') def index(request): @@ -334,6 +387,23 @@ def index(request): users_list = paginator.page(paginator.num_pages) return render(request, 'users/index.html', {'users_list': users_list}) +@login_required +@permission_required('perm') +def index_ajour(request): + """ Affiche l'ensemble des users, need droit admin """ + users_list = Adhesion.objects.all().order_by('annee_debut').reverse().first().adherent.all().order_by('name') + paginator = Paginator(users_list, PAGINATION_NUMBER) + page = request.GET.get('page') + try: + users_list = paginator.page(page) + except PageNotAnInteger: + # If page is not an integer, deliver first page. + users_list = paginator.page(1) + except EmptyPage: + # If page is out of range (e.g. 9999), deliver last page of results. + users_list = paginator.page(paginator.num_pages) + return render(request, 'users/index.html', {'users_list': users_list}) + @login_required def history(request, object, id): """ Affichage de l'historique : (acl, argument) @@ -353,6 +423,12 @@ def history(request, object, id): except Clef.DoesNotExist: messages.error(request, "Utilisateur inexistant") return redirect("/users/") + elif object == 'adhesion': + try: + object_instance = Adhesion.objects.get(pk=id) + except Adhesion.DoesNotExist: + messages.error(request, "Utilisateur inexistant") + return redirect("/users/") elif object == 'listright': try: object_instance = ListRight.objects.get(pk=id) @@ -401,6 +477,24 @@ def profil(request, userid): } ) +@login_required +@permission_required('bureau') +def adherer(request, userid): + try: + users = User.objects.get(pk=userid) + except User.DoesNotExist: + messages.error(request, "Utilisateur inexistant") + return redirect("/users/") + adh_annee = Adhesion.objects.all().order_by('annee_debut').reverse().first() + with transaction.atomic(), reversion.create_revision(): + reversion.set_user(request.user) + adh_annee.adherent.add(users) + adh_annee.save() + reversion.set_comment("Adhesion de %s" % users) + messages.success(request, "Adhesion effectuee") + return redirect("/users/profil/" + userid) + + def reset_password(request): userform = ResetPasswordForm(request.POST or None) if userform.is_valid():