forked from ckan/ckanext-dcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
168 lines (119 loc) · 4.48 KB
/
__init__.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
# -*- coding: utf-8 -*-
from builtins import object
import os
from ckantoolkit import config
from ckan import plugins as p
from ckan.lib.plugins import DefaultTranslation
import ckanext.dcat.blueprints as blueprints
import ckanext.dcat.cli as cli
from ckanext.dcat.logic import (dcat_dataset_show,
dcat_catalog_show,
dcat_catalog_search,
dcat_datasets_list,
dcat_auth,
)
from ckanext.dcat import utils
CUSTOM_ENDPOINT_CONFIG = 'ckanext.dcat.catalog_endpoint'
TRANSLATE_KEYS_CONFIG = 'ckanext.dcat.translate_keys'
HERE = os.path.abspath(os.path.dirname(__file__))
I18N_DIR = os.path.join(HERE, u"../i18n")
class DCATPlugin(p.SingletonPlugin, DefaultTranslation):
p.implements(p.IConfigurer, inherit=True)
p.implements(p.ITemplateHelpers, inherit=True)
p.implements(p.IActions, inherit=True)
p.implements(p.IAuthFunctions, inherit=True)
p.implements(p.IPackageController, inherit=True)
p.implements(p.ITranslation, inherit=True)
p.implements(p.IClick)
p.implements(p.IBlueprint)
# IClick
def get_commands(self):
return cli.get_commands()
# IBlueprint
def get_blueprint(self):
return [blueprints.dcat]
# ITranslation
def i18n_directory(self):
return I18N_DIR
# IConfigurer
def update_config(self, config):
p.toolkit.add_template_directory(config, '../templates')
# Check catalog URI on startup to emit a warning if necessary
utils.catalog_uri()
# Check custom catalog endpoint
custom_endpoint = config.get(CUSTOM_ENDPOINT_CONFIG)
if custom_endpoint:
if not custom_endpoint[:1] == '/':
raise Exception(
'"{0}" should start with a backslash (/)'.format(
CUSTOM_ENDPOINT_CONFIG))
if '{_format}' not in custom_endpoint:
raise Exception(
'"{0}" should contain {{_format}}'.format(
CUSTOM_ENDPOINT_CONFIG))
# ITemplateHelpers
def get_helpers(self):
return {
'helper_available': utils.helper_available,
'dcat_get_endpoint': utils.get_endpoint,
}
# IActions
def get_actions(self):
return {
'dcat_dataset_show': dcat_dataset_show,
'dcat_catalog_show': dcat_catalog_show,
'dcat_catalog_search': dcat_catalog_search,
}
# IAuthFunctions
def get_auth_functions(self):
return {
'dcat_dataset_show': dcat_auth,
'dcat_catalog_show': dcat_auth,
'dcat_catalog_search': dcat_auth,
}
# IPackageController
# CKAN < 2.10 hooks
def after_show(self, context, data_dict):
return self.after_dataset_show(context, data_dict)
# CKAN >= 2.10 hooks
def after_dataset_show(self, context, data_dict):
# check if config is enabled to translate keys (default: True)
if not p.toolkit.asbool(config.get(TRANSLATE_KEYS_CONFIG, True)):
return data_dict
if context.get('for_view'):
field_labels = utils.field_labels()
def set_titles(object_dict):
for key, value in object_dict.copy().items():
if key in field_labels:
object_dict[field_labels[key]] = object_dict[key]
del object_dict[key]
for resource in data_dict.get('resources', []):
set_titles(resource)
for extra in data_dict.get('extras', []):
if extra['key'] in field_labels:
extra['key'] = field_labels[extra['key']]
return data_dict
class DCATJSONInterface(p.SingletonPlugin):
p.implements(p.IActions)
p.implements(p.IAuthFunctions, inherit=True)
p.implements(p.IBlueprint)
# IBlueprint
def get_blueprint(self):
return [blueprints.dcat_json_interface]
# IActions
def get_actions(self):
return {
'dcat_datasets_list': dcat_datasets_list,
}
# IAuthFunctions
def get_auth_functions(self):
return {
'dcat_datasets_list': dcat_auth,
}
class StructuredDataPlugin(p.SingletonPlugin):
p.implements(p.ITemplateHelpers, inherit=True)
# ITemplateHelpers
def get_helpers(self):
return {
'structured_data': utils.structured_data,
}