Commit 31a23c63 authored by BAIRE Anthony's avatar BAIRE Anthony
Browse files

Merge branch '225-api-errors-should-be-returned-in-json-format' into 'django'

Resolve "API errors should be returned in json format"

Closes #225

See merge request !142
parents 2857c5e0 d9df98f7
Pipeline #44139 failed with stage
in 1 minute and 1 second
import hashlib
import logging
import os
import config.env
from django.core.validators import ValidationError
from django.http import HttpResponse, JsonResponse, FileResponse
from django.http import JsonResponse
from django.shortcuts import redirect
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import View
from main.models import Job, Webapp, JobQueue
from main.helpers import upload_data, get_base_url, lookup_job_file, get_request_user
from main.mixins import JobAuthMixin
from main.models import Job, Webapp, JobQueue
log = logging.getLogger('allgo')
......@@ -21,7 +18,6 @@ BUF_SIZE = 65536
def get_link(jobid, dir, filename, request):
filepath = os.path.join(dir, filename)
return '/'.join((get_base_url(request), "datastore", str(jobid), filename))
......@@ -35,17 +31,22 @@ def job_response(job, request):
6: 'aborting'}
job_dir = job.data_dir
files = {f: get_link(, job_dir, f, request) for f in os.listdir(job_dir)
if lookup_job_file(, f)}
if lookup_job_file(, f)}
return {str( {'status': status[job.state],
'files': files
class APIJobView(JobAuthMixin, View):
def get(self, request, pk):
job = Job.objects.get(id=pk)
return JsonResponse(job_response(job, request))
job = Job.objects.get(id=pk)
return JsonResponse(job_response(job, request))
except Job.DoesNotExist as e:
log.error("Job not found %s", str(e))
return JsonResponse({'error': 'Job not found'}, status=404)
......@@ -54,7 +55,7 @@ def jobs(request):
if not user:"API request without http authorisation %s %s %s", request.META['HTTP_USER_AGENT'],
request.META['REMOTE_ADDR'], request.META['QUERY_STRING'])
return HttpResponse(status=401)
return JsonResponse({'error': 'API request without http authorisation'}, status=401)
if request.POST['job[webapp_id]'].isdigit():
app = Webapp.objects.filter(id=request.POST['job[webapp_id]']).first()
......@@ -62,29 +63,28 @@ def jobs(request):
app = Webapp.objects.filter(name=request.POST['job[webapp_id]']).first()
if not app:
return HttpResponse("App not found", status=404)
return JsonResponse({'error': 'Application not found'}, status=404)
queue = app.job_queue
if 'job[queue]' in request.POST:
queue = JobQueue.objects.get(name=request.POST['job[queue]'])
except JobQueue.DoesNotExist:
return HttpResponse("Unknown queue", status=400)
return JsonResponse({'error': 'Unknown queue'}, status=400)"Job submit by user %s", user)
job = Job.objects.create(param=request.POST['job[param]'], queue=queue, webapp=app, user=user.user)
job.version = app.get_webapp_version().number #TODO: add version selection in the api
if job.version is None:
job = Job.objects.create(param=request.POST.get('job[param]', ''), queue=queue, webapp=app, user=user)
if app.get_webapp_version() is None:
log.debug('No usable versions')
return HttpResponse("This app is not yet published", status=404)
return JsonResponse({'error': "This app is not yet published"}, status=404)
job.version = app.get_webapp_version().number # TODO: add version selection in the api
upload_data(request.FILES.values(), job)
# run the Job validators
except ValidationError as e:
return HttpResponse("Invalid parameters: %s" % e.error_dict, status=400)
return JsonResponse({'error': "Invalid parameters: %s" % e.error_dict}, status=400)
# start the job
job.state = Job.WAITING
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment