aboutsummaryrefslogtreecommitdiff
path: root/models.py
diff options
context:
space:
mode:
authorAlban Gruin2017-02-12 13:44:05 +0100
committerAlban Gruin2017-02-12 13:44:05 +0100
commita543aaa14957f390d2b640a264113b639c7d3194 (patch)
tree62bf0fdbd83717f3020dad2313dd1f4f4a9dcdce /models.py
parent3d5d17475909a6b351fadf8bbdb0bf8dc8d8e928 (diff)
parent908ed3b4d0bc57131f6dafc663c6f7e098f0b7e1 (diff)
Merge branch 'stable/0.y.z' into prod/pa1ch/0.y.zv0.6.0-pa1ch
Diffstat (limited to 'models.py')
-rw-r--r--models.py27
1 files changed, 12 insertions, 15 deletions
diff --git a/models.py b/models.py
index 2100d35..1e96a88 100644
--- a/models.py
+++ b/models.py
@@ -15,13 +15,11 @@
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from django.db import connection, models
-from django.db.models import Q
+from django.db.models import Count, Manager, 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
@@ -75,6 +73,10 @@ class Group(models.Model):
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)
+ @property
+ def group_info(self):
+ return self.timetable.id, self.mention, self.subgroup, self.td, self.tp
+
def __str__(self):
return self.name
@@ -141,22 +143,17 @@ class Room(models.Model):
verbose_name_plural = "salles"
-class CourseManager(GroupedManager):
- def __get_weeks(self, qs):
- extractYear = ExtractYear("begin")
- qs = qs.group_by("groups", "year", "week", "groups__timetable", "groups__mention", "groups__subgroup", "groups__td", "groups__tp", "begin", "groups__name").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)
-
+class CourseManager(Manager):
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, timetable=group.timetable, **filters).order_by("begin")
def get_weeks(self, **criteria):
- qs = self.get_queryset().filter(**criteria)
- return self.__get_weeks(qs)
+ qs = self.get_queryset().filter(**criteria).order_by("groups__name", "year", "week").annotate(_=Count(("groups", "year", "week", "begin")), year=ExtractYear("begin"))
+
+ if connection.vendor == "postgresql":
+ return qs.annotate(week=ExtractWeek("begin"))
+ else:
+ return qs.annotate(week=RawSQL("""cast(strftime("%%W", "begin") as integer)""", []))
class Course(models.Model):