|
1
|
|
|
import re |
|
2
|
|
|
from datetime import timedelta |
|
3
|
|
|
|
|
4
|
|
|
from shoop.utils.dates import parse_date |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
def expand_datestring_without_day(date): |
|
8
|
|
|
"""Expand date string YYYY-MM with day. |
|
9
|
|
|
|
|
10
|
|
|
For example; 2016-02 will become 2016-02-01. |
|
11
|
|
|
|
|
12
|
|
|
Args: |
|
13
|
|
|
string - Date string that will be checked. |
|
14
|
|
|
Returns: |
|
15
|
|
|
string - Expanded date string with "-01" appended, or original given string. |
|
16
|
|
|
""" |
|
17
|
|
|
if re.match("^[0-9]{4}-[0-9]{2}$", date): |
|
18
|
|
|
return "%s-01" % date |
|
19
|
|
|
return date |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
def get_start_and_end_from_request(request): |
|
23
|
|
|
"""Get start and end dates from request. |
|
24
|
|
|
|
|
25
|
|
|
Returns: |
|
26
|
|
|
date, date or None, None |
|
27
|
|
|
""" |
|
28
|
|
|
start = getattr(request, request.method).get("start", None) |
|
29
|
|
|
if start: |
|
30
|
|
|
start = expand_datestring_without_day(start) |
|
31
|
|
|
end = getattr(request, request.method).get("end", None) |
|
32
|
|
|
if end: |
|
33
|
|
|
end = expand_datestring_without_day(end) |
|
34
|
|
|
if "days" in getattr(request, request.method): |
|
35
|
|
|
days = getattr(request, request.method).get("days", None) |
|
36
|
|
|
else: |
|
37
|
|
|
days = getattr(request, request.method).get("quantity", None) |
|
38
|
|
|
if start and (end or days): |
|
39
|
|
|
start_date = parse_date(start) |
|
40
|
|
|
if end: |
|
41
|
|
|
end_date = parse_date(end) |
|
42
|
|
|
else: |
|
43
|
|
|
end_date = start_date + timedelta(days=int(days)) |
|
44
|
|
|
else: |
|
45
|
|
|
return None, None |
|
46
|
|
|
return start_date, end_date |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
def get_persons_from_request(request): |
|
50
|
|
|
"""Get person count from request. |
|
51
|
|
|
|
|
52
|
|
|
Returns: |
|
53
|
|
|
int |
|
54
|
|
|
""" |
|
55
|
|
|
persons = int(getattr(request, request.method).get("persons", 1)) |
|
56
|
|
|
return persons |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
def daterange(start_date, end_date): |
|
60
|
|
|
"""Taken from http://stackoverflow.com/a/1060330/1489738.""" |
|
61
|
|
|
for n in range(int ((end_date - start_date).days)): |
|
62
|
|
|
yield start_date + timedelta(n) |
|
63
|
|
|
|