-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared.py
29 lines (21 loc) · 852 Bytes
/
shared.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
from datetime import datetime, timedelta, tzinfo
import pytz
def preprocess(timezone, default_period=timedelta(days=30)):
"""Decorator that returns a function that takes a Request-like object with a
'since' parameter of the form YYYYmmdd and calls the wrapped function with
a datetime.
"""
if not isinstance(timezone, tzinfo):
timezone = pytz.timezone(timezone)
def wrapper_fn(view_fn):
def wrapped(req):
since = req["since"]
if since:
since = timezone.localize(datetime.strptime(since, "%Y%m%d"))
else:
now = pytz.utc.localize(datetime.utcnow()).astimezone(timezone)
since = now - default_period
return view_fn(since)
wrapped.__name__ = view_fn.__name__
return wrapped
return wrapper_fn