aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlban Gruin2017-01-28 13:20:23 +0100
committerAlban Gruin2017-01-28 13:20:23 +0100
commitebb7c3bf0dc3eef2efa3f4add60ccfc7dc063248 (patch)
tree089d20c831cfc7f02b2b65beca6f78b8f1a59943
parentdfb352540ce5a66ef475f0c6d855fc2f322d553a (diff)
Traitement des semaines avec un group by, semblant diviser par deux le temps de traitement
-rw-r--r--models.py8
-rw-r--r--views.py19
2 files changed, 12 insertions, 15 deletions
diff --git a/models.py b/models.py
index bcd9215..aa7be24 100644
--- a/models.py
+++ b/models.py
@@ -119,6 +119,7 @@ class Room(models.Model):
class CourseManager(GroupedManager):
def __get_weeks(self, qs):
extractYear = ExtractYear("begin")
+ qs = qs.group_by("groups", "year", "week").order_by("groups__name", "year", "week")
if connection.vendor == "postgresql":
return qs.annotate(week=ExtractWeek("begin"), year=extractYear)
@@ -128,11 +129,8 @@ class CourseManager(GroupedManager):
def get_courses_for_group(self, group, **filters):
return self.get_queryset().filter(Q(groups__td__isnull=True) | Q(groups__td=group.td), Q(groups__tp__isnull=True) | Q(groups__tp=group.tp), groups__mention=group.mention, groups__subgroup=group.subgroup, **filters).order_by("begin")
- def get_weeks(self):
- return self.__get_weeks(self.get_queryset())
-
- def get_courses_for_group_and_weeks(self, group, **filters):
- qs = self.get_courses_for_group(group, **filters)
+ def get_weeks(self, **criteria):
+ qs = self.get_queryset().filter(**criteria)
return self.__get_weeks(qs)
diff --git a/views.py b/views.py
index 24473d1..e883e9d 100644
--- a/views.py
+++ b/views.py
@@ -18,18 +18,17 @@ def index(request):
current_year, current_week, _ = timezone.now().isocalendar()
start, _ = get_week(current_year, current_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:
- courses = Course.objects.get_courses_for_group(group, begin__gte=start).values("begin").annotate(count=Count("begin"))
- weeks = []
- for course in courses:
- year, week, _ = course["begin"].isocalendar()
- date, _ = get_week(year, week)
-
- if date not in weeks:
- weeks.append(date)
-
- group.weeks = weeks
+ for group_week in groups_weeks:
+ if group.corresponds_to(group_week["groups__timetable"], group_week["groups__mention"], group_week["groups__subgroup"], group_week["groups__td"], group_week["groups__tp"]):
+ if not hasattr(group, "weeks"):
+ group.weeks = []
+
+ date, _ = get_week(group_week["year"], group_week["week"])
+ if date not in group.weeks:
+ group.weeks.append(date)
return render(request, "index.html", {"timetables": timetables, "groups": groups})