aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlban Gruin2017-10-20 13:29:07 +0200
committerAlban Gruin2017-10-20 13:29:07 +0200
commit07e9a1d1e6c77d64e5c3daf0324ae751117f168e (patch)
treed5691d98844954b1a1490cf09e1a140322cd9533
parent276dc9017b414404539b20f32bcc65959fd32adb (diff)
Modification de la requĂȘte qui sĂ©lectionne les groupes pertinents.
-rw-r--r--models.py21
-rw-r--r--views.py9
2 files changed, 15 insertions, 15 deletions
diff --git a/models.py b/models.py
index db9e262..74fab9e 100644
--- a/models.py
+++ b/models.py
@@ -16,7 +16,8 @@
from functools import reduce
from django.db import models
-from django.db.models import Count, Manager, Q
+from django.db.models import Count, Manager, Q, Subquery
+from django.db.models.expressions import OuterRef
from django.db.models.functions import ExtractWeek, ExtractYear
from django.utils import timezone
from django.utils.text import slugify
@@ -69,17 +70,13 @@ class Timetable(SlugModel):
class GroupManager(Manager):
- def get_relevant_children(self, group):
- parent_in = self.get_queryset().filter(parent=group)
- return self.get_queryset().filter(Q(parent=group) | Q(parent__in=parent_in)) \
- .annotate(children_count=Count("children")) \
- .filter(children_count=0, hidden=False) \
- .order_by("name")
-
- def get_relevant_groups(self, *args, **criteria):
- return self.get_queryset().filter(*args, **criteria) \
- .annotate(children_count=Count("children")) \
- .filter(children_count=0, hidden=False)
+ def get_relevant_groups(self, timetable, *args, **criteria):
+ sub = Group.objects.filter(timetable=timetable,mention=OuterRef("mention"),
+ subgroup__startswith=OuterRef("subgroup")) \
+ .order_by().values("mention").annotate(c=Count("*")).values("c")
+ return Group.objects.filter(*args, timetable=timetable, hidden=False, **criteria) \
+ .annotate(nbsub=Subquery(sub, output_field=models.IntegerField())) \
+ .filter(nbsub=1).order_by("name")
class Group(models.Model):
diff --git a/views.py b/views.py
index e5c89a2..9c38524 100644
--- a/views.py
+++ b/views.py
@@ -59,7 +59,7 @@ def group_list_common(request, timetable, groups):
def group_list(request, year_slug, timetable_slug):
timetable = get_object_or_404(Timetable, year__slug=year_slug, slug=timetable_slug)
- groups = Group.objects.filter(timetable=timetable).order_by("name")
+ groups = Group.objects.get_relevant_groups(timetable)
return group_list_common(request, timetable, groups)
def timetable(request, year_slug, timetable_slug, group_slug, year=None, week=None):
@@ -77,8 +77,11 @@ def timetable(request, year_slug, timetable_slug, group_slug, year=None, week=No
timetable = get_object_or_404(Timetable, year__slug=year_slug, slug=timetable_slug)
group = get_object_or_404(Group, slug=group_slug, timetable=timetable)
- # if group.children.count():
- # return group_list_common(request, timetable, Group.objects.get_relevant_children(group))
+ if Group.objects.filter(timetable=timetable, mention=group.mention,
+ subgroup__startswith=group.subgroup).count() > 1:
+ subgroups = Group.objects.get_relevant_groups(timetable, mention=group.mention,
+ subgroup__startswith=group.subgroup)
+ return group_list_common(request, timetable, subgroups)
courses = Course.objects.get_courses_for_group(group, begin__gte=start, begin__lt=end)
if courses.count() == 0 and provided_week: