Fix up how the pager view works and how we return errors from some views

so that we can propogate non 200 statuses
This commit is contained in:
Matthew Jones
2014-07-16 12:29:36 -04:00
parent fe764c2114
commit 364e185412
2 changed files with 44 additions and 28 deletions

View File

@@ -7,6 +7,7 @@ import functools
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.settings import api_settings from rest_framework.settings import api_settings
from rest_framework import status
def paginated(method): def paginated(method):
"""Given an method with a Django REST Framework API method signature """Given an method with a Django REST Framework API method signature
@@ -46,29 +47,34 @@ def paginated(method):
kwargs['ordering'] = ordering kwargs['ordering'] = ordering
# Okay, call the underlying method. # Okay, call the underlying method.
results, count = method(self, request, *args, **kwargs) results, count, stat = method(self, request, *args, **kwargs)
if stat is None:
stat = status.HTTP_200_OK
# Determine the next and previous pages, if any. if stat == status.HTTP_200_OK:
prev, next_ = None, None # Determine the next and previous pages, if any.
if page > 1: prev, next_ = None, None
get_copy = copy.copy(request.GET) if page > 1:
get_copy['page'] = page - 1 get_copy = copy.copy(request.GET)
prev = '%s?%s' % (request.path, get_copy.urlencode()) get_copy['page'] = page - 1
if count > offset + limit: prev = '%s?%s' % (request.path, get_copy.urlencode())
get_copy = copy.copy(request.GET) if count > offset + limit:
get_copy['page'] = page + 1 get_copy = copy.copy(request.GET)
next_ = '%s?%s' % (request.path, get_copy.urlencode()) get_copy['page'] = page + 1
next_ = '%s?%s' % (request.path, get_copy.urlencode())
# Compile the results into a dictionary with pagination # Compile the results into a dictionary with pagination
# information. # information.
answer = collections.OrderedDict(( answer = collections.OrderedDict((
('count', count), ('count', count),
('next', next_), ('next', next_),
('previous', prev), ('previous', prev),
('results', results), ('results', results),
)) ))
else:
answer = results
# Okay, we're done; return response data. # Okay, we're done; return response data.
return Response(answer) return Response(answer, status=stat)
return func return func

View File

@@ -1630,7 +1630,10 @@ class JobJobPlaysList(BaseJobEventsList):
@paginated @paginated
def get(self, request, limit, offset, ordering, *args, **kwargs): def get(self, request, limit, offset, ordering, *args, **kwargs):
all_plays = [] all_plays = []
job = get_object_or_404(self.parent_model, pk=self.kwargs['pk']) job = Job.objects.filter(pk=self.kwargs['pk'])
if not job.exists():
return ({'detail': 'job not found'}, -1, status.HTTP_404_NOT_FOUND)
job = job[0]
# Put together a queryset for relevant job events. # Put together a queryset for relevant job events.
qs = job.job_events.filter(event='playbook_on_play_start') qs = job.job_events.filter(event='playbook_on_play_start')
@@ -1686,7 +1689,7 @@ class JobJobPlaysList(BaseJobEventsList):
all_plays.append(play_details) all_plays.append(play_details)
# Done; return the plays and the total count. # Done; return the plays and the total count.
return all_plays, count return all_plays, count, None
class JobJobTasksList(BaseJobEventsList): class JobJobTasksList(BaseJobEventsList):
@@ -1709,11 +1712,18 @@ class JobJobTasksList(BaseJobEventsList):
# Get the job and the parent task. # Get the job and the parent task.
# If there's no event ID specified, this will return a 404. # If there's no event ID specified, this will return a 404.
# FIXME: Make this a good error message. job = Job.objects.filter(pk=self.kwargs['pk'])
job = get_object_or_404(self.parent_model, pk=self.kwargs['pk']) if not job.exists():
parent_task = get_object_or_404(job.job_events, return ({'detail': 'job not found'}, -1, status.HTTP_404_NOT_FOUND)
pk=int(request.QUERY_PARAMS.get('event_id', -1)), job = job[0]
)
if 'event_id' not in request.QUERY_PARAMS:
return ({'detail': '"event_id" not provided'}, -1, status.HTTP_400_BAD_REQUEST)
parent_task = job.job_events.filter(pk=int(request.QUERY_PARAMS.get('event_id', -1)))
if not parent_task.exists():
return ({'detail': 'parent event not found'}, -1, status.HTTP_404_NOT_FOUND)
parent_task = parent_task[0]
# Some events correspond to a playbook or task starting up, # Some events correspond to a playbook or task starting up,
# and these are what we're interested in here. # and these are what we're interested in here.
@@ -1818,7 +1828,7 @@ class JobJobTasksList(BaseJobEventsList):
results.append(task_data) results.append(task_data)
# Done; return the results and count. # Done; return the results and count.
return results, count return (results, count, None)
class UnifiedJobTemplateList(ListAPIView): class UnifiedJobTemplateList(ListAPIView):