From b8c3dda95b094859691265040debf5e487262e02 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Sun, 16 Aug 2020 00:35:13 +0200 Subject: [PATCH] Replace timezone.now().date() by date.today() --- apps/member/tables.py | 7 ++-- apps/permission/backends.py | 6 ++-- .../tests/test_permission_denied.py | 32 +++++++++---------- .../tests/test_permission_queries.py | 10 +++--- apps/permission/views.py | 5 ++- apps/wei/tables.py | 7 ++-- apps/wei/tests/test_wei_registration.py | 4 +-- apps/wei/views.py | 17 +++++----- 8 files changed, 46 insertions(+), 42 deletions(-) diff --git a/apps/member/tables.py b/apps/member/tables.py index 16f436e1..86fc386a 100644 --- a/apps/member/tables.py +++ b/apps/member/tables.py @@ -1,9 +1,10 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later +from datetime import date + import django_tables2 as tables from django.contrib.auth.models import User -from django.utils import timezone from django.utils.translation import gettext_lazy as _ from django.urls import reverse_lazy from django.utils.html import format_html @@ -106,8 +107,8 @@ class MembershipTable(tables.Table): empty_membership = Membership( club=record.club, user=record.user, - date_start=timezone.now().date(), - date_end=timezone.now().date(), + date_start=date.today(), + date_end=date.today(), fee=0, ) if PermissionBackend.check_perm(get_current_authenticated_user(), diff --git a/apps/permission/backends.py b/apps/permission/backends.py index c5a43b1e..cde6998d 100644 --- a/apps/permission/backends.py +++ b/apps/permission/backends.py @@ -1,6 +1,8 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later +from datetime import date + from django.contrib.auth.backends import ModelBackend from django.contrib.auth.models import User, AnonymousUser from django.contrib.contenttypes.models import ContentType @@ -43,7 +45,7 @@ class PermissionBackend(ModelBackend): for role in membership.roles.all(): for perm in role.permissions.filter(type=t, mask__rank__lte=get_current_session().get("permission_mask", -1)).all(): if not perm.permanent: - if membership.date_start > timezone.now().date() or membership.date_end < timezone.now().date(): + if membership.date_start > date.today() or membership.date_end < date.today(): continue perm.membership = membership perms.append(perm) @@ -80,7 +82,7 @@ class PermissionBackend(ModelBackend): F=F, Q=Q, now=timezone.now(), - today=timezone.now().date(), + today=date.today(), ) yield permission diff --git a/apps/permission/tests/test_permission_denied.py b/apps/permission/tests/test_permission_denied.py index 80bf4129..ebeaa133 100644 --- a/apps/permission/tests/test_permission_denied.py +++ b/apps/permission/tests/test_permission_denied.py @@ -1,7 +1,7 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later -from datetime import timedelta +from datetime import timedelta, date from django.contrib.auth.models import User from django.test import TestCase @@ -84,18 +84,18 @@ class TestPermissionDenied(TestCase): def test_create_wei_bus(self): wei = WEIClub.objects.create( - membership_start=timezone.now().date(), - date_start=timezone.now().date() + timedelta(days=1), - date_end=timezone.now().date() + timedelta(days=1), + membership_start=date.today(), + date_start=date.today() + timedelta(days=1), + date_end=date.today() + timedelta(days=1), ) response = self.client.get(reverse("wei:add_bus", kwargs=dict(pk=wei.pk))) self.assertEqual(response.status_code, 403) def test_create_wei_team(self): wei = WEIClub.objects.create( - membership_start=timezone.now().date(), - date_start=timezone.now().date() + timedelta(days=1), - date_end=timezone.now().date() + timedelta(days=1), + membership_start=date.today(), + date_start=date.today() + timedelta(days=1), + date_end=date.today() + timedelta(days=1), ) bus = Bus.objects.create(wei=wei) response = self.client.get(reverse("wei:add_team", kwargs=dict(pk=bus.pk))) @@ -103,27 +103,27 @@ class TestPermissionDenied(TestCase): def test_create_1a_weiregistration(self): wei = WEIClub.objects.create( - membership_start=timezone.now().date(), - date_start=timezone.now().date() + timedelta(days=1), - date_end=timezone.now().date() + timedelta(days=1), + membership_start=date.today(), + date_start=date.today() + timedelta(days=1), + date_end=date.today() + timedelta(days=1), ) response = self.client.get(reverse("wei:wei_register_1A", kwargs=dict(wei_pk=wei.pk))) self.assertEqual(response.status_code, 403) def test_create_old_weiregistration(self): wei = WEIClub.objects.create( - membership_start=timezone.now().date(), - date_start=timezone.now().date() + timedelta(days=1), - date_end=timezone.now().date() + timedelta(days=1), + membership_start=date.today(), + date_start=date.today() + timedelta(days=1), + date_end=date.today() + timedelta(days=1), ) response = self.client.get(reverse("wei:wei_register_2A", kwargs=dict(wei_pk=wei.pk))) self.assertEqual(response.status_code, 403) def test_validate_weiregistration(self): wei = WEIClub.objects.create( - membership_start=timezone.now().date(), - date_start=timezone.now().date() + timedelta(days=1), - date_end=timezone.now().date() + timedelta(days=1), + membership_start=date.today(), + date_start=date.today() + timedelta(days=1), + date_end=date.today() + timedelta(days=1), ) registration = WEIRegistration.objects.create(wei=wei, user=self.user, birth_date="2000-01-01") response = self.client.get(reverse("wei:validate_registration", kwargs=dict(pk=registration.pk))) diff --git a/apps/permission/tests/test_permission_queries.py b/apps/permission/tests/test_permission_queries.py index 913893e7..e0af9cf0 100644 --- a/apps/permission/tests/test_permission_queries.py +++ b/apps/permission/tests/test_permission_queries.py @@ -1,6 +1,8 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later +from datetime import date + from django.contrib.auth.models import User from django.core.exceptions import FieldError from django.db.models import F, Q @@ -22,14 +24,14 @@ class PermissionQueryTestCase(TestCase): NoteUser.objects.create(user=user) wei = WEIClub.objects.create( name="wei", - date_start=timezone.now().date(), - date_end=timezone.now().date(), + date_start=date.today(), + date_end=date.today(), ) NoteClub.objects.create(club=wei) weiregistration = WEIRegistration.objects.create( user=user, wei=wei, - birth_date=timezone.now().date(), + birth_date=date.today(), ) bus = Bus.objects.create( name="bus", @@ -68,7 +70,7 @@ class PermissionQueryTestCase(TestCase): F=F, Q=Q, now=timezone.now(), - today=timezone.now().date(), + today=date.today(), ) try: instanced.update_query() diff --git a/apps/permission/views.py b/apps/permission/views.py index 6a5c9b12..2262b49a 100644 --- a/apps/permission/views.py +++ b/apps/permission/views.py @@ -7,7 +7,6 @@ from django.contrib.auth.mixins import LoginRequiredMixin from django.core.exceptions import PermissionDenied from django.db.models import Q from django.forms import HiddenInput -from django.utils import timezone from django.utils.translation import gettext_lazy as _ from django.views.generic import UpdateView, TemplateView, CreateView from member.models import Membership @@ -95,8 +94,8 @@ class RightsView(TemplateView): if self.request.user.is_authenticated: special_memberships = Membership.objects.filter( - date_start__lte=timezone.now().date(), - date_end__gte=timezone.now().date(), + date_start__lte=date.today(), + date_end__gte=date.today(), ).filter(roles__in=Role.objects.filter(~(Q(name="Adhérent BDE") | Q(name="Adhérent Kfet") | Q(name="Membre de club") diff --git a/apps/wei/tables.py b/apps/wei/tables.py index 6ddfcf39..4d576b00 100644 --- a/apps/wei/tables.py +++ b/apps/wei/tables.py @@ -1,9 +1,10 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later +from datetime import date + import django_tables2 as tables from django.urls import reverse_lazy -from django.utils import timezone from django.utils.html import format_html from django.utils.translation import gettext_lazy as _ from django_tables2 import A @@ -89,8 +90,8 @@ class WEIRegistrationTable(tables.Table): get_current_authenticated_user(), "wei.add_weimembership", WEIMembership( club=record.wei, user=record.user, - date_start=timezone.now().date(), - date_end=timezone.now().date(), + date_start=date.today(), + date_end=date.today(), fee=0, registration=record, ) diff --git a/apps/wei/tests/test_wei_registration.py b/apps/wei/tests/test_wei_registration.py index de14c8b3..1e9036f0 100644 --- a/apps/wei/tests/test_wei_registration.py +++ b/apps/wei/tests/test_wei_registration.py @@ -72,7 +72,7 @@ class TestWEIRegistration(TestCase): membership_start=date(self.year, 1, 1), membership_end=date(self.year, 12, 31), year=self.year, - date_start=timezone.now().date() + timedelta(days=2), + date_start=date.today() + timedelta(days=2), date_end=date(self.year, 12, 31), ) NoteClub.objects.create(club=self.wei) @@ -772,7 +772,7 @@ class TestWEISurveyAlgorithm(TestCase): membership_start=date(self.year, 1, 1), membership_end=date(self.year, 12, 31), year=self.year, - date_start=timezone.now().date() + timedelta(days=2), + date_start=date.today() + timedelta(days=2), date_end=date(self.year, 12, 31), ) NoteClub.objects.create(club=self.wei) diff --git a/apps/wei/views.py b/apps/wei/views.py index 978c77f7..358cb14a 100644 --- a/apps/wei/views.py +++ b/apps/wei/views.py @@ -17,7 +17,6 @@ from django.http import HttpResponse from django.shortcuts import redirect from django.template.loader import render_to_string from django.urls import reverse_lazy -from django.utils import timezone from django.views import View from django.views.generic import DetailView, UpdateView, RedirectView, TemplateView from django.utils.translation import gettext_lazy as _ @@ -61,8 +60,8 @@ class WEIListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView): name="", email="weiclub@example.com", year=0, - date_start=timezone.now().date(), - date_end=timezone.now().date(), + date_start=date.today(), + date_end=date.today(), )) return context @@ -81,8 +80,8 @@ class WEICreateView(ProtectQuerysetMixin, ProtectedCreateView): name="", email="weiclub@example.com", year=0, - date_start=timezone.now().date(), - date_end=timezone.now().date(), + date_start=date.today(), + date_end=date.today(), ) def form_valid(self, form): @@ -723,8 +722,8 @@ class WEIUpdateRegistrationView(ProtectQuerysetMixin, LoginRequiredMixin, Update if PermissionBackend.check_perm(self.request.user, "wei.add_weimembership", WEIMembership( club=self.object.wei, user=self.object.user, - date_start=timezone.now().date(), - date_end=timezone.now().date(), + date_start=date.today(), + date_end=date.today(), fee=0, registration=self.object, )): @@ -774,8 +773,8 @@ class WEIValidateRegistrationView(ProtectQuerysetMixin, ProtectedCreateView): return WEIMembership( club=registration.wei, user=registration.user, - date_start=timezone.now().date(), - date_end=timezone.now().date() + timedelta(days=1), + date_start=date.today(), + date_end=date.today() + timedelta(days=1), fee=0, registration=registration, )