aboutsummaryrefslogtreecommitdiff
path: root/utils.py
blob: 1a177b453678371ade5169b5b0312e52acba8bde (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
from django.utils import timezone

import datetime

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