Commit 86fc95ff authored by Jonas Waeber's avatar Jonas Waeber
Browse files

Add note request to type report writer

parent f0a61a7f
Pipeline #24370 passed with stages
in 2 minutes and 36 seconds
......@@ -11,53 +11,81 @@ class WriteTypeReportToDrupal(Resource):
Write a report for an recordset or institution Drupal
---
tags:
- reporting
- reporting
parameters:
- in: body
name: body
required: true
schema:
items:
type: object
properties:
type:
type: string
example: 'recordset'
description: Either 'recordset' or 'institution'
id:
type: string
example: '123'
description: The id of the type
status:
type: boolean
example: True
description: The status
report:
type: string
example: reporting json...
description: The report
- in: body
name: body
required: true
schema:
item:
type: object
properties:
node_type:
type: string
example: 'recordset'
description: Either 'recordset' or 'institution'.
id:
type: string
example: 'sag'
description: The memobase id of the record set or institution.
status:
type: boolean
example: True
description: The status
report:
type: string
example: "Failed to process record set."
description: Text message of the report.
responses:
200:
description: It was successful
404:
description: No content with such a id found
500:
description: There was a problem
200:
description: It was successful
404:
description: No content with such a id found
500:
description: There was a problem
"""
try:
type = request.json[0]['type']
id = request.json[0]['id']
node_type = request.json[0]['node_type']
identifier = request.json[0]['id']
status = request.json[0]['status']
report = request.json[0]['report']
return write_results(type, id, status, report)
except KeyError as er:
return {
'message': f'Missing a element in input body: {er}'
}, 400
try:
node_id = get_node_id(identifier)
return write_results(node_type, node_id, status, report)
except ImportApiError as e:
current_app.logger.error("exception while writing " + type + "/" + id + "/" +
str(status) + "/" + report + ": " + e.message)
return {'error': e.message}, 500
current_app.logger.error(
f"Could not write report to {node_type}/{identifier}: {e.message}")
return {
'error': f"Could not write report to {node_type}/{identifier}: "
f"{e.message}"}, 500
def get_node_id(memobase_id: str):
current_app.logger.debug(
f'Retrieve node id from durpal json api with memobase id {memobase_id}.')
headers = {
'Content-Type': 'application/vnd.api+json',
'Accept': 'application/vnd.api+json',
'X-API-Key': current_app.config['drupal-cms-admin-key'],
'Authorization': 'Basic bWVtb2Jhc2U6MjAyMA=='
}
url = f'{current_app.config["drupal-api-url"]}/jsonapi/node/institution?filter[' \
f'field_memobase_id]={memobase_id} '
response = requests.get(url, headers=headers)
if response.ok:
return response.json()['data'][0]['attributes']['id']
else:
raise ImportApiError(f"Could not find element for report: {response.text}.")
def write_results(type, id, status, report):
current_app.logger.debug("writing: " + type + "/" + id + "/" + str(status) + "/" + report)
def write_results(node_type, identifier, status, report):
current_app.logger.debug(
"writing: " + node_type + "/" + identifier + "/" + str(status) + "/" + report)
headers = {
'Content-Type': 'application/vnd.api+json',
'Accept': 'application/vnd.api+json',
......@@ -65,20 +93,20 @@ def write_results(type, id, status, report):
'Authorization': 'Basic bWVtb2Jhc2U6MjAyMA=='
}
drupalType = ''
url = current_app.config['drupal-api-url'] + '/jsonapi/node/'
if type == 'institution':
drupalType = 'node--institution'
drupal_type = ''
url = f'{current_app.config["drupal-api-url"]}/jsonapi/node/{identifier}'
if node_type == 'institution':
drupal_type = 'node--institution'
url += 'institution/'
elif type == 'recordset':
drupalType = 'node--record_set'
elif node_type == 'recordset':
drupal_type = 'node--record_set'
url += 'record_set/'
url += id
url += identifier
data = {
"data": {
"id": id,
"type": drupalType,
"id": identifier,
"type": drupal_type,
"attributes": {
"field_migrated": status,
"field_error": report
......@@ -86,7 +114,7 @@ def write_results(type, id, status, report):
}
}
returnVal = {'message': ''}
result = {'message': ''}
try:
response = requests.patch(
......@@ -99,9 +127,9 @@ def write_results(type, id, status, report):
via the following url " + url
current_app.logger.error(message)
raise ImportApiError(message)
if response.status_code == 200:
if response.ok:
current_app.logger.debug('Updated: ' + url)
returnVal['message'] += 'Updated: ' + url + '\n'
result['message'] += 'Updated: ' + url + '\n'
elif response.status_code == 403:
message = "Not authorized to write to: " + url
current_app.logger.error(message)
......@@ -114,4 +142,4 @@ def write_results(type, id, status, report):
message = "Unknown response status code for drupal api for url " + url
current_app.logger.error(message)
raise ImportApiError(message)
return returnVal
return result
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