diff options
author | Alban Gruin | 2017-01-28 13:27:32 +0100 |
---|---|---|
committer | Alban Gruin | 2017-01-28 13:27:32 +0100 |
commit | d5c846ac3214efcbd2b3cb8d562b58006c3a5eb5 (patch) | |
tree | 4e4721c0cdec315471379692f2f540b7a6c24851 /models.py | |
parent | cfd2969cd9fa18e6148e97c1066341b1c1add6cf (diff) | |
parent | ebb7c3bf0dc3eef2efa3f4add60ccfc7dc063248 (diff) |
Merge branch 'dev/db-groupby' into dev/dbv0.2.0
Diffstat (limited to 'models.py')
-rw-r--r-- | models.py | 22 |
1 files changed, 18 insertions, 4 deletions
@@ -1,7 +1,11 @@ -from django.db import models +from django.db import connection, 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 +from .db import GroupedManager + import hashlib import os @@ -107,12 +111,22 @@ class Room(models.Model): verbose_name_plural = "salles" -class CourseManager(models.Manager): +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) + 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, 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_weeks(self, **criteria): + qs = self.get_queryset().filter(**criteria) + return self.__get_weeks(qs) class Course(models.Model): |