diff --git a/users/forms.py b/users/forms.py index 2934ffed..1d39fad0 100644 --- a/users/forms.py +++ b/users/forms.py @@ -26,6 +26,7 @@ from django import forms from django.contrib.auth.forms import ReadOnlyPasswordHashField from django.core.validators import MinLengthValidator +from django.utils import timezone from .models import User, ServiceUser, get_admin_right @@ -137,3 +138,13 @@ class ServiceUserChangeForm(forms.ModelForm): class ResetPasswordForm(forms.Form): pseudo = forms.CharField(label=u'Pseudo', max_length=255) email = forms.EmailField(max_length=255) + +class MassArchiveForm(forms.Form): + date = forms.DateTimeField(help_text='%d/%m/%y') + + def clean(self): + cleaned_data=super(MassArchiveForm, self).clean() + date = cleaned_data.get("date") + if date: + if date>timezone.now(): + raise forms.ValidationError("Impossible d'archiver des utilisateurs dont la fin d'accès se situe dans le futur !") diff --git a/users/models.py b/users/models.py index 5069ac30..6b5f4082 100644 --- a/users/models.py +++ b/users/models.py @@ -223,7 +223,7 @@ class User(AbstractBaseUser): return date_max def end_whitelist(self): - """ Renvoie la date de fin de ban d'un user, False sinon """ + """ Renvoie la date de fin de whitelist d'un user, False sinon """ date_max = Whitelist.objects.filter(user=self).aggregate(models.Max('date_end'))['date_end__max'] return date_max @@ -252,6 +252,19 @@ class User(AbstractBaseUser): return self.state == User.STATE_ACTIVE \ and not self.is_ban() and (self.is_adherent() or self.is_whitelisted()) + def end_access(self): + """ Renvoie la date de fin normale d'accès (adhésion ou whiteliste)""" + if not self.end_adhesion(): + if not self.end_whitelist(): + return None + else: + return self.end_whitelist() + else: + if not self.end_whitelist(): + return self.end_adhesion() + else: + return max(self.end_adhesion(), self.end_whitelist()) + def user_interfaces(self): return Interface.objects.filter(machine__in=Machine.objects.filter(user=self, active=True)) diff --git a/users/templates/users/mass_archive.html b/users/templates/users/mass_archive.html new file mode 100644 index 00000000..fad77631 --- /dev/null +++ b/users/templates/users/mass_archive.html @@ -0,0 +1,46 @@ +{% 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 %} Utilisateurs à archiver{% endblock %} + +{% block content %} +
+ {% csrf_token %} + {% bootstrap_form userform %} + + +
+ + + +

Les utilisateurs suivant seront archivés ({{ to_archive_list|length }}):

+ {% include "users/aff_users.html" with users_list=to_archive_list %} +
+
+
+{% endblock %} + diff --git a/users/templates/users/sidebar.html b/users/templates/users/sidebar.html index 9e571bea..f3b28029 100644 --- a/users/templates/users/sidebar.html +++ b/users/templates/users/sidebar.html @@ -55,6 +55,10 @@ with this program; if not, write to the Free Software Foundation, Inc., Retirer un droit + + + Archiver en masse + {% endif %} {% endif %} {% endblock %} diff --git a/users/urls.py b/users/urls.py index f9df4260..f0a8b058 100644 --- a/users/urls.py +++ b/users/urls.py @@ -50,6 +50,7 @@ urlpatterns = [ url(r'^mon_profil/$', views.mon_profil, name='mon-profil'), url(r'^process/(?P[a-z0-9]{32})/$', views.process, name='process'), url(r'^reset_password/$', views.reset_password, name='reset-password'), + url(r'^mass_archive/$', views.mass_archive, name='mass-archive'), url(r'^history/(?Puser)/(?P[0-9]+)$', views.history, name='history'), url(r'^history/(?Pban)/(?P[0-9]+)$', views.history, name='history'), url(r'^history/(?Pwhitelist)/(?P[0-9]+)$', views.history, name='history'), diff --git a/users/views.py b/users/views.py index 53579920..505828ba 100644 --- a/users/views.py +++ b/users/views.py @@ -43,7 +43,7 @@ from users.models import DelRightForm, BanForm, WhitelistForm, DelSchoolForm, De from users.models import EditInfoForm, InfoForm, BaseInfoForm, StateForm, RightForm, SchoolForm, ListRightForm from cotisations.models import Facture from machines.models import Machine, Interface -from users.forms import PassForm, ResetPasswordForm +from users.forms import MassArchiveForm, PassForm, ResetPasswordForm from machines.views import unassign_ips, assign_ips from re2o.login import hashNT @@ -455,6 +455,26 @@ def del_listright(request): return redirect("/users/index_listright/") return form({'userform': listright}, 'users/user.html', request) +@login_required +@permission_required('bureau') +def mass_archive(request): + """ Permet l'archivage massif""" + to_archive_date = MassArchiveForm(request.POST or None) + to_archive_list = [] + if to_archive_date.is_valid(): + date = to_archive_date.cleaned_data['date'] + to_archive_list = [user for user in User.objects.exclude(state=User.STATE_ARCHIVE) if not user.end_access() or user.end_access() < date] + if "valider" in request.POST: + for user in to_archive_list: + archive(user) + with transaction.atomic(), reversion.create_revision(): + user.state=User.STATE_ARCHIVE + user.save() + reversion.set_comment("Archivage") + messages.success(request, "%s users ont été archivés" % len(to_archive_list)) + return redirect("/users/") + return form({'userform': to_archive_date, 'to_archive_list': to_archive_list}, 'users/mass_archive.html', request) + @login_required @permission_required('cableur') def index(request):