aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlban Gruin2017-02-11 13:58:41 +0100
committerAlban Gruin2017-02-11 13:58:41 +0100
commitbb5c77bf35b13f5c11bc4cdbc0325983e349b984 (patch)
tree888b5189c582371417ddcba02cbf5e883fa0e36e
parent64f7dedf227a88bac522cb34c6a99982b3d65321 (diff)
Squashed commit of the following:
commit ff25e2dc285c423f4c40a83deb89a56ee62b15a9 Author: Alban Gruin <alban dot gruin at gmail dot com> Date: Sat Feb 11 13:47:38 2017 +0100 Nettoyage du code de CourseManager commit 26a132d5d1fdb7fc9da827bbc9d5e409834fb954 Author: Alban Gruin <alban dot gruin at gmail dot com> Date: Sat Feb 11 13:45:14 2017 +0100 Suppression du GroupedManager et de toutes ses sous-classes
-rw-r--r--db.py80
-rw-r--r--models.py29
2 files changed, 13 insertions, 96 deletions
diff --git a/db.py b/db.py
deleted file mode 100644
index 94df376..0000000
--- a/db.py
+++ /dev/null
@@ -1,80 +0,0 @@
-# Copyright (C) 2017 Alban Gruin
-#
-# celcatsanitizer is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# celcatsanitizer is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License along
-# with celcatsanitizer; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-
-from django.db import connections
-from django.db.models import Manager
-from django.db.models.functions import Extract
-from django.db.models.sql.compiler import SQLCompiler
-from django.db.models.sql.query import Query
-from django.db.models.sql.where import WhereNode
-from django.db.models.query import QuerySet
-
-class ExtractWeek(Extract):
- lookup_name = "week"
-
-
-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 __init__(self, model, where=WhereNode):
- 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)
-
- def clear_grouping(self):
- 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):
- def __init__(self, model=None, query=None, using=None, hints=None):
- super(GroupedQuerySet, self).__init__(model, query, using, hints)
- self.query = query or GroupedQuery(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
diff --git a/models.py b/models.py
index 1723536..1e96a88 100644
--- a/models.py
+++ b/models.py
@@ -15,17 +15,19 @@
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from django.db import connection, models
-from django.db.models import Q
+from django.db.models import Count, Manager, Q
from django.db.models.expressions import RawSQL
-from django.db.models.functions import ExtractYear
+from django.db.models.functions import Extract, ExtractYear
from django.utils.text import slugify
-from .db import ExtractWeek, GroupedManager
-
import hashlib
import os
+class ExtractWeek(Extract):
+ lookup_name = "week"
+
+
class Timetable(models.Model):
name = models.CharField(max_length=64, unique=True, verbose_name="nom")
url = models.URLField(max_length=255, unique=True, verbose_name="URL")
@@ -141,22 +143,17 @@ class Room(models.Model):
verbose_name_plural = "salles"
-class CourseManager(GroupedManager):
- def __get_weeks(self, qs):
- extractYear = ExtractYear("begin")
- qs = qs.group_by("groups", "year", "week", "groups__timetable", "groups__mention", "groups__subgroup", "groups__td", "groups__tp", "begin", "groups__name").order_by("groups__name", "year", "week")
-
- if connection.vendor == "postgresql":
- return qs.annotate(week=ExtractWeek("begin"), year=extractYear)
- else:
- return qs.annotate(week=RawSQL("""cast(strftime("%%W", "begin") as integer)""", []), year=extractYear)
-
+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), groups__mention=group.mention, groups__subgroup=group.subgroup, timetable=group.timetable, **filters).order_by("begin")
def get_weeks(self, **criteria):
- qs = self.get_queryset().filter(**criteria)
- return self.__get_weeks(qs)
+ qs = self.get_queryset().filter(**criteria).order_by("groups__name", "year", "week").annotate(_=Count(("groups", "year", "week", "begin")), year=ExtractYear("begin"))
+
+ if connection.vendor == "postgresql":
+ return qs.annotate(week=ExtractWeek("begin"))
+ else:
+ return qs.annotate(week=RawSQL("""cast(strftime("%%W", "begin") as integer)""", []))
class Course(models.Model):