Commit 47d42f0a authored by Jonas Waeber's avatar Jonas Waeber
Browse files

Refactor institution update endpoint.

parent b007b952
......@@ -10,7 +10,7 @@ from kubernetes.config import ConfigException
from import_api_app.resources.WriteJobResultsToDrupal import WriteJobResultToDrupal
from import_api_app.resources.WriteTypeReportToDrupal import WriteTypeReportToDrupal
# from import_api_app.resources.ReadJobOptionsFromDrupal import ReadJobOptionsFromDrupal
from import_api_app.resources.MonitorDrupalJsonApiInstitution import MonitorDrupalJsonApiInstitution
from import_api_app.resources.UpdateInstitution import UpdateInstitution
from import_api_app.resources.ClearCache import ClearCache
from import_api_app.resources.UpdateRecordSet import UpdateRecordSet
from import_api_app.resources.ImportProcessStart import ImportProcessStart
......@@ -101,7 +101,7 @@ def create_app(test_config=None):
# api.add_resource(JobReport, '/v1/job/<topic_name>/report')
api.add_resource(WriteJobResultToDrupal, '/v1/drupal/<job_drupal_uuid>/<job_log_drupal_uuid>')
api.add_resource(WriteTypeReportToDrupal, '/v1/drupal/WriteElementReport')
api.add_resource(MonitorDrupalJsonApiInstitution, '/v1/drupal/institution/<institutionId>')
api.add_resource(UpdateInstitution, '/v1/drupal/institution/<institutionId>')
api.add_resource(UpdateRecordSet, '/v1/drupal/recordset/<record_set_id>')
api.add_resource(
DeleteRecord,
......
......@@ -6,19 +6,17 @@ import traceback
from requests.auth import HTTPBasicAuth
class MonitorDrupalJsonApiInstitution(Resource):
class UpdateInstitution(Resource):
# Todo write/correct comment for swagger
def get(self, institutionId):
def get(self, institution_drupal_uuid):
"""
Get uuid of updated institution form drupal, gets more information form \
drupal and puts it into the dedicated kafka topic
Update the institution with the given drupal UUID in the backend.
---
tags:
- Monitor Drupal JSON API
- Update Institution
parameters:
- in: path
name: institutionId
name: institution_drupal_uuid
required: true
description: The UUID of the updated institution
example: 0c4c777c-94f8-45ba-945a-bfe6967d40da
......@@ -44,13 +42,15 @@ class MonitorDrupalJsonApiInstitution(Resource):
password = current_app.config['drupal-password']
auth = HTTPBasicAuth(user, password)
base_url = current_app.config['drupal-api-url']
path = f'/jsonapi/node/institution/{institutionId}'
de_url = f"{base_url}/de{path}"
fr_url = f"{base_url}/fr{path}"
it_url = f"{base_url}/it{path}"
response_de, status_de = self.request_institution_data(de_url, headers, auth)
response_fr, status_fr = self.request_institution_data(fr_url, headers, auth)
response_it, status_it = self.request_institution_data(it_url, headers, auth)
institution_path = f'/jsonapi/node/institution/{institution_drupal_uuid}'
extended_address_path = '/jsonapi/paragraph/extended_address/'
record_set_path = '/jsonapi/node/record_set'
de_url = f"{base_url}/de{institution_path}"
fr_url = f"{base_url}/fr{institution_path}"
it_url = f"{base_url}/it{institution_path}"
response_de, status_de = UpdateInstitution.request_institution_data(de_url, headers, auth)
response_fr, status_fr = UpdateInstitution.request_institution_data(fr_url, headers, auth)
response_it, status_it = UpdateInstitution.request_institution_data(it_url, headers, auth)
if status_de + status_fr + status_it != 600:
return {
'de': response_de,
......@@ -63,44 +63,36 @@ class MonitorDrupalJsonApiInstitution(Resource):
try:
addresses = []
drupalResponseAddressParagraphs = \
institution_data_de['relationships']['field_extended_address']
for addressElement in drupalResponseAddressParagraphs['data']:
drupalResponseAddress = \
requests.get(current_app.config['drupal-api-url'] + '/jsonapi/paragraph/' +
'extended_address/' + addressElement['id'],
headers=headers, auth=auth).json()['data']
fieldAdressess = drupalResponseAddress['attributes']['field_address']
if fieldAdressess is not None:
fieldAdressess['coordinates'] = \
drupalResponseAddress['attributes']['field_geographical_coordinates'][
'value']
addresses.append(fieldAdressess)
address_paragraphs = institution_data_de['relationships']['field_extended_address']
for element in address_paragraphs['data']:
paragraph_url = f'{base_url}{extended_address_path}{element["id"]}'
drupal_response_address = requests.get(paragraph_url, headers=headers, auth=auth)
response_data = drupal_response_address.json()['data']
addresses = response_data['attributes']['field_address']
if addresses is not None:
addresses['coordinates'] = \
response_data['attributes']['field_geographical_coordinates']['value']
addresses.append(addresses)
institutionTypes = []
institutionTypesParagraphsUri = \
institution_types = []
paragraph_url = \
institution_data_de['relationships']['field_institution_types']['links']['related'][
'href'] # noqa: E501
drupalResponseInstitutionTypesParagraphs = \
requests.get(institutionTypesParagraphsUri,
headers=headers, auth=auth).json()['data']
drupalResponseInstitutionTypes = drupalResponseInstitutionTypesParagraphs
for institutionTypeElement in drupalResponseInstitutionTypes:
institutionTypes. \
append(institutionTypeElement['attributes']['field_wikidata']['uri'])
institution_types_data = \
requests.get(paragraph_url, headers=headers, auth=auth).json()['data']
for element in institution_types_data:
institution_types.append(element['attributes']['field_wikidata']['uri'])
recordSetIds = []
drupalResponseRecordSets = \
requests.get(current_app.config['drupal-api-url'] + '/jsonapi/node/' +
'record_set?filter[field_institution.id][value]=' +
institutionId, headers=headers, auth=auth).json()['data']
for recordSet in drupalResponseRecordSets:
recordSetIds.append(
recordSet['attributes']['field_memobase_id']
record_set_ids = []
filter_param = f'?filter[field_institution.id][value]={institution_drupal_uuid}'
url = f'{base_url}{record_set_path}{filter_param}'
record_set_data = requests.get(url, headers=headers, auth=auth).json()['data']
for record_set in record_set_data:
record_set_ids.append(
record_set['attributes']['field_memobase_id']
)
except LookupError as ex:
msg = 'LookupError for ' + institutionId + ': ' + str(ex) + '\n' + \
traceback.format_exc() + '\n'
msg = f'Could not find key ({institution_drupal_uuid}): {ex}.'
current_app.logger.error(msg)
return {
'status': 'FAILURE',
......@@ -108,8 +100,7 @@ class MonitorDrupalJsonApiInstitution(Resource):
'topic_value': topic_value
}, 500
except Exception as ex:
msg = 'Exception for ' + institutionId + ': ' + str(ex) + '\n' + \
traceback.format_exc() + '\n'
msg = f'Unknown Exception ({institution_drupal_uuid}): {ex}\n{traceback.format_exc()}'
current_app.logger.error(msg)
return {
'status': 'FAILURE',
......@@ -134,9 +125,9 @@ class MonitorDrupalJsonApiInstitution(Resource):
'field_text_de': institution_data_de['attributes']['field_text'],
'field_text_fr': institution_data_fr['attributes']['field_text'],
'field_text_it': institution_data_it['attributes']['field_text'],
'field_institution_types': institutionTypes,
'field_institution_types': institution_types,
'field_teaser_color': institution_data_de['attributes']['field_teaser_color'],
'recordset_ids': recordSetIds,
'recordset_ids': record_set_ids,
'computed_teaser_image_url':
institution_data_de['attributes']['computed_teaser_image_url'],
'computed_teaser_color': institution_data_de['attributes']['computed_teaser_color'],
......@@ -150,7 +141,7 @@ class MonitorDrupalJsonApiInstitution(Resource):
key = bytes(topic_value.get('field_memobase_id'), encoding='utf-8')
producer.send(current_app.config['topic-drupal-export'], topic_value, key)
except Exception as ex:
msg = 'Exception for ' + institutionId + ': ' + str(ex) + '\n' + \
msg = 'Exception for ' + institution_drupal_uuid + ': ' + str(ex) + '\n' + \
traceback.format_exc()
current_app.logger.error(msg)
return {
......@@ -159,14 +150,15 @@ class MonitorDrupalJsonApiInstitution(Resource):
'topic_value': topic_value
}, 500
current_app.logger.debug('success for ' + institutionId)
current_app.logger.debug('success for ' + institution_drupal_uuid)
return {
'status': 'SUCCESS',
'topic_key': topic_value.get('field_memobase_id'),
'topic_value': topic_value
}, 200
def request_institution_data(self, url, headers, auth):
@staticmethod
def request_institution_data(url, headers, auth):
response = requests.get(url, headers=headers, auth=auth)
if response.ok:
try:
......
......@@ -17,11 +17,9 @@ class UpdateRecordSet(Resource):
value_serializer=lambda m: json.dumps(m, ensure_ascii=False)
.encode('utf-8'))
# Todo write/correct comment for swagger
def get(self, record_set_id):
"""
Get uuid of updated recordset form drupal, gets more information form \
drupal and puts it into the dedicated kafka topic
Update the record set with the given drupal UUID in the backend.
---
tags:
- Import Record Set
......
Markdown is supported
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