aboutsummaryrefslogtreecommitdiff
path: root/models.py
diff options
context:
space:
mode:
authorAlban Gruin2017-09-04 22:13:31 +0200
committerAlban Gruin2017-09-04 22:13:31 +0200
commitab9df7a174e2debbf9c644c8ec3e2127eeeaa3cf (patch)
treebf9510823644aa1c5dba0ab39277633c0f18ad49 /models.py
parentce0ed9347adde9ca80c11efe79766c966d5749ba (diff)
parent4b3fc9e4c41e2247bf0fa0fe2629b2b57fc174b0 (diff)
Merge branch 'stable/0.y.z' into prod/pa1ch/0.y.zv0.8.0-pa1chprod/pa1ch/0.8.z
Diffstat (limited to 'models.py')
-rw-r--r--models.py33
1 files changed, 16 insertions, 17 deletions
diff --git a/models.py b/models.py
index 478b48f..21b08f5 100644
--- a/models.py
+++ b/models.py
@@ -16,8 +16,7 @@
from django.db import connection, models
from django.db.models import Count, Manager, Q
-from django.db.models.expressions import RawSQL
-from django.db.models.functions import Extract, ExtractYear
+from django.db.models.functions import ExtractWeek, ExtractYear
from django.utils.text import slugify
from .utils import parse_group
@@ -26,10 +25,6 @@ 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")
@@ -60,20 +55,28 @@ class LastUpdate(models.Model):
verbose_name_plural = "dernières mises à jour"
+class GroupManager(Manager):
+ def get_relevant_groups(self):
+ return self.get_queryset().annotate(children_count=Count("children")).filter(children_count=0)
+
+
class Group(models.Model):
+ objects = GroupManager()
+
name = models.CharField(max_length=255, verbose_name="nom")
celcat_name = models.CharField(max_length=255, verbose_name="nom dans Celcat")
timetable = models.ForeignKey(Timetable, on_delete=models.CASCADE, verbose_name="emploi du temps")
mention = models.CharField(max_length=32)
- subgroup = models.CharField(max_length=1, verbose_name="sous-groupe")
+ subgroup = models.CharField(max_length=1, verbose_name="sous-groupe", null=True)
td = models.IntegerField(verbose_name="groupe de TD", null=True)
tp = models.IntegerField(verbose_name="groupe de TP", null=True)
+ parent = models.ForeignKey("self", verbose_name="groupe parent", null=True, default=None, related_name="children")
slug = models.SlugField(max_length=64, default="")
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)
+ return self.timetable.id == timetable_id and self.mention.startswith(mention) and (self.subgroup == subgroup or self.subgroup is None) 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)
@property
def group_info(self):
@@ -83,11 +86,12 @@ class Group(models.Model):
return self.name
def save(self, *args, **kwargs):
- self.mention, self.subgroup, self.td, self.tp = parse_group(self.celcat_name)
if self.name == "":
self.name = self.celcat_name
self.slug = slugify(self.name)
- super(Group, self).save()
+
+ self.mention, self.subgroup, self.td, self.tp = parse_group(self.name)
+ super(Group, self).save()
class Meta:
@@ -133,15 +137,10 @@ 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), groups__mention=group.mention, groups__subgroup=group.subgroup, timetable=group.timetable, **filters).order_by("begin")
+ 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_weeks(self, **criteria):
- 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)""", []))
+ return self.get_queryset().filter(**criteria).order_by("groups__name", "year", "week").annotate(_=Count(("groups", "year", "week", "begin")), year=ExtractYear("begin"), week=ExtractWeek("begin"))
class Course(models.Model):