From 706983d73996637148d640281b5a5e97f217b9b5 Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Sun, 5 Feb 2017 16:12:51 +0100 Subject: Mise en commun de la fonction de récupération des semaines --- management/commands/sendmails.py | 7 ++----- utils.py | 7 +++++++ views.py | 17 +++++++---------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/management/commands/sendmails.py b/management/commands/sendmails.py index a134d80..b700990 100644 --- a/management/commands/sendmails.py +++ b/management/commands/sendmails.py @@ -5,7 +5,7 @@ from django.template import Context, loader from django.conf import settings from edt.models import Group, Subscription, Course -from edt.utils import get_week, group_courses +from edt.utils import get_current_or_next_week, get_week, group_courses import datetime @@ -16,10 +16,7 @@ class Command(BaseCommand): def handle(self, *args, **options): translation.activate(settings.LANGUAGE_CODE) - year, week, day = timezone.now().isocalendar() - if day >= 6: - year, week, _ = (timezone.now() + datetime.timedelta(weeks=1)).isocalendar() - + year, week = get_current_or_next_week() start, end = get_week(year, week) subscriptions = Subscription.objects.filter(active=True) diff --git a/utils.py b/utils.py index 3479b47..1a177b4 100644 --- a/utils.py +++ b/utils.py @@ -2,6 +2,13 @@ from django.utils import timezone import datetime +def get_current_or_next_week(): + year, week, day = timezone.now().isocalendar() + if day >= 6: + year, week, _ = (timezone.now() + datetime.timedelta(weeks=1)).isocalendar() + + return year, week + def get_week(year, week): start = timezone.make_aware(datetime.datetime.strptime( "{0}-W{1:02d}-1".format(year, week), "%Y-W%W-%w")) diff --git a/views.py b/views.py index fca7e8e..9d441b8 100644 --- a/views.py +++ b/views.py @@ -8,7 +8,7 @@ from django.template import Context, loader from edt.forms import SubscribeForm from edt.models import Timetable, LastUpdate, Group, Subscription, Course -from edt.utils import get_week, group_courses +from edt.utils import get_current_or_next_week, get_week, group_courses import datetime @@ -16,8 +16,8 @@ def index(request): timetables = Timetable.objects.all() groups = Group.objects.filter(tp__isnull=False).order_by("name") - current_year, current_week, _ = timezone.now().isocalendar() - start, _ = get_week(current_year, current_week) + year, week = get_current_or_next_week() + start, _ = get_week(year, week) groups_weeks = Course.objects.get_weeks(begin__gte=start).values("groups__timetable", "groups__mention", "groups__subgroup", "groups__td", "groups__tp", "year", "week") for group in groups: @@ -30,15 +30,14 @@ def index(request): if date not in group.weeks: group.weeks.append(date) - group.weeks.sort() + if hasattr(group, "weeks"): + group.weeks.sort() return render(request, "index.html", {"timetables": timetables, "groups": groups}) def timetable(request, timetable_slug, group_slug, year=None, week=None): if year is None or week is None: - year, week, day = timezone.now().isocalendar() - if day >= 6: - year, week, _ = (timezone.now() + datetime.timedelta(weeks=1)).isocalendar() + year, week = get_current_or_next_week() start, end = get_week(int(year), int(week)) @@ -79,9 +78,7 @@ def confirm_subscription(request, token): subscription.active = True subscription.save() - year, week, day = timezone.now().isocalendar() - if day >= 6: - year, week, _ = (timezone.now() + datetime.timedelta(weeks=1)).isocalendar() + year, week = get_current_or_next_week() return redirect("timetable", group_slug=subscription.group.slug, year=year, week=week) -- cgit v1.2.1