WriteTypeReportToDrupal.py 5.68 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
from flask_restful import Resource, request
from flask_restful import current_app
from helpers.Error import ImportApiError
import requests
import json


class WriteTypeReportToDrupal(Resource):
    def post(self):
        """
        Write a report for an recordset or institution Drupal
        ---
        tags:
Matthias's avatar
Matthias committed
14
          - reporting
15
        parameters:
Matthias's avatar
Matthias committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
            - 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.
39
        responses:
Matthias's avatar
Matthias committed
40
41
42
43
44
45
            200:
                description: It was successful
            404:
                description: No content with such a id found
            500:
                description: There was a problem
46
        """
Matthias's avatar
Matthias committed
47

48
        try:
Jonas Waeber's avatar
Jonas Waeber committed
49
50
51
52
            node_type = request.json['node_type']
            identifier = request.json['id']
            status = request.json['status']
            report = request.json['report']
53
54
55
56
57
        except KeyError as er:
            return {
                       'message': f'Missing a element in input body: {er}'
                   }, 400
        try:
Jonas Waeber's avatar
Jonas Waeber committed
58
            node_id = get_node_id(identifier, node_type)
59
            return write_results(node_type, node_id, status, report)
60
        except ImportApiError as e:
61
62
63
64
65
66
67
            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


Jonas Waeber's avatar
Jonas Waeber committed
68
def get_node_id(memobase_id: str, node_type: str):
69
70
71
72
73
74
75
76
    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=='
    }
Jonas Waeber's avatar
Jonas Waeber committed
77
78
    url = f'{current_app.config["drupal-api-url"]}/jsonapi/node/{node_type}?filter['\
          f'field_memobase_id]={memobase_id}'
79
80
81

    response = requests.get(url, headers=headers)
    if response.ok:
Jonas Waeber's avatar
Jonas Waeber committed
82
        try:
Jonas Waeber's avatar
Jonas Waeber committed
83
            return response.json()['data'][0]['attributes']['drupal_internal__nid']
Jonas Waeber's avatar
Jonas Waeber committed
84
85
        except KeyError as er:
            current_app.logger.error(f"Could not find the key {er} in data: {response.text}.")
Jonas Waeber's avatar
Jonas Waeber committed
86
87
            raise ImportApiError(f"Could not find the key {er} in data (url={url}): "
                                 f"{response.text}.")
Jonas Waeber's avatar
Jonas Waeber committed
88
        except IndexError:
Jonas Waeber's avatar
Jonas Waeber committed
89
90
91
92
            current_app.logger.error(f"Data field does not contain a list (url={url}): "
                                     f"{response.text}.")
            raise ImportApiError(f"Data field does not contain a list (url={url}): "
                                 f"{response.text}.")
93
94
    else:
        raise ImportApiError(f"Could not find element for report: {response.text}.")
95
96


97
98
99
def write_results(node_type, identifier, status, report):
    current_app.logger.debug(
        "writing: " + node_type + "/" + identifier + "/" + str(status) + "/" + report)
100
101
102
103
104
105
106
    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=='
    }

107
108
109
110
    drupal_type = ''
    url = f'{current_app.config["drupal-api-url"]}/jsonapi/node/{identifier}'
    if node_type == 'institution':
        drupal_type = 'node--institution'
111
        url += 'institution/'
112
113
    elif node_type == 'recordset':
        drupal_type = 'node--record_set'
114
        url += 'record_set/'
115
    url += identifier
116
117
118

    data = {
        "data": {
119
120
            "id": identifier,
            "type": drupal_type,
121
122
123
124
125
126
127
            "attributes": {
                "field_migrated": status,
                "field_error": report
            }
        }
    }

128
    result = {'message': ''}
129
130
131
132
133
134
135
136
137
138
139
140

    try:
        response = requests.patch(
            url,
            headers=headers,
            data=json.dumps(data)
        )
    except requests.exceptions.RequestException:
        message = "It was not possible to write to Drupal API \
                  via the following url " + url
        current_app.logger.error(message)
        raise ImportApiError(message)
141
    if response.ok:
142
        current_app.logger.debug('Updated: ' + url)
143
        result['message'] += 'Updated: ' + url + '\n'
144
145
146
147
148
149
150
151
152
153
154
155
    elif response.status_code == 403:
        message = "Not authorized to write to: " + url
        current_app.logger.error(message)
        raise ImportApiError(message)
    elif response.status_code == 404:
        message = 'Not Found: ' + url
        current_app.logger.error(message)
        raise ImportApiError(message)
    else:
        message = "Unknown response status code for drupal api for url " + url
        current_app.logger.error(message)
        raise ImportApiError(message)
156
    return result