diff options
author | Alban Gruin | 2017-02-05 16:12:51 +0100 |
---|---|---|
committer | Alban Gruin | 2017-02-05 16:12:51 +0100 |
commit | 706983d73996637148d640281b5a5e97f217b9b5 (patch) | |
tree | 287ee3507aba1e30cfb96edb2b309c7bfee31681 | |
parent | 04162486502fee0fc6aa26aecd6919bde4cd3f73 (diff) |
Mise en commun de la fonction de récupération des semaines
-rw-r--r-- | management/commands/sendmails.py | 7 | ||||
-rw-r--r-- | utils.py | 7 | ||||
-rw-r--r-- | 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) @@ -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")) @@ -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) |