-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommon.py
executable file
·180 lines (150 loc) · 5.42 KB
/
common.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from __future__ import unicode_literals
from datetime import date, timedelta
from calendar import monthrange
from django.utils import timezone
from django.utils.six.moves import xrange
get_now = lambda: timezone.localtime(timezone.now())
now = get_now()
# Warning! please do not use 'now' anywhere: it is initialized on first import
# and so it will not change for days in some environments. It's here just for backward
# compatibility
def inc_month(month, year):
"""
Increment the month and, if neccessary, the year.
Both month and year should be ints.
"""
month += 1
if month > 12:
month = 1
year += 1
return month, year
def _inc_day(year, month, day, net):
"""Increments the day by converting to a datetime.date()."""
d = date(year, month, day)
new_d = d + timezone.timedelta(days=net)
return new_d.year, new_d.month, new_d.day
def get_net_category_tag(req):
if req.META['QUERY_STRING']:
net = get_net(req)
category, tag = get_category_tag(req)
else:
net = 0
category = None
tag = None
return net, category, tag
def get_net(req):
"""Get the net of any 'next' and 'prev' querystrings."""
try:
nxt, prev = map(
int, (req.GET.get('cal_next', 0), req.GET.get('cal_prev', 0))
)
net = nxt - prev
except Exception:
net = 0
return net
def get_category_tag(req):
"""Get value of any category and/or tag querystrings"""
return req.GET.get('cal_category', None), req.GET.get('cal_tag', None)
def get_qs(old_qs):
there = (
'cal_mini', 'cal_next', 'cal_prev', 'cal_month', 'cal_year'
)
return [x for x in old_qs.split('&') if x.split('=')[0] not in there]
# TODO change from d to something more descriptive
def order_events(events, d=False):
"""
Group events that occur on the same day, then sort them alphabetically
by title, then sort by day. Returns a list of tuples that looks like
[(day: [events])], where day is the day of the event(s), and [events]
is an alphabetically sorted list of the events for the day.
"""
ordered_events = {}
for event in events:
try:
for occ in event.occurrence:
try:
ordered_events[occ].append(event)
except Exception:
ordered_events[occ] = [event]
except AttributeError: # no occurrence for this event
# This shouldn't happen, since an event w/o an occurrence
# shouldn't get this far, but if it does, just skip it since
# it shouldn't be displayed on the calendar anyway.
pass
if d:
# return as a dict without sorting by date
return ordered_events
else:
# return ordered_events as a list tuples sorted by date
return sorted(ordered_events.items())
def get_next_and_prev(net):
"""Returns what the next and prev querystrings should be."""
if net == 0:
nxt = prev = 1
elif net > 0:
nxt = net + 1
prev = -(net - 1)
else:
nxt = net + 1
prev = abs(net) + 1
return nxt, prev
def _check_year(year, month, error, error_msg):
"""Checks that the year is within 50 years from now."""
if year not in xrange((now.year - 50), (now.year + 51)):
year = now.year
month = now.month
error = error_msg
return year, month, error
def clean_year_month_day(year, month, day, net):
error = False
error_msg = "The date given was invalid."
# check that the month is within 1-12
if month not in xrange(1, 13):
month = now.month
error = error_msg
# check that the day is within range for the month
if day not in xrange(1, monthrange(year, month)[1] + 1):
day = 1
error = error_msg
# if no error yet, increment the day by net then check the year
if not error:
year, month, day = _inc_day(year, month, day, net)
year, month, error = _check_year(year, month, error, error_msg)
return year, month, day, error
def clean_year_month(year, month, month_orig):
"""
If 'month_orig', which is the month given in the url BEFORE any next/prev
query strings have been applied, is out of range, sets month to the
current month and returns an error message. Also Returns an error
message if the year given is +/- 50 years from now.
If 'month', which is the month given in the url AFTER any next/prev
query strings have been applied, is out of range, adjusts it to be
in range (by also adjusting the year).
"""
error = False
error_msg = "The date given was invalid."
if month_orig not in xrange(1, 13) and month_orig is not None:
month = now.month
error = error_msg
# This takes care of 'next' query strings making month > 12
while month > 12:
month -= 12
year += 1
# This takes care of 'prev' query strings making month < 1
while month < 1:
month += 12
year -= 1
year, month, error = _check_year(year, month, error, error_msg)
return year, month, error
def check_weekday(year, month, day, reverse=False):
"""
Make sure any event day we send back for weekday repeating
events is not a weekend.
"""
d = date(year, month, day)
while d.weekday() in (5, 6):
if reverse:
d -= timedelta(days=1)
else:
d += timedelta(days=1)
return d.year, d.month, d.day