From df2b69af6923aa9ef00e4e3070a28906b17593fc Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Fri, 20 Jan 2017 14:40:19 +0100 Subject: Déduplication de la fonction get_week() --- management/commands/_private.py | 7 +++---- utils.py | 10 ++++++++++ views.py | 8 +------- 3 files changed, 14 insertions(+), 11 deletions(-) create mode 100644 utils.py diff --git a/management/commands/_private.py b/management/commands/_private.py index 65772a4..f5a3ebc 100644 --- a/management/commands/_private.py +++ b/management/commands/_private.py @@ -1,6 +1,8 @@ from bs4 import BeautifulSoup from django.utils import timezone + from edt.models import Group, Room, Course +from edt.utils import get_week import datetime import requests @@ -23,10 +25,7 @@ def add_time(date, time): return date + delta def delete_courses_in_week(timetable, year, week): - start = timezone.make_aware(datetime.datetime.strptime( - "{0}-W{1:02d}-1".format(year, week), "%Y-W%W-%w")) - end = start + datetime.timedelta(weeks=1) - + start, end = get_week(year, week) Course.objects.filter(begin__gte=start, begin__lt=end, timetable=timetable).delete() diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..5cc6648 --- /dev/null +++ b/utils.py @@ -0,0 +1,10 @@ +from django.utils import timezone + +import datetime + +def get_week(year, week): + start = timezone.make_aware(datetime.datetime.strptime( + "{0}-W{1:02d}-1".format(year, week), "%Y-W%W-%w")) + end = start + datetime.timedelta(weeks=1) + + return start, end diff --git a/views.py b/views.py index d11c026..99255c7 100644 --- a/views.py +++ b/views.py @@ -1,18 +1,12 @@ from django.http import HttpResponse from django.db.models import Count from django.shortcuts import get_object_or_404, render, render_to_response -from django.utils import timezone from edt.models import Timetable, Group, Course +from edt.utils import get_week import datetime -def get_week(year, week): - start = timezone.make_aware(datetime.datetime.strptime("{0}-W{1:02d}-1".format(year, week), "%Y-W%W-%w")) - end = start + datetime.timedelta(weeks=1) - - return start, end - def index(request): timetables = Timetable.objects.all() groups = Group.objects.filter(tp__isnull=False).order_by("name") -- cgit v1.2.1