Drupal.py 3.67 KB
Newer Older
Lionel Walter's avatar
Lionel Walter committed
1
2
from import_api_app.models.Error import ImportApiError
from flask_restful import current_app
3
from datetime import datetime
Lionel Walter's avatar
Lionel Walter committed
4
5
6
7
8
import requests
import json


def write_results(job_log_drupal_uuid, report):
Lionel Walter's avatar
Lionel Walter committed
9
10
    current_app.logger.debug("uuid to write: " + job_log_drupal_uuid)
    current_app.logger.debug("report to write: " + report)
Lionel Walter's avatar
Lionel Walter committed
11
12
13
14
15
16
17
18
19
20
21
    headers = {
        'Content-Type': 'application/vnd.api+json',
        'Accept': 'application/vnd.api+json',
        'X-API-Key': current_app.config['drupal-api-key']
    }

    data = {
        "data": {
            "id": job_log_drupal_uuid,
            "type": "paragraph--job_log_result",
            "attributes": {
22
23
                "field_summary": report,
                "field_end_date": datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S+00:00")
Lionel Walter's avatar
Lionel Walter committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
            }
        }
    }

    url = current_app.config['DRUPAL_API_URL'] + \
        '/paragraph/job_log_result/' + job_log_drupal_uuid

    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)
    if response.status_code == 200:
Lionel Walter's avatar
Lionel Walter committed
43
        current_app.logger.error('Updated: '+url)
Lionel Walter's avatar
Lionel Walter committed
44
45
46
47
48
49
50
51
52
53
54
55
56
        return {'message': 'Updated: ' + url}
    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)
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109


# make the job as not active any more and write the end data
# todo refactor this in a more generic function
def update_status(job_drupal_uuid, job_name):
    # in drupal job names have underscore instead of minus
    job_name = job_name.replace("-", "_")
    current_app.logger.debug("uuid to write: " + job_drupal_uuid)
    headers = {
        'Content-Type': 'application/vnd.api+json',
        'Accept': 'application/vnd.api+json',
        'X-API-Key': current_app.config['drupal-api-key']
    }

    data = {
        "data": {
            "id": job_drupal_uuid,
            "type": "paragraph--job_"+job_name,
            "attributes": {
                "field_is_active": False
            }
        }
    }

    url = current_app.config['DRUPAL_API_URL'] + \
        '/paragraph/job_'+job_name+'/' + job_drupal_uuid

    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)
    if response.status_code == 200:
        current_app.logger.error('Updated: '+url)
        return {'message': 'Updated: ' + url}
    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)