diff options
author | Alban Gruin | 2017-01-27 22:00:41 +0100 |
---|---|---|
committer | Alban Gruin | 2017-01-27 22:00:41 +0100 |
commit | fa450f840641969cd3fea769300536ce603229cd (patch) | |
tree | 291bf584f7f1fb5bd3c9a15d2f705823c14a75e6 | |
parent | 0f2525a12ea0ed03d3d33a48b112d3d80a622b75 (diff) | |
parent | cfd2969cd9fa18e6148e97c1066341b1c1add6cf (diff) |
Merge branch 'dev/db' into dev/db-groupby
-rw-r--r-- | models.py | 27 | ||||
-rw-r--r-- | views.py | 19 |
2 files changed, 15 insertions, 31 deletions
@@ -1,18 +1,11 @@ -from django.db import connection, models +from django.db import models from django.db.models import Q -from django.db.models.expressions import RawSQL -from django.db.models.functions import Extract, ExtractYear from django.utils.text import slugify import hashlib import os -@models.DateTimeField.register_lookup -class ExtractWeek(Extract): - lookup_name = "week" - - class Timetable(models.Model): name = models.CharField(max_length=64, unique=True, verbose_name="nom") url = models.URLField(max_length=255, unique=True, verbose_name="URL") @@ -51,6 +44,9 @@ class Group(models.Model): slug = models.SlugField(max_length=64, unique=True, default="") + def corresponds_to(self, timetable_id, mention, subgroup, td, tp): + return self.timetable.id == timetable_id and self.mention == mention and self.subgroup == subgroup and (self.td == td or self.td is None or td is None) and (self.tp == tp or self.tp is None or tp is None) + def __str__(self): return self.name @@ -112,23 +108,12 @@ class Room(models.Model): class CourseManager(models.Manager): - def __get_weeks(self, qs): - extractYear = ExtractYear("begin") - - if connection.vendor == "postgresql": - return qs.annotate(week=ExtractWeek("begin"), year=extractYear) - else: - return qs.annotate(week=RawSQL("""cast(strftime("%%W", "begin") as integer)""", []), year=extractYear) - 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_weeks(self, date): + return self.get_queryset().raw("select edt_course.id, cast(strftime('%%W', begin) as integer) as week, cast(strftime('%%Y', begin) as integer) as year, edt_group.name, edt_group.timetable_id, edt_group.mention, edt_group.subgroup, edt_group.td, edt_group.tp, edt_group.slug, edt_group.id as group_id from edt_course inner join edt_course_groups on (edt_course.id = edt_course_groups.course_id) inner join edt_group on (edt_course_groups.group_id = edt_group.id) where begin >= %s group by group_id, week, year order by group_id, year, week", [date]) - def get_courses_for_group_and_weeks(self, group, **filters): - qs = self.get_courses_for_group(group, **filters) - return self.__get_weeks(qs) class Course(models.Model): objects = CourseManager() @@ -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(start) 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.timetable_id, group_week.mention, group_week.subgroup, group_week.td, group_week.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}) |