Repository URL to install this package:
|
Version:
1.0.1 ▾
|
from datetime import datetime
def get_date_interval_from_get(request):
"""
Parses a request and returns the start and end date from it.
Parameters
----------
request: The GET Request Object
Returns
-------
Return a tuple in the form of (start_date, end_date). If either the start date or the end date is not present in the request None is returned in the tuple
"""
start_identifier = ['start', 'start_date', 'from']
end_identifier = ['end', 'end_date', 'to']
start = next((identifier for identifier in start_identifier if identifier in request.GET), None)
end = next((identifier for identifier in end_identifier if identifier in request.GET), None)
if start:
try:
start = datetime.strptime(request.GET.get(start), '%Y-%m-%d').date()
except ValueError:
start = None
if end:
try:
end = datetime.strptime(request.GET.get(end), '%Y-%m-%d').date()
except ValueError:
end = None
return start, end