diff options
author | Alban Gruin | 2017-02-27 09:52:08 +0100 |
---|---|---|
committer | Alban Gruin | 2017-02-27 09:52:08 +0100 |
commit | 60092220ca3c47a20f50b677a2bb2173ae3f89a3 (patch) | |
tree | 6d7e8dca6e5e028e598b03e2d98a30f2338a2c9c | |
parent | 0649ee9c528d7a3957fb65e195a4a56a702e8d48 (diff) |
Parsage du nom des groupes basé sur une regex pour éviter les problèmes liés à l'ancienne méthode
-rw-r--r-- | models.py | 22 | ||||
-rw-r--r-- | utils.py | 15 |
2 files changed, 20 insertions, 17 deletions
@@ -20,6 +20,8 @@ from django.db.models.expressions import RawSQL from django.db.models.functions import Extract, ExtractYear from django.utils.text import slugify +from .utils import parse_group + import hashlib import os @@ -81,23 +83,9 @@ class Group(models.Model): return self.name def save(self, *args, **kwargs): - try: - parts = self.celcat_name.split("-")[-2:] - group = parts[1].strip()[2:] - - self.mention = parts[0].strip() - self.subgroup = group[0] - - if len(group) > 1: - self.td = int(group[1]) - if len(group) > 2: - self.tp = int(group[2]) - except: - print("Malformed name: {0}. Ignoring".format(self.celcat_name)) - finally: - if self.name == "": - self.name = self.celcat_name - + 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() @@ -17,6 +17,7 @@ from django.utils import timezone import datetime +import re def get_current_week(): return timezone.now().isocalendar()[:2] @@ -44,3 +45,17 @@ def group_courses(courses): grouped_courses[-1].append(course) return grouped_courses + +def parse_group(name): + group_regex = re.compile("^(.+?)\s*\-\s*(((CM)(\w))|((TD)(\w)(\d))|((TP)(\w)(\d)(\d)))$") + search = group_regex.search(name) + if search is None: + return None, None, None, None + + parts = search.groups(0) + if parts[3] == "CM": + return parts[0], parts[4], None, None + elif parts[6] == "TD": + return parts[0], parts[7], parts[8], None + elif parts[10] == "TP": + return parts[0], parts[11], parts[12], parts[13] |