Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
memoriav
Memobase 2020
services
Import Process
Import API
Commits
86fc95ff
Commit
86fc95ff
authored
Apr 09, 2021
by
Jonas Waeber
Browse files
Add note request to type report writer
parent
f0a61a7f
Pipeline
#24370
passed with stages
in 2 minutes and 36 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
import_api_app/resources/WriteTypeReportToDrupal.py
View file @
86fc95ff
...
...
@@ -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:
item
s
:
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: T
he 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 t
he 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'
]
id
entifier
=
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=='
}
drupal
T
ype
=
''
url
=
current_app
.
config
[
'
drupal-api-url
'
]
+
'
/jsonapi/node/'
if
type
==
'institution'
:
drupal
T
ype
=
'node--institution'
drupal
_t
ype
=
''
url
=
f
'
{
current_app
.
config
[
"
drupal-api-url
"
]
}
/jsonapi/node/
{
identifier
}
'
if
node_
type
==
'institution'
:
drupal
_t
ype
=
'node--institution'
url
+=
'institution/'
elif
type
==
'recordset'
:
drupal
T
ype
=
'node--record_set'
elif
node_
type
==
'recordset'
:
drupal
_t
ype
=
'node--record_set'
url
+=
'record_set/'
url
+=
id
url
+=
id
entifier
data
=
{
"data"
:
{
"id"
:
id
,
"type"
:
drupal
T
ype
,
"id"
:
id
entifier
,
"type"
:
drupal
_t
ype
,
"attributes"
:
{
"field_migrated"
:
status
,
"field_error"
:
report
...
...
@@ -86,7 +114,7 @@ def write_results(type, id, status, report):
}
}
re
turnVal
=
{
'message'
:
''
}
re
sult
=
{
'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
)
re
turnVal
[
'message'
]
+=
'Updated: '
+
url
+
'
\n
'
re
sult
[
'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
re
turnVal
return
re
sult
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment