# Copyright (C) 2017 Alban Gruin # # celcatsanitizer is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published # by the Free Software Foundation, either version 3 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 Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with celcatsanitizer. If not, see . import datetime import re from django.utils import timezone def get_current_week(): return timezone.now().isocalendar()[:2] def get_current_or_next_week(): year, week, day = timezone.now().isocalendar() if day >= 6: year, week, _ = (timezone.now() + datetime.timedelta(weeks=1)).isocalendar() return year, week def get_week(year, week): start = timezone.make_aware(datetime.datetime.strptime( "{0}-W{1:02d}-1".format(year, week), "%Y-W%W-%w")) end = start + datetime.timedelta(weeks=1) return start, end def group_courses(courses): grouped_courses = [] for i, course in enumerate(courses): if i == 0 or courses[i - 1].begin.day != course.begin.day: grouped_courses.append([course]) else: grouped_courses[-1].append(course) return grouped_courses def parse_group(name): # Explication de la regex # # ^(.+?)\s+((CM(\w))|(TD(\w)(\d))|(TP(\w)(\d)(\d)))?(\s+\(.+\))?$ # ^ début de la ligne # (.+?) correspond à au moins un caractère # \s+ un ou plusieurs espaces # ((CM(\w))| correspond à CM suivi d'une lettre ou… # (TD(\w)(\d))| … à TD suivi d’une lettre et d'un chiffre ou… # (TP(\w)(\d)(\d)) … à TP suivi d’une lettre et de deux chiffres # )? groupe optionnel # (\s+ un ou plusieurs espaces # \(.+\)) un ou plusieurs caractères quelconques entre parenthèses # ? groupe optionnel # $ fin de la ligne group_regex = re.compile(r"^(.+?)\s+((CM(\w))|(TD(\w)(\d))|(TP(\w)(\d)(\d)))?(\s+\(.+\))?$") search = group_regex.search(name) if search is None: return name, None, None, None parts = search.groups() if parts[1] is None: # Pas de groupe précis indiqué return parts[0], None, None, None elif parts[2] is not None: # Groupe de CM return parts[0], parts[3], None, None elif parts[4] is not None: # Groupe de TD return parts[0], parts[5], parts[6], None elif parts[7] is not None: # Groupe de TP return parts[0], parts[8], parts[9], parts[10]