Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct usage of jsonify with HTTP status #906

Merged
merged 3 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions apollo/api/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,30 @@


def process_expired_token(jwt_header, jwt_payload):
return jsonify({
response = jsonify({
'status': _('error'),
'message': _('Token has expired')
}), HTTPStatus.UNAUTHORIZED
})
response.status_code = HTTPStatus.UNAUTHORIZED
return response


def process_invalid_token(reason):
return jsonify({
response = jsonify({
'status': _('error'),
'message': _(reason)
}), HTTPStatus.UNPROCESSABLE_ENTITY
})
response.status_code = HTTPStatus.UNPROCESSABLE_ENTITY
return response


def process_revoked_token(jwt_header, jwt_payload):
return jsonify({
response = jsonify({
'status': _('error'),
'message': _('Token has been revoked')
}), HTTPStatus.UNAUTHORIZED
})
response.status_code = HTTPStatus.UNAUTHORIZED
return response


def check_if_token_is_blocklisted(jwt_header, jwt_payload):
Expand Down
9 changes: 7 additions & 2 deletions apollo/participants/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ def login():

if participant is None:
response = {'message': gettext('Login failed'), 'status': 'error'}
return jsonify(response), HTTPStatus.FORBIDDEN
rv = jsonify(response)
rv.status_code = HTTPStatus.FORBIDDEN

return rv

access_token = create_access_token(
identity=str(participant.uuid), fresh=True)
Expand Down Expand Up @@ -259,7 +262,9 @@ def get_forms():
'status': 'error'
}

return jsonify(response), HTTPStatus.BAD_REQUEST
rv = jsonify(response)
rv.status_code = HTTPStatus.BAD_REQUEST
return rv

forms, serials = _get_form_data(participant)

Expand Down
56 changes: 36 additions & 20 deletions apollo/submissions/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,37 +128,41 @@ def checklist_qa_status(uuid):
try:
participant = Participant.query.filter_by(uuid=participant_uuid).one()
except NoResultFound:
response = {
response_body = {
'message': gettext('Invalid participant'),
'status': 'error'
}

return jsonify(response), HTTPStatus.BAD_REQUEST
response = jsonify(response_body)
response.status_code = HTTPStatus.BAD_REQUEST
return response

try:
submission = Submission.query.filter_by(
uuid=uuid, participant_id=participant.id).one()
except NoResultFound:
response = {
response_body = {
'message': gettext('Invalid checklist'),
'status': 'error'
}

return jsonify(response), HTTPStatus.BAD_REQUEST
response = jsonify(response_body)
response.status_code = HTTPStatus.BAD_REQUEST
return response

form = submission.form
submission_qa_status = [
qa_status(submission, check) for check in form.quality_checks] \
if form.quality_checks else []
passed_qa = QUALITY_STATUSES['FLAGGED'] not in submission_qa_status

response = {
response_body = {
'message': gettext('Ok'),
'status': 'ok',
'passedQA': passed_qa
}

return jsonify(response)
return jsonify(response_body)


@csrf.exempt
Expand All @@ -167,12 +171,14 @@ def submission():
try:
request_data = json.loads(request.form.get('submission'))
except Exception:
response = {
response_body = {
'message': gettext('Invalid data sent'),
'status': 'error'
}

return jsonify(response), HTTPStatus.BAD_REQUEST
response = jsonify(response_body)
response.status_code = HTTPStatus.BAD_REQUEST
return response

form_id = request_data.get('form')
form_serial = request_data.get('serial')
Expand All @@ -181,45 +187,53 @@ def submission():

form = filter_form(form_id)
if form is None:
response = {
response_body = {
'message': gettext('Invalid form'),
'status': 'error'
}

return jsonify(response), HTTPStatus.BAD_REQUEST
response = jsonify(response_body)
response.status_code = HTTPStatus.BAD_REQUEST
return response

try:
participant = Participant.query.filter_by(uuid=participant_uuid).one()
except NoResultFound:
response = {
response_body = {
'message': gettext('Invalid participant'),
'status': 'error'
}

return jsonify(response), HTTPStatus.BAD_REQUEST
response = jsonify(response_body)
response.status_code = HTTPStatus.BAD_REQUEST
return response

participant = filter_participants(form, participant.participant_id)
if participant is None:
response = {
response_body = {
'message': gettext('Invalid participant'),
'status': 'error'
}

return jsonify(response), HTTPStatus.BAD_REQUEST
response = jsonify(response_body)
response.status_code = HTTPStatus.BAD_REQUEST
return response

# validate payload
schema_class = form.create_schema()
data, errors = schema_class().load(payload)
if errors:
error_fields = sorted(errors.keys())
response = {
response_body = {
'message': gettext('Invalid value(s) for: %(fields)s',
fields=','.join(error_fields)),
'status': 'error',
'errorFields': error_fields,
}

return jsonify(response), HTTPStatus.BAD_REQUEST
response = jsonify(response_body)
response.status_code = HTTPStatus.BAD_REQUEST
return response

current_event = getattr(g, 'event', Event.default())
current_events = Event.overlapping_events(current_event)
Expand Down Expand Up @@ -268,11 +282,13 @@ def submission():

# if submission is None, there's no submission
if submission is None:
response = {
response_body = {
'message': gettext('Could not update data. Please check your ID'),
'status': 'error'
}
return jsonify(response), HTTPStatus.BAD_REQUEST
response = jsonify(response_body)
response.status_code = HTTPStatus.BAD_REQUEST
return response

data = submission.data.copy() if submission.data else {}
payload2 = payload.copy()
Expand Down Expand Up @@ -376,7 +392,7 @@ def submission():

# return the submission ID so that any updates
# (for example, sending attachments) can be done
response = {
response_body = {
'message': gettext('Data successfully submitted'),
'status': 'ok',
'submission': submission.id,
Expand All @@ -385,4 +401,4 @@ def submission():
'_id': submission.uuid,
}

return jsonify(response)
return jsonify(response_body)