forked from UtrechtUniversity/yoda-ruleset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_landing_page.py
240 lines (200 loc) · 7.85 KB
/
json_landing_page.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# -*- coding: utf-8 -*-
"""Functions for transforming JSON to landingpage HTML."""
__copyright__ = 'Copyright (c) 2019, Utrecht University'
__license__ = 'GPLv3, see LICENSE'
import jinja2
from util import *
def json_landing_page_create_json_landing_page(callback, rodsZone, template_name, combiJsonPath, json_schema):
"""Get the landing page of published YoDa metadata as a string.
:param callback: Callback to rule Language
:param rodsZone: Zone name
:param template_name: Name of landingpage template
:param combiJsonPath: path to Yoda metadata JSON
:param json_schema: Dict holding entire contents of metadata.json for the category involved
:return: Output HTML landing page
"""
# Landing page creation is part of the publication process
# Read user & system metadata from corresponding combi JSON file
# (Python2) 'want_bytes=False': Do not encode embedded unicode strings as
# UTF-8, as that will trip up jinja2.
dictJsonData = jsonutil.read(callback, combiJsonPath, want_bytes=False)
# Remove empty lists, empty dicts, or None elements
# to prevent empty fields on landingpage.
dictJsonData = jsonutil.remove_empty(dictJsonData)
# Load the Jinja template.
landingpage_template_path = '/' + rodsZone + '/yoda/templates/' + template_name
template = data_object.read(callback, landingpage_template_path)
# Enable autoescaping for all templates.
# NOTE: autoescape is no longer an extension starting in jinja 2.9 (2017).
Template = jinja2.Environment(autoescape=True,
extensions=['jinja2.ext.autoescape']).from_string
# Pre work input for render process.
# When empty landing page, take a short cut
if template_name == 'emptylandingpage.html.j2':
persistent_identifier_datapackage = dictJsonData['System']['Persistent_Identifier_Datapackage']
tm = Template(template)
landing_page = tm.render(persistent_identifier_datapackage=persistent_identifier_datapackage)
return landing_page
# Gather all metadata.
title = dictJsonData['Title']
description = dictJsonData['Description']
# Geo specific lab handling
try:
labids = dictJsonData['Lab']
labs = []
schema_labids = json_schema['definitions']['optionsLabs']['enum']
schema_labnames = json_schema['definitions']['optionsLabs']['enumNames']
for id in labids:
index = schema_labids.index(id)
labs.append(schema_labnames[index])
except KeyError:
labs = []
# Geo specific additional lab handling
try:
additional_labs = dictJsonData['Additional_Lab'] # niet verplicht
except KeyError:
additional_labs = []
try:
discipline_ids = dictJsonData['Discipline']
disciplines = []
schema_disc_ids = json_schema['definitions']['optionsDiscipline']['enum']
schema_disc_names = json_schema['definitions']['optionsDiscipline']['enumNames']
for id in discipline_ids:
index = schema_disc_ids.index(id)
disciplines.append(schema_disc_names[index])
except KeyError:
disciplines = []
try:
version = dictJsonData['Version']
except KeyError:
version = ''
try:
language = ''
language_id = dictJsonData['Language']
schema_lang_ids = json_schema['definitions']['optionsLanguage']['enum']
schema_lang_names = json_schema['definitions']['optionsLanguage']['enumNames']
index = schema_lang_ids.index(language_id)
language = schema_lang_names[index]
except KeyError:
language = ''
try:
datatype = ''
datatype_id = dictJsonData['Data_Type']
schema_dt_ids = json_schema['definitions']['optionsDataType']['enum']
schema_dt_names = json_schema['definitions']['optionsDataType']['enumNames']
index = schema_dt_ids.index(datatype_id)
datatype = schema_dt_names[index]
except KeyError:
datatype = ''
try:
covered_geolocation_place = dictJsonData['Covered_Geolocation_Place']
except KeyError:
covered_geolocation_place = {}
try:
tags = dictJsonData['Tag'] # not mandatory
except KeyError:
tags = []
try:
apparatus = dictJsonData['Apparatus']
except KeyError:
apparatus = []
try:
main_setting = dictJsonData['Main_Setting']
except KeyError:
main_setting = []
try:
process_hazard = dictJsonData['Process_Hazard']
except KeyError:
process_hazard = []
try:
geological_structure = dictJsonData['Geological_Structure']
except KeyError:
geological_structure = []
try:
geomorphical_feature = dictJsonData['Geomorphical_Feature']
except KeyError:
geomorphical_feature = []
try:
material = dictJsonData['Material']
except KeyError:
material = []
try:
monitoring = dictJsonData['Monitoring']
except KeyError:
monitoring = []
try:
software = dictJsonData['Software']
except KeyError:
software = []
try:
measured_property = dictJsonData['Measured_Property']
except KeyError:
measured_property = []
# Route all domain specific keywords to tag area of landingpage
all_taggebles = (tags + apparatus + main_setting + process_hazard + geological_structure
+ geomorphical_feature + material + monitoring + software + measured_property)
try:
related_datapackages = dictJsonData['Related_Datapackage'] # not mandatory
except KeyError:
related_datapackages = []
try:
creators = dictJsonData['Creator']
except KeyError:
creators = []
try:
contributors = dictJsonData['Contributor']
except KeyError:
contributors = []
try:
contacts = dictJsonData['Contact']
except KeyError:
contacts = []
try:
funding_reference = dictJsonData['Funding_Reference']
except KeyError:
funding_reference = []
license = dictJsonData['License']
data_access_restriction = dictJsonData['Data_Access_Restriction']
data_classification = dictJsonData['Data_Classification']
last_modified_date = dictJsonData['System']['Last_Modified_Date']
persistent_identifier_datapackage = dictJsonData['System']['Persistent_Identifier_Datapackage']
publication_date = dictJsonData['System']['Publication_Date']
open_access_link = dictJsonData['System']['Open_access_Link']
license_uri = dictJsonData['System']['License_URI']
try:
geolocations = dictJsonData['GeoLocation']
except KeyError:
geolocations = {}
# Collection name ILAB specific - part of default schemas
try:
collection_name = dictJsonData['Collection_Name']
except KeyError:
collection_name = ''
tm = Template(template)
landing_page = tm.render(
title=title,
description=description,
datatype=datatype,
labs=labs,
additional_labs=additional_labs,
disciplines=disciplines,
version=version,
language=language,
tags=all_taggebles,
creators=creators,
contributors=contributors,
contacts=contacts,
publication_date=publication_date,
data_access_restriction=data_access_restriction,
license=license,
license_uri=license_uri,
open_access_link=open_access_link,
funding_reference=funding_reference,
data_classification=data_classification,
collection_name=collection_name,
last_modified_date=last_modified_date,
related_datapackages=related_datapackages,
persistent_identifier_datapackage=persistent_identifier_datapackage,
geolocations=geolocations,
covered_geolocation_place=covered_geolocation_place)
return landing_page