From 0f2525a12ea0ed03d3d33a48b112d3d80a622b75 Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Fri, 27 Jan 2017 21:05:33 +0100 Subject: Travail initial sur l'implémentation d'un 'group by' un minimum sain pour l'orm de django --- db.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 db.py diff --git a/db.py b/db.py new file mode 100644 index 0000000..d8f92ef --- /dev/null +++ b/db.py @@ -0,0 +1,33 @@ +from django.db.models import Manager +from django.db.models.query import QuerySet +from django.db.models.sql.query import Query + + +class GroupedQuery(Query): + def add_grouping(self, *grouping): + if self.group_by is None: + self.group_by = [] + + if isinstance(self.group_by, list): + self.group_by.extend(grouping) + + def clear_grouping(self): + self.group_by = None + + +class GroupedQuerySet(QuerySet): + def __init__(self, model=None, query=None, using=None, hints=None): + super(BetterQuerySet, self).__init__(model, query, using, hints) + self.query = query or BetterQuery(self.model) + + def group_by(self, *field_names): + obj = self._clone() + obj.query.clear_grouping() + obj.query.add_grouping(*field_names) + return obj + + +class GroupedManager(Manager): + def __init__(self): + super(GroupedManager, self).__init__() + self._queryset_class = GroupedQuerySet -- cgit v1.2.1 From e6c95f5e792ed1400965eae90f2770f385872469 Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Fri, 27 Jan 2017 22:12:38 +0100 Subject: Problème de nom --- db.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/db.py b/db.py index d8f92ef..df9ceda 100644 --- a/db.py +++ b/db.py @@ -17,8 +17,8 @@ class GroupedQuery(Query): class GroupedQuerySet(QuerySet): def __init__(self, model=None, query=None, using=None, hints=None): - super(BetterQuerySet, self).__init__(model, query, using, hints) - self.query = query or BetterQuery(self.model) + super(GroupedQuerySet, self).__init__(model, query, using, hints) + self.query = query or GroupedQuery(self.model) def group_by(self, *field_names): obj = self._clone() -- cgit v1.2.1 From ad5bbfe24c8d00d3bc4b2ae96cb583c141ae27dd Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Fri, 27 Jan 2017 22:48:47 +0100 Subject: Création de différentes classes permettant d'effectuer un group by simple * GroupedCompiler rajoute une étape à get_group_by() pour insérer de manière simple les champs à grouper * GroupedQuery rajoute une liste stockant les champs à grouper * GroupedQuerySet implémente la fonction group_by() * GroupedManager change le type de QuerySet par défaut pour GroupedQuerySet Le manager des cours se base sur GroupedManager --- db.py | 34 ++++++++++++++++++++++++++++------ models.py | 5 ++++- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/db.py b/db.py index df9ceda..e75adca 100644 --- a/db.py +++ b/db.py @@ -1,18 +1,40 @@ +from django.db import connections from django.db.models import Manager from django.db.models.query import QuerySet +from django.db.models.sql.compiler import SQLCompiler from django.db.models.sql.query import Query +from django.db.models.sql.where import WhereNode + + +class GroupedCompiler(SQLCompiler): + def get_group_by(self, select, order_by): + result = super(GroupedCompiler, self).get_group_by(select, order_by) + expressions = [] + for expr in self.query.real_group_by: + ref = expr if hasattr(expr, "as_sql") else self.query.resolve_ref(expr) + sql, params = self.compile(ref) + result.append((sql, params)) + + return result class GroupedQuery(Query): - def add_grouping(self, *grouping): - if self.group_by is None: - self.group_by = [] + def __init__(self, model, where=WhereNode): + super(GroupedQuery, self).__init__(model, where) + self.real_group_by = [] - if isinstance(self.group_by, list): - self.group_by.extend(grouping) + def add_grouping(self, *grouping): + self.real_group_by.extend(grouping) def clear_grouping(self): - self.group_by = None + self.real_group_by = [] + + def get_compiler(self, using=None, connection=None): + if using is None and connection is None: + raise ValueError("Need either using or connection") + if using: + connection = connections[using] + return GroupedCompiler(self, connection, using) class GroupedQuerySet(QuerySet): diff --git a/models.py b/models.py index 64fb675..26bb63d 100644 --- a/models.py +++ b/models.py @@ -4,6 +4,8 @@ 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 @@ -111,7 +113,7 @@ class Room(models.Model): verbose_name_plural = "salles" -class CourseManager(models.Manager): +class CourseManager(GroupedManager): def __get_weeks(self, qs): extractYear = ExtractYear("begin") @@ -130,6 +132,7 @@ class CourseManager(models.Manager): qs = self.get_courses_for_group(group, **filters) return self.__get_weeks(qs) + class Course(models.Model): objects = CourseManager() -- cgit v1.2.1 From c4049ef83e7e77bc7d4a9ab5795ce746eb8e8459 Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Sat, 28 Jan 2017 12:38:46 +0100 Subject: Réimplémentation du clonage de GroupedQuery --- db.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/db.py b/db.py index e75adca..2d4d320 100644 --- a/db.py +++ b/db.py @@ -23,6 +23,11 @@ class GroupedQuery(Query): super(GroupedQuery, self).__init__(model, where) self.real_group_by = [] + def clone(self, klass=None, memo=None, **kwargs): + obj = super(GroupedQuery, self).clone(klass, memo, **kwargs) + obj.real_group_by = self.real_group_by[:] + return obj + def add_grouping(self, *grouping): self.real_group_by.extend(grouping) -- cgit v1.2.1 From 6a7f243eabdc1007e8effcc6d8efa6a990fe40c1 Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Fri, 27 Jan 2017 21:40:24 +0100 Subject: Surcharge de l'opérateur in du modèle des groupes --- models.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/models.py b/models.py index 26bb63d..f1ff118 100644 --- a/models.py +++ b/models.py @@ -53,6 +53,9 @@ class Group(models.Model): slug = models.SlugField(max_length=64, unique=True, default="") + def __contains__(self, group): + return self.timetable == group.timetable and self.mention == group.mention and self.subgroup == group.subgroup and (self.td == group.td or self.td is None or group.td is None) and (self.tp == group.tp or self.tp is None or group.tp is None) + def __str__(self): return self.name -- cgit v1.2.1 From dfb352540ce5a66ef475f0c6d855fc2f322d553a Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Fri, 27 Jan 2017 21:48:53 +0100 Subject: Bon en fait pas de surcharge --- models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models.py b/models.py index f1ff118..bcd9215 100644 --- a/models.py +++ b/models.py @@ -53,8 +53,8 @@ class Group(models.Model): slug = models.SlugField(max_length=64, unique=True, default="") - def __contains__(self, group): - return self.timetable == group.timetable and self.mention == group.mention and self.subgroup == group.subgroup and (self.td == group.td or self.td is None or group.td is None) and (self.tp == group.tp or self.tp is None or group.tp is None) + 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) def __str__(self): return self.name -- cgit v1.2.1 From ebb7c3bf0dc3eef2efa3f4add60ccfc7dc063248 Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Sat, 28 Jan 2017 13:20:23 +0100 Subject: Traitement des semaines avec un group by, semblant diviser par deux le temps de traitement --- models.py | 8 +++----- views.py | 19 +++++++++---------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/models.py b/models.py index bcd9215..aa7be24 100644 --- a/models.py +++ b/models.py @@ -119,6 +119,7 @@ class Room(models.Model): class CourseManager(GroupedManager): def __get_weeks(self, qs): extractYear = ExtractYear("begin") + qs = qs.group_by("groups", "year", "week").order_by("groups__name", "year", "week") if connection.vendor == "postgresql": return qs.annotate(week=ExtractWeek("begin"), year=extractYear) @@ -128,11 +129,8 @@ class CourseManager(GroupedManager): 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, **filters).order_by("begin") - def get_weeks(self): - return self.__get_weeks(self.get_queryset()) - - def get_courses_for_group_and_weeks(self, group, **filters): - qs = self.get_courses_for_group(group, **filters) + def get_weeks(self, **criteria): + qs = self.get_queryset().filter(**criteria) return self.__get_weeks(qs) diff --git a/views.py b/views.py index 24473d1..e883e9d 100644 --- a/views.py +++ b/views.py @@ -18,18 +18,17 @@ def index(request): current_year, current_week, _ = timezone.now().isocalendar() start, _ = get_week(current_year, current_week) + groups_weeks = Course.objects.get_weeks(begin__gte=start).values("groups__timetable", "groups__mention", "groups__subgroup", "groups__td", "groups__tp", "year", "week") for group in groups: - courses = Course.objects.get_courses_for_group(group, begin__gte=start).values("begin").annotate(count=Count("begin")) - weeks = [] - for course in courses: - year, week, _ = course["begin"].isocalendar() - date, _ = get_week(year, week) - - if date not in weeks: - weeks.append(date) - - group.weeks = weeks + for group_week in groups_weeks: + if group.corresponds_to(group_week["groups__timetable"], group_week["groups__mention"], group_week["groups__subgroup"], group_week["groups__td"], group_week["groups__tp"]): + if not hasattr(group, "weeks"): + group.weeks = [] + + date, _ = get_week(group_week["year"], group_week["week"]) + if date not in group.weeks: + group.weeks.append(date) return render(request, "index.html", {"timetables": timetables, "groups": groups}) -- cgit v1.2.1