diff --git a/users/forms.py b/users/forms.py index d8c6755..9f3fda1 100644 --- a/users/forms.py +++ b/users/forms.py @@ -1,7 +1,7 @@ from django import forms from django.contrib.auth.models import User, Group from dal import autocomplete -from .models import School, CotisationHistory, WhiteListHistory +from .models import School, CotisationHistory, WhiteListHistory, BanishmentHistory from preferences.models import PaymentMethod class LoginForm(forms.Form): @@ -114,6 +114,15 @@ class addWhiteListHistoryForm(forms.ModelForm): model = WhiteListHistory fields = ("duration", "reason") + +class addBanishmentHistoryForm(forms.ModelForm): + """ + Form to add a :class:`users.models.BanishmentHistory` to user (:class:`django.contrib.auth.models.User`). + """ + class Meta: + model = BanishmentHistory + fields = ("end_date", "reason") + class SchoolForm(forms.ModelForm): """ Form to add and edit a :class:`users.models.School`. diff --git a/users/migrations/0013_banishmenthistory_historicalbanishmenthistory.py b/users/migrations/0013_banishmenthistory_historicalbanishmenthistory.py new file mode 100644 index 0000000..516fcef --- /dev/null +++ b/users/migrations/0013_banishmenthistory_historicalbanishmenthistory.py @@ -0,0 +1,54 @@ +# Generated by Django 2.1 on 2019-10-06 09:25 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion +import simple_history.models + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('users', '0012_auto_20190925_0125'), + ] + + operations = [ + migrations.CreateModel( + name='BanishmentHistory', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('ban_date', models.DateTimeField(auto_now_add=True, verbose_name='Date du banissement')), + ('end_date', models.DateTimeField(verbose_name='Date de fin')), + ('reason', models.CharField(blank=True, max_length=255, verbose_name='Raison')), + ('coopeman', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='banishment_made', to=settings.AUTH_USER_MODEL)), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL, verbose_name='Client')), + ], + options={ + 'verbose_name': 'Historique banissement', + 'verbose_name_plural': 'Historique banissements', + }, + ), + migrations.CreateModel( + name='HistoricalBanishmentHistory', + fields=[ + ('id', models.IntegerField(auto_created=True, blank=True, db_index=True, verbose_name='ID')), + ('ban_date', models.DateTimeField(blank=True, editable=False, verbose_name='Date du banissement')), + ('end_date', models.DateTimeField(verbose_name='Date de fin')), + ('reason', models.CharField(blank=True, max_length=255, verbose_name='Raison')), + ('history_id', models.AutoField(primary_key=True, serialize=False)), + ('history_change_reason', models.CharField(max_length=100, null=True)), + ('history_date', models.DateTimeField()), + ('history_type', models.CharField(choices=[('+', 'Created'), ('~', 'Changed'), ('-', 'Deleted')], max_length=1)), + ('coopeman', models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to=settings.AUTH_USER_MODEL)), + ('history_user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL)), + ('user', models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to=settings.AUTH_USER_MODEL)), + ], + options={ + 'verbose_name': 'historical Historique banissement', + 'ordering': ('-history_date', '-history_id'), + 'get_latest_by': 'history_date', + }, + bases=(simple_history.models.HistoricalChanges, models.Model), + ), + ] diff --git a/users/models.py b/users/models.py index 8311602..4cf3c38 100644 --- a/users/models.py +++ b/users/models.py @@ -152,6 +152,9 @@ class Profile(models.Model): """ Test if a client is adherent. """ + banishments = self.user.banishmenthistory_set.order_by("-end_date") + if banishments and banishments[0].end_date > timezone.now(): + return False if(self.cotisationEnd and self.cotisationEnd > timezone.now()): return True else: @@ -227,4 +230,34 @@ def str_user(self): return self.username + " (" + self.first_name + " " + self.last_name + ", " + str(self.profile.balance) + "€, " + fin + ")" -User.add_to_class("__str__", str_user) \ No newline at end of file +User.add_to_class("__str__", str_user) + +class BanishmentHistory(models.Model): + """ + Stores banishment history. + """ + class Meta: + verbose_name = "Historique banissement" + verbose_name_plural = "Historique banissements" + + user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name="Client") + """ + Client (:class:`django.contrib.auth.models.User`). + """ + ban_date = models.DateTimeField(auto_now_add=True, verbose_name="Date du banissement") + """ + Date of the beginning of the whitelist. + """ + end_date = models.DateTimeField(verbose_name="Date de fin") + """ + End date of the whitelist. + """ + coopeman = models.ForeignKey(User, on_delete=models.PROTECT, related_name="banishment_made") + """ + User (:class:`django.contrib.auth.models.User`) who registered the cotisation. + """ + reason = models.CharField(max_length=255, verbose_name="Raison", blank=True) + """ + Reason of the whitelist + """ + history = HistoricalRecords() \ No newline at end of file diff --git a/users/templates/users/profile.html b/users/templates/users/profile.html index c7b3de7..0f6786c 100644 --- a/users/templates/users/profile.html +++ b/users/templates/users/profile.html @@ -13,6 +13,7 @@
  • {% if self %} Mes cotisations {% else %} Cotisations {% endif %}
  • {% if self %} Mes accès gracieux {% else %} Accès gracieux {% endif %}
  • +
  • {% if self %} Mes banissements {% else %} Banissements {% endif %}
  • {% endblock %} {% block content %} @@ -278,4 +279,35 @@ + +
    +
    +

    {{ self | yesno:"Mes banissements,Banissements"}}

    +
    +
    + {% if perms.users.add_banishmenthistory %} + Ajouter un banissement

    + {% endif %} +
    + + + + + + + + + + {% for banishment in banishments %} + + + + + + {% endfor %} + +
    Date de débutDate de finRaison
    {{banishment.ban_date}}{{banishment.end_date}}{{ banishment.reason }}
    +
    +
    +
    {%endblock%} \ No newline at end of file diff --git a/users/urls.py b/users/urls.py index 413178d..43c643d 100644 --- a/users/urls.py +++ b/users/urls.py @@ -45,4 +45,5 @@ urlpatterns = [ path('exportCSV', views.export_csv, name="exportCSV"), path('switchActivateUser/', views.switch_activate_user, name="switchActivateUser"), path('genUserInfos/', views.gen_user_infos, name="genUserInfos"), + path('addBanishmentHistory/', views.addBanishmentHistory, name="addBanishmentHistory"), ] diff --git a/users/views.py b/users/views.py index 0f03e3c..147a9c8 100644 --- a/users/views.py +++ b/users/views.py @@ -27,8 +27,8 @@ import os from django_tex.views import render_to_pdf from coopeV3.acl import admin_required, superuser_required, self_or_has_perm, active_required -from .models import CotisationHistory, WhiteListHistory, School -from .forms import CreateUserForm, LoginForm, CreateGroupForm, EditGroupForm, SelectUserForm, GroupsEditForm, EditPasswordForm, addCotisationHistoryForm, addCotisationHistoryForm, addWhiteListHistoryForm, SelectNonAdminUserForm, SelectNonSuperUserForm, SchoolForm, ExportForm +from .models import CotisationHistory, WhiteListHistory, School, BanishmentHistory +from .forms import CreateUserForm, LoginForm, CreateGroupForm, EditGroupForm, SelectUserForm, GroupsEditForm, EditPasswordForm, addCotisationHistoryForm, addCotisationHistoryForm, addWhiteListHistoryForm, SelectNonAdminUserForm, SelectNonSuperUserForm, SchoolForm, ExportForm, addBanishmentHistoryForm from gestion.models import Reload, Consumption, ConsumptionHistory, MenuHistory from preferences.models import GeneralPreferences @@ -132,6 +132,7 @@ def profile(request, pk): whitelists = WhiteListHistory.objects.filter(user=user) reloads = Reload.objects.filter(customer=user).order_by('-date')[:5] consumptionsChart = Consumption.objects.filter(customer=user) + banishments = BanishmentHistory.objects.filter(user=user) products_pre = [] quantities_pre = [] for ch in consumptionsChart: @@ -162,6 +163,7 @@ def profile(request, pk): "quantities": quantities, "lastConsumptions": lastConsumptions, "lastMenus": lastMenus, + "banishments": banishments }) @active_required @@ -654,6 +656,26 @@ def addWhiteListHistory(request, pk): return redirect(reverse('users:profile', kwargs={'pk':user.pk})) return render(request, "form.html", {"form": form, "form_title": "Ajout d'un accès gracieux pour " + user.username, "form_button": "Ajouter", "form_button_icon": "plus-square"}) +########## Banishment ########## + +@active_required +@login_required +@permission_required('users.add_banishmenthistory') +def addBanishmentHistory(request, pk): + """ + Displays a :class:`users.forms.addBanishmenthisotryForm` to add a :class:`~users.models.BanishmentHistory` to the requested user (:class:`django.contrib.auth.models.User`). + """ + user = get_object_or_404(User, pk=pk) + form = addBanishmentHistoryForm(request.POST or None) + if(form.is_valid()): + ban = form.save(commit=False) + ban.user = user + ban.coopeman = request.user + ban.save() + messages.success(request, "Le banissement a bien été ajouté") + return redirect(reverse('users:profile', kwargs={'pk':user.pk})) + return render(request, "form.html", {"form": form, "form_title": "Ajout d'un banissement pour " + user.username, "form_button": "Ajouter", "form_button_icon": "plus-square", "extra_html": "La date doit être au format JJ/MM/AAAA HH:ii"}) + ########## Schools ########## @active_required