From af0fe7024ce13cdb30170f00df8a1883eef23bf8 Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Fri, 8 Sep 2017 12:58:04 +0200 Subject: Changement des index du modèle Timetable, meilleure utilisation des requêtes --- models.py | 15 ++++++++------- views.py | 10 +++------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/models.py b/models.py index 3e8eae3..109b327 100644 --- a/models.py +++ b/models.py @@ -39,15 +39,16 @@ class Year(models.Model): class Timetable(models.Model): year = models.ForeignKey(Year, verbose_name="année") - name = models.CharField(max_length=64, unique=True, verbose_name="nom") - url = models.URLField(max_length=255, unique=True, verbose_name="URL") - slug = models.SlugField(max_length=64, unique=True, default="") + name = models.CharField(max_length=64, verbose_name="nom") + url = models.URLField(max_length=255, verbose_name="URL") + slug = models.SlugField(max_length=64, default="") def __str__(self): return self.year.name + " " + self.name class Meta: + unique_together = (("year", "name"), ("year", "slug"),) verbose_name = "emploi du temps" verbose_name_plural = "emplois du temps" @@ -71,8 +72,8 @@ class LastUpdate(models.Model): class GroupManager(Manager): - def get_relevant_groups(self, *args, **filters): - return self.get_queryset().filter(*args, **filters).annotate(children_count=Count("children")).filter(children_count=0) + def get_relevant_groups(self, *args, **criteria): + return self.get_queryset().filter(*args, **criteria).annotate(children_count=Count("children")).filter(children_count=0) class Group(models.Model): @@ -151,8 +152,8 @@ class Room(models.Model): 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), Q(groups__subgroup__isnull=True) | Q(groups__subgroup=group.subgroup), groups__mention=group.mention, timetable=group.timetable, **filters).order_by("begin") + def get_courses_for_group(self, group, **criteria): + 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), Q(groups__subgroup__isnull=True) | Q(groups__subgroup=group.subgroup), groups__mention=group.mention, timetable=group.timetable, **criteria).order_by("begin") def get_weeks(self, **criteria): return self.get_queryset().filter(**criteria).order_by("groups__name", "year", "week").annotate(_=Count(("groups", "year", "week", "begin")), year=ExtractYear("begin"), week=ExtractWeek("begin")) diff --git a/views.py b/views.py index ffed2cc..2101a21 100644 --- a/views.py +++ b/views.py @@ -33,8 +33,7 @@ def mention_list(request, year_slug): return render(request, "mention_list.html", {"year": year, "timetables": timetables}) def group_list(request, year_slug, timetable_slug): - year_obj = get_object_or_404(Year, slug=year_slug) - timetable = get_object_or_404(Timetable, year=year_obj, slug=timetable_slug) + timetable = get_object_or_404(Timetable, year__slug=year_slug, slug=timetable_slug) groups = Group.objects.get_relevant_groups(timetable=timetable).order_by("name") year, week = get_current_week() @@ -62,8 +61,7 @@ def timetable(request, year_slug, timetable_slug, group_slug, year=None, week=No start, end = get_week(int(year), int(week)) - get_object_or_404(Year, slug=year_slug) - timetable = get_object_or_404(Timetable, slug=timetable_slug) + timetable = get_object_or_404(Timetable, year__slug=year_slug, slug=timetable_slug) group = get_object_or_404(Group, slug=group_slug, timetable=timetable) last_update = get_object_or_404(LastUpdate, timetable=timetable, week=week, year=year) courses = Course.objects.get_courses_for_group(group, begin__gte=start, begin__lt=end) @@ -73,9 +71,7 @@ def timetable(request, year_slug, timetable_slug, group_slug, year=None, week=No return render(request, "timetable.html", {"group": group, "courses": grouped_courses, "last_update": last_update.date, "year": year, "week": int(week)}) def subscribe(request, year_slug, timetable_slug, group_slug, year, week): - year_obj = get_object_or_404(Year, slug=year_slug) - timetable = get_object_or_404(Timetable, year=year_obj, slug=timetable_slug) - group = get_object_or_404(Group, slug=group_slug, timetable=timetable) + group = get_object_or_404(Group, slug=group_slug, timetable__slug=timetable_slug, timetable__year__slug=year_slug) if request.method == "POST": form = SubscribeForm(request.POST) -- cgit v1.2.1