diff options
-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] |