aboutsummaryrefslogtreecommitdiff
path: root/models.py
diff options
context:
space:
mode:
authorAlban Gruin2018-04-15 12:04:20 +0200
committerAlban Gruin2018-04-15 12:04:20 +0200
commit324e391633b30bef5b781a733c56d26d29b45246 (patch)
tree86387cbb4c19aa95f5c433ec7e5b130cb29d600f /models.py
parent1abd9faddd2f07341c6ff70dfa88d7c83eac40c3 (diff)
parenteb32f6011eafd77db2455ce3183c527c2e86deeb (diff)
Merge branch 'fusion/alban' into qsjps
Diffstat (limited to 'models.py')
-rw-r--r--models.py13
1 files changed, 11 insertions, 2 deletions
diff --git a/models.py b/models.py
index 538d64c..f7450dc 100644
--- a/models.py
+++ b/models.py
@@ -16,7 +16,7 @@
from functools import reduce
from django.db import models
-from django.db.models import Manager, Q
+from django.db.models import Count, Manager, OuterRef, Q, Subquery
from django.db.models.functions import ExtractWeek, ExtractYear
from django.utils import timezone
from django.utils.text import slugify
@@ -31,6 +31,7 @@ class SlugModel(models.Model):
super(SlugModel, self).save(*args, **kwargs)
+
class Meta:
abstract = True
@@ -104,6 +105,13 @@ class GroupManager(Manager):
mention=group.mention,
source=group.source)
+ def get_relevant_groups(self, start, **criteria):
+ courses = Course.objects.filter(groups=OuterRef("pk"), begin__gte=start) \
+ .only("pk")[:1]
+ return self.get_queryset().annotate(c=Subquery(courses,
+ output_field=models.IntegerField())) \
+ .filter(c__isnull=False, **criteria).order_by("name")
+
class Group(SlugModel):
objects = GroupManager()
@@ -213,7 +221,8 @@ class CourseManager(Manager):
.annotate(year=ExtractYear("begin"),
week=ExtractWeek("begin")) \
.values("groups__mention", "groups__semester",
- "groups__subgroup", "year", "week")
+ "groups__subgroup", "year", "week") \
+ .annotate(c=Count("*"))
class Course(models.Model):