api: add jobs endpoint to get afterjobs status (#52137)

This commit is contained in:
Thomas NOËL 2021-03-17 16:22:05 +01:00
parent 748b70a535
commit 96aac2c5e1
3 changed files with 72 additions and 0 deletions

View File

@ -11,6 +11,7 @@ from utilities import clean_temporary_pub, create_temporary_pub, get_app
from wcs.api_utils import sign_url
from wcs.formdef import FormDef
from wcs.qommon.afterjobs import AfterJob
from wcs.qommon.http_request import HTTPRequest
@ -216,3 +217,28 @@ def test_geocoding(pub):
urlopen.call_args[0][0]
== 'http://reverse.example.net/?param=value&format=json&q=test&accept-language=en'
)
def test_afterjobs_status(pub):
job = AfterJob('test')
job.store()
# missing signature
get_app(pub).get('/api/jobs/%s/' % job.id, status=403)
# unknown id
resp = get_app(pub).get(sign_url('/api/jobs/not-a-job-id/?orig=coucou', '1234'), status=404)
assert resp.json['err'] == 1
# without trailing /
resp = get_app(pub).get(sign_url('/api/jobs/%s?orig=coucou' % job.id, '1234'), status=404)
resp = get_app(pub).get(sign_url('/api/jobs/%s/?orig=coucou' % job.id, '1234'), status=200)
assert resp.json == {
'err': 0,
'data': {
'label': 'test',
'status': 'registered',
'creation_time': job.creation_time,
'completion_time': None,
'completion_status': None,
},
}

View File

@ -211,6 +211,12 @@ def test_cards_import_csv(pub, local_user):
job_id = resp.json['data']['job']['id']
job = AfterJob.get(job_id)
assert AfterJob.get(job_id).status == 'completed'
# get job status from its api url
resp = get_app(pub).get(sign_uri(resp.json['data']['job']['url'], user=local_user))
assert resp.json['err'] == 0
assert resp.json['data']['label'] == 'Importing data into cards'
assert resp.json['data']['status'] == 'completed'
assert resp.json['data']['creation_time'] <= resp.json['data']['completion_time']
def test_post_invalid_json(pub, local_user):

View File

@ -47,6 +47,7 @@ from wcs.data_sources import NamedDataSource
from wcs.data_sources import get_object as get_data_source_object
from wcs.roles import logged_users_role
from wcs.forms.common import FormStatusPage
from wcs.qommon.afterjobs import AfterJob
import wcs.qommon.storage as st
from wcs.api_utils import sign_url_auto_orig, is_url_signed, get_user_from_api_query_string, get_query_flag
@ -353,6 +354,7 @@ class ApiCardPage(ApiFormPageMixin, BackofficeCardPage):
'data': {
'job': {
'id': job.id,
'url': get_publisher().get_frontoffice_url() + '/api/jobs/%s/' % job.id,
}
},
}
@ -1088,6 +1090,42 @@ class GeoJsonDirectory(Directory):
return json.dumps(data_source.get_geojson_data(force_url=url))
class AfterJobDirectory(Directory):
_q_exports = ['']
def __init__(self, afterjob):
self.afterjob = afterjob
def _q_index(self):
get_response().set_content_type('application/json')
return json.dumps(
{
'err': 0,
'data': {
'status': self.afterjob.status,
'label': self.afterjob.label,
'creation_time': self.afterjob.creation_time,
'completion_time': self.afterjob.completion_time,
'completion_status': self.afterjob.completion_status,
},
},
cls=misc.JSONEncoder,
)
class AfterJobsDirectory(Directory):
_q_exports = ['']
def _q_lookup(self, component):
if not (is_url_signed() or (get_request().user and get_request().user.is_admin)):
raise AccessForbiddenError('unsigned request or user is not admin')
try:
afterjob = AfterJob.get(component, ignore_errors=False)
except KeyError:
raise TraversalError()
return AfterJobDirectory(afterjob)
class ApiDirectory(Directory):
_q_exports = [
'forms',
@ -1101,6 +1139,7 @@ class ApiDirectory(Directory):
'autocomplete',
'cards',
'geojson',
'jobs',
]
cards = ApiCardsDirectory()
@ -1112,6 +1151,7 @@ class ApiDirectory(Directory):
code = ApiTrackingCodeDirectory()
autocomplete = AutocompleteDirectory()
geojson = GeoJsonDirectory()
jobs = AfterJobsDirectory()
def roles(self):
get_response().set_content_type('application/json')