-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathannoucements.py
62 lines (51 loc) · 1.76 KB
/
annoucements.py
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
import datetime
from enum import Enum
from django.utils import timezone
from apps.db_data.models import Event
class AnnouncementType(Enum):
WEEK = "week"
TOMORROW = "tomorrow"
TODAY = "today"
HOUR = "hour"
B_TIME = "now" # Berkeley Time
def get_events_in_time_delta(requested_atype: AnnouncementType):
"""
Retrieves a list of Event objects within the requested
time delta.
requested_atype will take in enum constants in
AnnouncementType
"""
now = timezone.now().astimezone(timezone.get_current_timezone())
events = get_events_in_time_range(*timeify(requested_atype))
return events
def get_events_in_time_range(start_time, end_time):
"""
Takes in a two datetime objects, start_time and end_time
Returns events within the datetime range
"""
events = Event.objects.filter(start_time__gte=start_time).filter(
start_time__lte=end_time
)
return events
def timeify(requested_atype: AnnouncementType):
"""
Converts requested_tdelta string into a corresponding datetime object
"""
now = timezone.now()
time_ranges = {
AnnouncementType.WEEK: (now, now + datetime.timedelta(weeks=1)),
AnnouncementType.TOMORROW: (
now + datetime.timedelta(days=1),
now + datetime.timedelta(days=1, hours=23, minutes=59, seconds=59),
),
AnnouncementType.TODAY: (
now,
now + datetime.timedelta(hours=23, minutes=59, seconds=59),
),
AnnouncementType.HOUR: (
now + datetime.timedelta(hours=1),
now + datetime.timedelta(hours=1, minutes=59, seconds=59),
),
AnnouncementType.B_TIME: (now, now + datetime.timedelta(minutes=10)),
}
return time_ranges[requested_atype]