From 827641acd0b2c64feb704bae989b139458a3d0ca Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Fri, 27 Oct 2017 09:37:25 +0200 Subject: Fonction pour récupérer les parents d’un groupe --- models.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/models.py b/models.py index 74fab9e..c546737 100644 --- a/models.py +++ b/models.py @@ -70,11 +70,21 @@ class Timetable(SlugModel): class GroupManager(Manager): + def get_parents(self, group): + groups_criteria = Q(subgroup__isnull=True) | \ + reduce(lambda x, y: x | y, + [Q(subgroup=group.subgroup[:i]) + for i in range(1, len(group.subgroup) + 1)]) + + return self.get_queryset().filter(groups_criteria, mention=group.mention, + timetable=group.timetable) + 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) \ + + return self.get_queryset().filter(*args, timetable=timetable, hidden=False, **criteria) \ .annotate(nbsub=Subquery(sub, output_field=models.IntegerField())) \ .filter(nbsub=1).order_by("name") @@ -146,15 +156,8 @@ class Room(models.Model): class CourseManager(Manager): def get_courses_for_group(self, group, **criteria): - groups_criteria = reduce(lambda x, y: x | y, - [Q(groups__subgroup=group.subgroup[:i]) - for i in range(1, len(group.subgroup) + 1)]) | \ - Q(groups__subgroup__isnull=True) - return self.get_queryset() \ - .filter(groups_criteria, - groups__mention=group.mention, - timetable=group.timetable, **criteria) \ + .filter(groups__in=Group.objects.get_parents(group), **criteria) \ .order_by("begin") def get_weeks(self, **criteria): -- cgit v1.2.1