Skip to content

Commit

Permalink
Merge pull request #463 from ox-it/url-time-delta-3474
Browse files Browse the repository at this point in the history
Add time delta for api date ranges - 3747
  • Loading branch information
markdoub committed May 6, 2016
2 parents 2c32200 + 37f0888 commit a2db1b1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion talks/api/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def events_search(parameters):
else:
queries.append(Q(start__gt=from_date))

to_date = parse_date(parameters.get("to"))
to_date = parse_date(parameters.get("to"), from_date)
if to_date:
queries.append(Q(start__lt=to_date+timedelta(1)))

Expand Down
6 changes: 3 additions & 3 deletions talks/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def api_event_group(request, event_group_slug):
return Response({'error': "Item not found"},
status=status.HTTP_404_NOT_FOUND)
from_date = parse_date(request.GET.get('from', ''))
to_date = parse_date(request.GET.get('to',''))
to_date = parse_date(request.GET.get('to',''), from_date)

if from_date or to_date:
serializer = HALEventGroupSerializer(eg, context={'request': request, 'from-date': from_date, 'to-date': to_date})
Expand Down Expand Up @@ -318,7 +318,7 @@ def api_person(request, person_slug):
"""Get events associated with a person
"""
from_date = parse_date(request.GET.get('from', ''))
to_date = parse_date(request.GET.get('to', ''))
to_date = parse_date(request.GET.get('to', ''), from_date)
try:
person = Person.objects.get(slug=person_slug)
if from_date or to_date:
Expand Down Expand Up @@ -359,7 +359,7 @@ def api_collection(request, collection_slug):

# If from and to dates have been passed as request parameters, filter the events by those dates.
from_date = parse_date(request.GET.get('from', ''))
to_date = parse_date(request.GET.get('to', ''))
to_date = parse_date(request.GET.get('to', ''), from_date)
try:
collection = Collection.objects.get(slug=collection_slug)
if collection.public:
Expand Down
29 changes: 22 additions & 7 deletions talks/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,51 @@ def clean_xml(dirty):
clean = _illegal_xml_chars_RE.sub('', str(dirty))
return clean

def parse_date(date_param):
def parse_date(date_param, from_date=None):
"""
Parse the date string parameter
:param date_param:
Either a keyword:
'today', 'tomorrow'
or a string in the format 'dd/mm/yy'
or a time delta, i.e. +7
:from_date:
In
:return:
datetime object
"""

if not date_param:
return None
elif date_param == "today":
from_date = datetime.today().date()
date = datetime.today().date()
elif date_param == "tomorrow":
from_date = datetime.today().date() + timedelta(1)
date = datetime.today().date() + timedelta(1)
elif date_param[0:4] == "plus":
if not from_date:
raise ParseError("Cannot use time delta without a from date")
try:
delta = int(date_param[4:])
date = from_date+timedelta(delta)
except Exception as e:
raise ParseError(e.message)
date = from_d
else:
print "date_param=",date_param
print date_param[0:4]
try:
from_date = datetime.strptime(date_param, "%d/%m/%y")
date = datetime.strptime(date_param, "%d/%m/%y")
except Exception as e:
try:
from_date = datetime.strptime(date_param, "%d/%m/%Y")
date = datetime.strptime(date_param, "%d/%m/%Y")
except Exception as e:
try:
from_date = datetime.strptime(date_param, "%Y-%m-%d")
date = datetime.strptime(date_param, "%Y-%m-%d")
except Exception as e:
# catch the exception and raise an API exception instead, which the user will see
raise ParseError(e.message)
# TODO should raised a more specialised error than rest framework.
return from_date
return date


def read_yaml_param(fname, key):
Expand Down

0 comments on commit a2db1b1

Please sign in to comment.