aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--db.py60
-rw-r--r--models.py22
-rw-r--r--views.py6
3 files changed, 81 insertions, 7 deletions
diff --git a/db.py b/db.py
new file mode 100644
index 0000000..2d4d320
--- /dev/null
+++ b/db.py
@@ -0,0 +1,60 @@
+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 __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 9cb3bca..a2d4281 100644
--- a/models.py
+++ b/models.py
@@ -1,7 +1,11 @@
-from django.db import models
+from django.db import connection, models
from django.db.models import Q
+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
@@ -107,12 +111,22 @@ class Room(models.Model):
verbose_name_plural = "salles"
-class CourseManager(models.Manager):
+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)
+ else:
+ return qs.annotate(week=RawSQL("""cast(strftime("%%W", "begin") as integer)""", []), year=extractYear)
+
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, date):
- return self.get_queryset().raw("select edt_course.id, cast(strftime('%%W', begin) as integer) as week, cast(strftime('%%Y', begin) as integer) as year, edt_group.name, edt_group.timetable_id, edt_group.mention, edt_group.subgroup, edt_group.td, edt_group.tp, edt_group.slug, edt_group.id as group_id from edt_course inner join edt_course_groups on (edt_course.id = edt_course_groups.course_id) inner join edt_group on (edt_course_groups.group_id = edt_group.id) where begin >= %s group by group_id, week, year order by group_id, year, week", [date])
+ def get_weeks(self, **criteria):
+ qs = self.get_queryset().filter(**criteria)
+ return self.__get_weeks(qs)
class Course(models.Model):
diff --git a/views.py b/views.py
index c549cfb..e883e9d 100644
--- a/views.py
+++ b/views.py
@@ -18,15 +18,15 @@ def index(request):
current_year, current_week, _ = timezone.now().isocalendar()
start, _ = get_week(current_year, current_week)
- groups_weeks = Course.objects.get_weeks(start)
+ 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:
for group_week in groups_weeks:
- if group.corresponds_to(group_week.timetable_id, group_week.mention, group_week.subgroup, group_week.td, group_week.tp):
+ 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)
+ date, _ = get_week(group_week["year"], group_week["week"])
if date not in group.weeks:
group.weeks.append(date)