-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathdataset_plugin.py
More file actions
302 lines (267 loc) · 11.7 KB
/
dataset_plugin.py
File metadata and controls
302 lines (267 loc) · 11.7 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import re
import json
from flask import has_request_context, Blueprint
from urllib.parse import urlsplit
import logging
from typing import Optional, List, Dict, cast
from flask.typing import BeforeRequestCallable
from ckan.types import Context, Response, Any
import ckan.plugins as p
from ckan.plugins.toolkit import g, h, request
from ckanext.datastore.interfaces import IDataDictionaryForm
from ckanext.scheming.plugins import SchemingDatasetsPlugin
from ckanext.canada.helpers import RELEASE_DATE_FACET_STEP
from ckanext.canada.view import (
CanadaDatasetEditView,
CanadaDatasetEditPageView,
CanadaDatasetCreateView,
CanadaDatasetCreatePageView,
CanadaResourceEditView,
CanadaResourceCreateView,
canada_search,
canada_prevent_pd_views,
_get_package_type_from_dict
)
log = logging.getLogger(__name__)
fq_portal_release_date_match = re.compile(r"(portal_release_date:\"\[.*\]\")")
class CanadaDatasetsPlugin(SchemingDatasetsPlugin):
"""
Plugin for dataset and resource
"""
p.implements(p.IDatasetForm, inherit=True)
p.implements(p.IPackageController, inherit=True)
p.implements(p.IBlueprint)
p.implements(IDataDictionaryForm, inherit=True)
try:
from ckanext.validation.interfaces import IDataValidation
except ImportError:
log.warning('failed to import ckanext-validation interface')
else:
p.implements(IDataValidation, inherit=True)
# IBlueprint
def get_blueprint(self) -> List[Blueprint]:
"""
Prevents all Core Dataset and Resources Views
for all the PD types. Will type_redirect them
to the pd_type. Will allow /<pd_type>/activity
"""
blueprints = []
for pd_type in h.recombinant_get_types():
blueprint = Blueprint(
'canada_%s' % pd_type,
__name__,
url_prefix='/%s' % pd_type,
url_defaults={'package_type': pd_type})
blueprint.add_url_rule(
'/',
endpoint='canada_search_%s' % pd_type,
view_func=canada_search,
methods=['GET']
)
blueprint.add_url_rule(
'/<path:uri>',
endpoint='canada_prevent_%s' % pd_type,
view_func=canada_prevent_pd_views,
methods=['GET', 'POST']
)
blueprints.append(blueprint)
return blueprints
# type_ignore_reason: incomplete typing
def _redirect_pd_dataset_endpoints(blueprint: Blueprint # type: ignore
) -> Optional[Response]:
"""
Runs before request for /dataset and /dataset/<pkg id>/resource
Checks if the actual package type is a PD type and redirects it.
"""
if has_request_context() and hasattr(request, 'view_args'):
if not request.view_args:
return
id = request.view_args.get('id')
if not id:
return
package_type = request.view_args.get('package_type')
package_type = _get_package_type_from_dict(id, package_type)
if package_type in h.recombinant_get_types():
return h.redirect_to('canada.type_redirect',
resource_name=package_type)
# IDatasetForm
def prepare_dataset_blueprint(self, package_type: str,
blueprint: Blueprint) -> Blueprint:
blueprint.add_url_rule(
'/edit/<id>',
endpoint='canada_edit_%s' % package_type,
view_func=CanadaDatasetEditView.as_view(str('edit')),
methods=['GET', 'POST']
)
blueprint.add_url_rule(
'/new',
endpoint='canada_new_%s' % package_type,
view_func=CanadaDatasetCreateView.as_view(str('new')),
methods=['GET', 'POST']
)
blueprint.add_url_rule(
'/',
endpoint='canada_search_%s' % package_type,
view_func=canada_search,
methods=['GET'],
strict_slashes=False
)
blueprint.add_url_rule(
'/new/<id>/<page>',
'scheming_new_page',
CanadaDatasetCreatePageView.as_view('new_page'),
)
blueprint.add_url_rule(
'/edit/<id>/<page>',
'scheming_edit_page',
CanadaDatasetEditPageView.as_view('edit_page'),
)
# redirect PD endpoints accessed from /dataset/<pd pkg id>
blueprint.before_request(cast(BeforeRequestCallable,
self._redirect_pd_dataset_endpoints))
return blueprint
# IDatasetForm
def prepare_resource_blueprint(self, package_type: str,
blueprint: Blueprint) -> Blueprint:
blueprint.add_url_rule(
'/<resource_id>/edit',
endpoint='canada_resource_edit_%s' % package_type,
view_func=CanadaResourceEditView.as_view(str('edit')),
methods=['GET', 'POST']
)
blueprint.add_url_rule(
'/new',
endpoint='canada_resource_new_%s' % package_type,
view_func=CanadaResourceCreateView.as_view(str('new')),
methods=['GET', 'POST']
)
# redirect PD endpoints accessed from /dataset/<pd pkg id>/resource
blueprint.before_request(cast(BeforeRequestCallable,
self._redirect_pd_dataset_endpoints))
return blueprint
# IDataValidation
def can_validate(self, context: Context, resource: Dict[str, Any]):
"""
Only uploaded resources are allowed to be
validated, or allowed domain sources.
"""
if resource.get('url_type') == 'upload':
return True
if not resource.get('url_type'):
allowed_domains = p.toolkit.config.get(
'ckanext.canada.datastore_source_domain_allow_list', [])
url = resource.get('url')
url_parts = urlsplit(url)
if url_parts.netloc in allowed_domains:
return True
return False
# IPackageController
def before_dataset_search(self, search_params: Dict[str, Any]) -> Dict[str, Any]:
# We're going to group portal_release_date into two bins - to today and
# after today.
search_params['facet.range'] = 'portal_release_date'
search_params['facet.range.start'] = 'NOW/DAY-%sYEARS' % \
RELEASE_DATE_FACET_STEP
search_params['facet.range.end'] = 'NOW/DAY+%sYEARS' % \
RELEASE_DATE_FACET_STEP
search_params['facet.range.gap'] = '+%sYEARS' % \
RELEASE_DATE_FACET_STEP
# FIXME: so terrible. hack out WET4 wbdisable parameter
try:
search_params['fq'] = search_params['fq'].replace(
'wbdisable:"true"', '').replace(
'wbdisable:"false"', '')
except Exception:
pass
try:
g.fields_grouped.pop('wbdisable', None)
except Exception:
pass
# search extras for ckan-admin/publish route.
# we only want to show ready to publish,
# approved datasets without a release date.
if has_request_context() and 'ckan-admin/publish' in request.url:
search_params['extras']['ready_to_publish'] = 'true'
search_params['extras']['imso_approval'] = 'true'
search_params['fq'] += '+ready_to_publish:"true", '\
'+imso_approval:"true", -portal_release_date:*'
# CKAN Core search view wraps all fq values with double quotes.
# We need to remove double quotes from the portal_release_date queries.
if 'fq' in search_params:
for release_date_query in re.findall(fq_portal_release_date_match,
search_params['fq']):
search_params['fq'] = search_params['fq'].replace(
release_date_query, release_date_query.replace('"', ''))
return search_params
# IPackageController
def after_dataset_search(self, search_results: Dict[str, Any],
search_params: Dict[str, Any]) -> Dict[str, Any]:
for result in search_results.get('results', []):
for extra in result.get('extras', []):
if extra.get('key') in ['title_fra', 'notes_fra']:
result[extra['key']] = extra['value']
return search_results
# IPackageController
def before_dataset_index(self, data_dict: Dict[str, Any]) -> Dict[str, Any]:
kw = json.loads(data_dict.get('extras_keywords', '{}'))
data_dict['keywords'] = kw.get('en', [])
data_dict['keywords_fra'] = kw.get('fr', kw.get('fr-t-en', []))
data_dict['catalog_type'] = data_dict.get('type', '')
data_dict['subject'] = json.loads(data_dict.get('subject', '[]'))
data_dict['topic_category'] = json.loads(data_dict.get(
'topic_category', '[]'))
data_dict['spatial_representation_type'] = []
if (
data_dict.get('spatial_representation_type') and
isinstance(data_dict.get('spatial_representation_type'), str)):
rep_type: str = data_dict.get('spatial_representation_type', '')
try:
data_dict['spatial_representation_type'] = json.loads(rep_type)
except (TypeError, ValueError):
data_dict['spatial_representation_type'] = []
if data_dict.get('portal_release_date'):
data_dict.pop('ready_to_publish', None)
elif data_dict.get('ready_to_publish') == 'true':
data_dict['ready_to_publish'] = 'true'
else:
data_dict['ready_to_publish'] = 'false'
try:
geno = h.recombinant_get_geno(data_dict['type']) or {}
except AttributeError:
pass
else:
data_dict['portal_type'] = geno.get('portal_type', data_dict['type'])
if 'collection' in geno:
data_dict['collection'] = geno['collection']
# need to keep fgp_viewer in the index for Advanced Search App
if 'fgp_viewer' in data_dict.get('display_flags', []):
data_dict['fgp_viewer'] = 'map_view'
titles = json.loads(data_dict.get('title_translated', '{}'))
data_dict['title_fr'] = titles.get('fr', '')
data_dict['title_string'] = titles.get('en', '')
if data_dict['type'] == 'prop':
status = data_dict.get('status')
data_dict['status'] = status[-1]['reason'] if \
status else 'department_contacted'
if data_dict.get('credit'):
for i, cr in enumerate(data_dict['credit']):
cr.pop('__extras', None)
# credit is a string multiValue in SOLR,
# need to json stringify for SOLR 9+
data_dict['credit'][i] = json.dumps(cr)
if data_dict.get('relationship'):
data_dict['related_relationship'] = [rel['related_relationship'] for
rel in data_dict['relationship']]
data_dict['related_type'] = [rel['related_type'] for
rel in data_dict['relationship']]
data_dict.pop('relationship', None)
return data_dict
# IDataDictionaryForm
def update_datastore_info_field(self, field: Dict[str, Any],
plugin_data: Dict[str, Any]) -> Dict[str, Any]:
if 'info' or '_info' in plugin_data and 'info' not in field:
if 'info' in plugin_data:
field['info'] = plugin_data.get('info', {})
elif '_info' in plugin_data:
field['info'] = plugin_data.get('_info', {})
return field