зеркало из https://github.com/mozilla/treeherder.git
code cleanup in the views exception handling
This commit is contained in:
Родитель
9543d8d78a
Коммит
12dbe79fc0
|
@ -90,6 +90,4 @@ def test_objectstore_create_bad_project(webapp, job_sample, jm):
|
|||
status=404
|
||||
)
|
||||
assert resp.status_int == 404
|
||||
assert resp.json['message'] == ("No dataset found for project "
|
||||
"u'badproject', contenttype "
|
||||
"'objectstore'.")
|
||||
assert resp.json['message'] == "No project with name badproject"
|
||||
|
|
|
@ -44,7 +44,7 @@ def test_resultset_list_bad_project(webapp, jm):
|
|||
)
|
||||
|
||||
assert resp.status_int == 404
|
||||
assert resp.json == {"message": "No dataset found for project u'foo', contenttype 'jobs'."}
|
||||
assert resp.json == {"message": "No project with name foo"}
|
||||
|
||||
|
||||
def test_resultset_detail(webapp, eleven_jobs_processed, jm):
|
||||
|
@ -92,7 +92,7 @@ def test_result_set_detail_bad_project(webapp, jm):
|
|||
expect_errors=True
|
||||
)
|
||||
assert resp.status_int == 404
|
||||
assert resp.json == {"message": "No dataset found for project u'foo', contenttype 'jobs'."}
|
||||
assert resp.json == {"message": "No project with name foo"}
|
||||
|
||||
|
||||
def test_warning_levels_red_on_fail():
|
||||
|
|
|
@ -764,11 +764,13 @@ class JobsModel(TreeherderModelBase):
|
|||
|
||||
def get_json_blob_by_guid(self, guid):
|
||||
"""retrieves a json_blob given its guid"""
|
||||
return self.get_os_dhub().execute(
|
||||
iter_obj = self.get_os_dhub().execute(
|
||||
proc="objectstore.selects.get_json_blob_by_guid",
|
||||
placeholders=[guid],
|
||||
debug_show=self.DEBUG
|
||||
debug_show=self.DEBUG,
|
||||
return_type='iter',
|
||||
)
|
||||
return self.as_single(iter_obj, "objectstore", guid=guid)
|
||||
|
||||
def get_json_blob_list(self, page, limit):
|
||||
"""
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
import simplejson as json
|
||||
import itertools
|
||||
|
||||
from django.http import Http404
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.decorators import action
|
||||
|
@ -12,6 +11,31 @@ from treeherder.model.derived import (JobsModel, DatasetNotFoundError,
|
|||
ObjectNotFoundException)
|
||||
|
||||
|
||||
def use_jobs_model(project, model_func):
|
||||
"""
|
||||
Create a jobsmodel and pass it to the ``func``.
|
||||
|
||||
``func`` must take a jobsmodel object and return a response object
|
||||
|
||||
Catches exceptions
|
||||
"""
|
||||
try:
|
||||
jm = JobsModel(project)
|
||||
return model_func(jm)
|
||||
|
||||
except DatasetNotFoundError as e:
|
||||
return Response(
|
||||
{"message": "No project with name {0}".format(project)},
|
||||
status=404,
|
||||
)
|
||||
except ObjectNotFoundException as e:
|
||||
return Response({"message": unicode(e)}, status=404)
|
||||
except Exception as e: # pragma nocover
|
||||
return Response({"message": unicode(e)}, status=500)
|
||||
finally:
|
||||
jm.disconnect()
|
||||
|
||||
|
||||
class ObjectstoreViewSet(viewsets.ViewSet):
|
||||
"""
|
||||
This view is responsible for the objectstore endpoint.
|
||||
|
@ -24,41 +48,37 @@ class ObjectstoreViewSet(viewsets.ViewSet):
|
|||
"""
|
||||
POST method implementation
|
||||
"""
|
||||
jm = JobsModel(project)
|
||||
try:
|
||||
def model_func(jm):
|
||||
jm.store_job_data(
|
||||
json.dumps(request.DATA),
|
||||
request.DATA['job']['job_guid']
|
||||
)
|
||||
except DatasetNotFoundError as e:
|
||||
return Response({"message": str(e)}, status=404)
|
||||
except Exception as e: # pragma nocover
|
||||
return Response({"message": str(e)}, status=500)
|
||||
finally:
|
||||
jm.disconnect()
|
||||
|
||||
return Response({'message': 'well-formed JSON stored'})
|
||||
|
||||
return use_jobs_model(project, model_func)
|
||||
|
||||
def retrieve(self, request, project, pk=None):
|
||||
"""
|
||||
GET method implementation for detail view
|
||||
"""
|
||||
jm = JobsModel(project)
|
||||
def model_func(jm):
|
||||
obj = jm.get_json_blob_by_guid(pk)
|
||||
if obj:
|
||||
return Response(json.loads(obj[0]['json_blob']))
|
||||
else:
|
||||
raise Http404()
|
||||
return Response(json.loads(obj['json_blob']))
|
||||
|
||||
return use_jobs_model(project, model_func)
|
||||
|
||||
def list(self, request, project):
|
||||
"""
|
||||
GET method implementation for list view
|
||||
"""
|
||||
page = request.QUERY_PARAMS.get('page', 0)
|
||||
jm = JobsModel(project)
|
||||
page = int(request.QUERY_PARAMS.get('page', 0))
|
||||
|
||||
def model_func(jm):
|
||||
objs = jm.get_json_blob_list(page, 10)
|
||||
return Response([json.loads(obj['json_blob']) for obj in objs])
|
||||
|
||||
return use_jobs_model(project, model_func)
|
||||
|
||||
|
||||
class ArtifactViewSet(viewsets.ViewSet):
|
||||
"""
|
||||
|
@ -70,23 +90,14 @@ class ArtifactViewSet(viewsets.ViewSet):
|
|||
GET method implementation for an artifact blob
|
||||
|
||||
"""
|
||||
try:
|
||||
jm = JobsModel(project)
|
||||
def model_func(jm):
|
||||
obj = jm.get_job_artifact(pk)
|
||||
if obj["type"] == "json":
|
||||
obj["blob"] = json.loads(obj["blob"])
|
||||
except DatasetNotFoundError as e:
|
||||
return Response(
|
||||
{"message": "No project with name {0}".format(project)},
|
||||
status=404,
|
||||
)
|
||||
except ObjectNotFoundException as e:
|
||||
return Response({"message": unicode(e)}, status=404)
|
||||
except Exception as e: # pragma nocover
|
||||
return Response({"message": unicode(e)}, status=500)
|
||||
|
||||
return Response(obj)
|
||||
|
||||
return use_jobs_model(project, model_func)
|
||||
|
||||
|
||||
class JobsViewSet(viewsets.ViewSet):
|
||||
"""
|
||||
|
@ -101,8 +112,7 @@ class JobsViewSet(viewsets.ViewSet):
|
|||
Return a single job with log_references and
|
||||
artifact names and links to the artifact blobs.
|
||||
"""
|
||||
try:
|
||||
jm = JobsModel(project)
|
||||
def model_func(jm):
|
||||
job = jm.get_job(pk)
|
||||
job["logs"] = jm.get_log_references(pk)
|
||||
|
||||
|
@ -115,33 +125,21 @@ class JobsViewSet(viewsets.ViewSet):
|
|||
art["resource_uri"] = ref
|
||||
job["artifacts"].append(art)
|
||||
|
||||
except DatasetNotFoundError as e:
|
||||
return Response(
|
||||
{"message": "No project with name {0}".format(project)},
|
||||
status=404,
|
||||
)
|
||||
except ObjectNotFoundException as e:
|
||||
return Response({"message": unicode(e)}, status=404)
|
||||
except Exception as e: # pragma nocover
|
||||
return Response({"message": unicode(e)}, status=500)
|
||||
|
||||
return Response(job)
|
||||
|
||||
return use_jobs_model(project, model_func)
|
||||
|
||||
def list(self, request, project):
|
||||
"""
|
||||
GET method implementation for list view
|
||||
"""
|
||||
try:
|
||||
page = request.QUERY_PARAMS.get('page', 0)
|
||||
jm = JobsModel(project)
|
||||
|
||||
def model_func(jm):
|
||||
objs = jm.get_job_list(page, 10)
|
||||
return Response(objs)
|
||||
except DatasetNotFoundError as e:
|
||||
return Response({"message": unicode(e)}, status=404)
|
||||
except Exception as e: # pragma nocover
|
||||
return Response({"message": unicode(e)}, status=500)
|
||||
finally:
|
||||
jm.disconnect()
|
||||
|
||||
return use_jobs_model(project, model_func)
|
||||
|
||||
@action()
|
||||
def update_state(self, request, project, pk=None):
|
||||
|
@ -149,7 +147,8 @@ class JobsViewSet(viewsets.ViewSet):
|
|||
Change the state of a job.
|
||||
"""
|
||||
state = request.DATA.get('state', None)
|
||||
jm = JobsModel(project)
|
||||
|
||||
def model_func(jm):
|
||||
|
||||
# check that this state is valid
|
||||
if state not in jm.STATES:
|
||||
|
@ -165,17 +164,13 @@ class JobsViewSet(viewsets.ViewSet):
|
|||
if not pk: # pragma nocover
|
||||
return Response({"message": "job id required"}, status=400)
|
||||
|
||||
try:
|
||||
jm.get_job(pk)
|
||||
jm.set_state(pk, state)
|
||||
jm.disconnect()
|
||||
except ObjectNotFoundException as e:
|
||||
return Response({"message": unicode(e)}, status=404)
|
||||
except Exception as e: # pragma nocover
|
||||
return Response({"message": unicode(e)}, status=500)
|
||||
|
||||
return Response({"message": "state updated to '{0}'".format(state)})
|
||||
|
||||
return use_jobs_model(project, model_func)
|
||||
|
||||
|
||||
class ResultSetViewSet(viewsets.ViewSet):
|
||||
"""
|
||||
|
@ -191,10 +186,9 @@ class ResultSetViewSet(viewsets.ViewSet):
|
|||
|
||||
filters = ["author"]
|
||||
|
||||
jm = JobsModel(project)
|
||||
try:
|
||||
page = request.QUERY_PARAMS.get('page', 0)
|
||||
|
||||
def model_func(jm):
|
||||
objs = jm.get_result_set_list(
|
||||
page,
|
||||
10,
|
||||
|
@ -202,12 +196,8 @@ class ResultSetViewSet(viewsets.ViewSet):
|
|||
if k in filters)
|
||||
)
|
||||
return Response(objs)
|
||||
except DatasetNotFoundError as e:
|
||||
return Response({"message": unicode(e)}, status=404)
|
||||
except Exception as e: # pragma nocover
|
||||
return Response({"message": unicode(e)}, status=500)
|
||||
finally:
|
||||
jm.disconnect()
|
||||
|
||||
return use_jobs_model(project, model_func)
|
||||
|
||||
@classmethod
|
||||
def get_warning_level(cls, groups):
|
||||
|
@ -247,9 +237,7 @@ class ResultSetViewSet(viewsets.ViewSet):
|
|||
"""
|
||||
filters = ["job_type_name"]
|
||||
|
||||
jm = JobsModel(project)
|
||||
|
||||
try:
|
||||
def model_func(jm):
|
||||
rs = jm.get_result_set_by_id(pk)
|
||||
jobs_ungrouped = list(jm.get_result_set_job_list(
|
||||
pk,
|
||||
|
@ -301,14 +289,8 @@ class ResultSetViewSet(viewsets.ViewSet):
|
|||
})
|
||||
|
||||
return Response(rs)
|
||||
except DatasetNotFoundError as e:
|
||||
return Response({"message": unicode(e)}, status=404)
|
||||
except ObjectNotFoundException as e:
|
||||
return Response({"message": unicode(e)}, status=404)
|
||||
except Exception as e: # pragma nocover
|
||||
return Response({"message": unicode(e)}, status=500)
|
||||
finally:
|
||||
jm.disconnect()
|
||||
|
||||
return use_jobs_model(project, model_func)
|
||||
|
||||
|
||||
#####################
|
||||
|
|
Загрузка…
Ссылка в новой задаче