aboutsummaryrefslogtreecommitdiff
path: root/utils.py
blob: 5a9218e5d4cf0ad7eff4b2598caa1b5014a83a25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#    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 <http://www.gnu.org/licenses/>.

import datetime
import re

from django.utils import timezone

def get_current_week():
    return tz_now().isocalendar()[:2]

def get_current_or_next_week():
    year, week, day = tz_now().isocalendar()
    if day >= 6:
        year, week, _ = (tz_now() + datetime.timedelta(weeks=1)).isocalendar()

    return year, week

def get_week(year, week):
    start = timezone.make_aware(datetime.datetime.strptime(
        "{0}-W{1}-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*(s\d\s+)?)((CM|TD|TP|G)(\w\d{0,3}))?(\s+\(.+\))?$
    # ^                                                           début de la ligne
    #  ((.+?)                                                     correspond à au moins un caractère
    #        \s*                                                  éventuellement un ou plusieurs espaces
    #           (s\d\s+)?)                                        éventuellement un s suivi d’un nombre et d’un ou plusieurs espaces
    #                     ((CM|TD|TP|G)                           « CM » ou « TD » ou « TP » ou « G »
    #                                  (\w\d{0,3})                suivi d’un caractère puis entre 0 et 3 chiffres
    #                                             )?              groupe optionnel
    #                                               (\s*          éventuellement un ou plusieurs espaces
    #                                                   \(.+\))?  un ou plusieurs caractères entre parenthèses
    #                                                           $ fin de la ligne
    group_regex = re.compile(r"^((.+?)\s*(s\d\s+)?)((CM|TD|TP|G)(\w\d{0,3}))?(\s*\(.+\))?$")
    search = group_regex.search(name)
    if search is None:
        return name, None

    parts = search.groups()
    return parts[0], parts[5]

def tz_now():
    """Retourne la date et l’heure avec le bon fuseau horaire"""
    return timezone.make_aware(datetime.datetime.now())