ckanext-data-qld 3.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ckanext/data_qld/__init__.py +0 -0
- ckanext/data_qld/actions.py +409 -0
- ckanext/data_qld/auth_functions.py +25 -0
- ckanext/data_qld/blueprints.py +19 -0
- ckanext/data_qld/click_cli.py +50 -0
- ckanext/data_qld/command_functions.py +156 -0
- ckanext/data_qld/commands.py +39 -0
- ckanext/data_qld/constants.py +11 -0
- ckanext/data_qld/controller.py +14 -0
- ckanext/data_qld/controller_functions.py +78 -0
- ckanext/data_qld/converters.py +72 -0
- ckanext/data_qld/datarequest_auth_functions.py +68 -0
- ckanext/data_qld/dataset_deletion/__init__.py +0 -0
- ckanext/data_qld/dataset_deletion/helpers.py +43 -0
- ckanext/data_qld/de_identified_data/__init__.py +0 -0
- ckanext/data_qld/de_identified_data/helpers.py +41 -0
- ckanext/data_qld/flask_plugin.py +22 -0
- ckanext/data_qld/google_analytics/__init__.py +11 -0
- ckanext/data_qld/google_analytics/controller.py +75 -0
- ckanext/data_qld/google_analytics/plugin.py +89 -0
- ckanext/data_qld/helpers.py +428 -0
- ckanext/data_qld/plugin.py +260 -0
- ckanext/data_qld/pylons_plugin.py +36 -0
- ckanext/data_qld/reporting/__init__.py +0 -0
- ckanext/data_qld/reporting/blueprints.py +17 -0
- ckanext/data_qld/reporting/constants.py +10 -0
- ckanext/data_qld/reporting/controller.py +20 -0
- ckanext/data_qld/reporting/controller_functions.py +378 -0
- ckanext/data_qld/reporting/helpers/__init__.py +0 -0
- ckanext/data_qld/reporting/helpers/export_helpers.py +145 -0
- ckanext/data_qld/reporting/helpers/helpers.py +282 -0
- ckanext/data_qld/reporting/logic/__init__.py +0 -0
- ckanext/data_qld/reporting/logic/action/__init__.py +0 -0
- ckanext/data_qld/reporting/logic/action/get.py +679 -0
- ckanext/data_qld/resource_freshness/__init__.py +0 -0
- ckanext/data_qld/resource_freshness/commands.py +37 -0
- ckanext/data_qld/resource_freshness/helpers/__init__.py +0 -0
- ckanext/data_qld/resource_freshness/helpers/helpers.py +180 -0
- ckanext/data_qld/resource_freshness/logic/__init__.py +0 -0
- ckanext/data_qld/resource_freshness/logic/actions/__init__.py +0 -0
- ckanext/data_qld/resource_freshness/logic/actions/get.py +53 -0
- ckanext/data_qld/resource_freshness/validation.py +160 -0
- ckanext/data_qld/resource_visibility/__init__.py +0 -0
- ckanext/data_qld/resource_visibility/helpers.py +116 -0
- ckanext/data_qld/resource_visibility/validators.py +54 -0
- ckanext/data_qld/tests/__init__.py +0 -0
- ckanext/data_qld/tests/test_plugin.py +5 -0
- ckanext/data_qld/validation.py +36 -0
- ckanext_data_qld-3.0.1-py3.9-nspkg.pth +1 -0
- ckanext_data_qld-3.0.1.dist-info/LICENSE +661 -0
- ckanext_data_qld-3.0.1.dist-info/METADATA +14 -0
- ckanext_data_qld-3.0.1.dist-info/RECORD +56 -0
- ckanext_data_qld-3.0.1.dist-info/WHEEL +5 -0
- ckanext_data_qld-3.0.1.dist-info/entry_points.txt +16 -0
- ckanext_data_qld-3.0.1.dist-info/namespace_packages.txt +1 -0
- ckanext_data_qld-3.0.1.dist-info/top_level.txt +1 -0
|
File without changes
|
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
import datetime
|
|
4
|
+
import logging
|
|
5
|
+
|
|
6
|
+
from ckan import model
|
|
7
|
+
from ckan.lib import base, helpers, mailer
|
|
8
|
+
import ckantoolkit as tk
|
|
9
|
+
from ckantoolkit import config
|
|
10
|
+
|
|
11
|
+
from ckanext.datarequests import db, validator
|
|
12
|
+
|
|
13
|
+
import constants
|
|
14
|
+
|
|
15
|
+
log = logging.getLogger(__name__)
|
|
16
|
+
|
|
17
|
+
# Avoid user_show lag
|
|
18
|
+
USERS_CACHE = {}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _get_user(user_id):
|
|
22
|
+
try:
|
|
23
|
+
if user_id in USERS_CACHE:
|
|
24
|
+
return USERS_CACHE[user_id]
|
|
25
|
+
else:
|
|
26
|
+
user = tk.get_action('user_show')({'ignore_auth': True}, {'id': user_id})
|
|
27
|
+
USERS_CACHE[user_id] = user
|
|
28
|
+
return user
|
|
29
|
+
except Exception as e:
|
|
30
|
+
log.warn(e)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _get_organization(organization_id):
|
|
34
|
+
try:
|
|
35
|
+
organization_show = tk.get_action('organization_show')
|
|
36
|
+
return organization_show({'ignore_auth': True}, {'id': organization_id, 'include_users': True})
|
|
37
|
+
except Exception as e:
|
|
38
|
+
log.warn(e)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _get_package(package_id):
|
|
42
|
+
try:
|
|
43
|
+
package_show = tk.get_action('package_show')
|
|
44
|
+
return package_show({'ignore_auth': True}, {'id': package_id})
|
|
45
|
+
except Exception as e:
|
|
46
|
+
log.warn(e)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _dictize_datarequest(datarequest):
|
|
50
|
+
# Transform time
|
|
51
|
+
open_time = str(datarequest.open_time)
|
|
52
|
+
# Close time can be None and the transformation is only needed when the
|
|
53
|
+
# fields contains a valid date
|
|
54
|
+
close_time = datarequest.close_time
|
|
55
|
+
close_time = str(close_time) if close_time else close_time
|
|
56
|
+
|
|
57
|
+
# Convert the data request into a dict
|
|
58
|
+
data_dict = {
|
|
59
|
+
'id': datarequest.id,
|
|
60
|
+
'user_id': datarequest.user_id,
|
|
61
|
+
'title': datarequest.title,
|
|
62
|
+
'description': datarequest.description,
|
|
63
|
+
'organization_id': datarequest.organization_id,
|
|
64
|
+
'open_time': open_time,
|
|
65
|
+
'accepted_dataset_id': datarequest.accepted_dataset_id,
|
|
66
|
+
'close_time': close_time,
|
|
67
|
+
'closed': datarequest.closed,
|
|
68
|
+
'user': _get_user(datarequest.user_id),
|
|
69
|
+
'organization': None,
|
|
70
|
+
'accepted_dataset': None,
|
|
71
|
+
'followers': 0,
|
|
72
|
+
'dataset_url': helpers.url_for('datarequest.show', id=datarequest.id, qualified=True)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if datarequest.organization_id:
|
|
76
|
+
data_dict['organization'] = _get_organization(datarequest.organization_id)
|
|
77
|
+
|
|
78
|
+
if datarequest.accepted_dataset_id:
|
|
79
|
+
data_dict['accepted_dataset'] = _get_package(datarequest.accepted_dataset_id)
|
|
80
|
+
|
|
81
|
+
data_dict['followers'] = db.DataRequestFollower.get_datarequest_followers_number(
|
|
82
|
+
datarequest_id=datarequest.id)
|
|
83
|
+
|
|
84
|
+
if tk.h.closing_circumstances_enabled:
|
|
85
|
+
data_dict['close_circumstance'] = datarequest.close_circumstance
|
|
86
|
+
data_dict['approx_publishing_date'] = datarequest.approx_publishing_date
|
|
87
|
+
|
|
88
|
+
return data_dict
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _undictize_datarequest_basic(datarequest, data_dict):
|
|
92
|
+
datarequest.title = data_dict['title']
|
|
93
|
+
datarequest.description = data_dict['description']
|
|
94
|
+
organization = data_dict['organization_id']
|
|
95
|
+
datarequest.organization_id = organization if organization else None
|
|
96
|
+
_undictize_datarequest_closing_circumstances(datarequest, data_dict)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _undictize_datarequest_closing_circumstances(datarequest, data_dict):
|
|
100
|
+
if tk.h.closing_circumstances_enabled:
|
|
101
|
+
datarequest.close_circumstance = data_dict.get('close_circumstance') or None
|
|
102
|
+
datarequest.approx_publishing_date = data_dict.get('approx_publishing_date') or None
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _send_mail(user_ids, action_type, datarequest, job_title):
|
|
106
|
+
for user_id in user_ids:
|
|
107
|
+
try:
|
|
108
|
+
user_data = model.User.get(user_id)
|
|
109
|
+
extra_vars = {
|
|
110
|
+
'datarequest': datarequest,
|
|
111
|
+
'user': user_data,
|
|
112
|
+
'site_title': config.get('ckan.site_title'),
|
|
113
|
+
'site_url': config.get('ckan.site_url')
|
|
114
|
+
}
|
|
115
|
+
subject = base.render_jinja2('emails/subjects/{0}.txt'.format(action_type), extra_vars)
|
|
116
|
+
body = base.render_jinja2('emails/bodies/{0}.txt'.format(action_type), extra_vars)
|
|
117
|
+
tk.enqueue_job(mailer.mail_user, [user_data, subject, body], title=job_title)
|
|
118
|
+
except Exception:
|
|
119
|
+
logging.exception("Error sending notification to {0}".format(user_id))
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _get_admin_users_from_organisation(datarequest_dict):
|
|
123
|
+
# Data QLD modification.
|
|
124
|
+
users = set([user['id'] for user in datarequest_dict['organization']['users'] if user.get('capacity') == 'admin'])
|
|
125
|
+
return users
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
# Copied from ckanext.datarequests.actions. Please keep up to date with any extension updates
|
|
129
|
+
@tk.chained_action
|
|
130
|
+
def create_datarequest(original_action, context, data_dict):
|
|
131
|
+
"""
|
|
132
|
+
Action to create a new data request. The function checks the access rights
|
|
133
|
+
of the user before creating the data request. If the user is not allowed
|
|
134
|
+
a NotAuthorized exception will be risen.
|
|
135
|
+
|
|
136
|
+
In addition, you should note that the parameters will be checked and an
|
|
137
|
+
exception (ValidationError) will be risen if some of these parameters are
|
|
138
|
+
not valid.
|
|
139
|
+
|
|
140
|
+
Data QLD modification
|
|
141
|
+
Will send email notification to users of assigned organisation with admin access
|
|
142
|
+
|
|
143
|
+
:param title: The title of the data request
|
|
144
|
+
:type title: string
|
|
145
|
+
|
|
146
|
+
:param description: A brief description for your data request
|
|
147
|
+
:type description: string
|
|
148
|
+
|
|
149
|
+
:param organiztion_id: The ID of the organization you want to asign the
|
|
150
|
+
data request (optional).
|
|
151
|
+
:type organization_id: string
|
|
152
|
+
|
|
153
|
+
:returns: A dict with the data request (id, user_id, title, description,
|
|
154
|
+
organization_id, open_time, accepted_dataset, close_time, closed,
|
|
155
|
+
followers)
|
|
156
|
+
:rtype: dict
|
|
157
|
+
"""
|
|
158
|
+
|
|
159
|
+
model = context['model']
|
|
160
|
+
session = context['session']
|
|
161
|
+
|
|
162
|
+
# Init the data base
|
|
163
|
+
db.init_db(model)
|
|
164
|
+
|
|
165
|
+
# Check access
|
|
166
|
+
tk.check_access(constants.CREATE_DATAREQUEST, context, data_dict)
|
|
167
|
+
|
|
168
|
+
# Validate data
|
|
169
|
+
validator.validate_datarequest(context, data_dict)
|
|
170
|
+
|
|
171
|
+
# Store the data
|
|
172
|
+
data_req = db.DataRequest()
|
|
173
|
+
_undictize_datarequest_basic(data_req, data_dict)
|
|
174
|
+
data_req.user_id = context['auth_user_obj'].id
|
|
175
|
+
data_req.open_time = datetime.datetime.utcnow()
|
|
176
|
+
|
|
177
|
+
session.add(data_req)
|
|
178
|
+
session.commit()
|
|
179
|
+
|
|
180
|
+
datarequest_dict = _dictize_datarequest(data_req)
|
|
181
|
+
|
|
182
|
+
if datarequest_dict['organization']:
|
|
183
|
+
# Data QLD modification
|
|
184
|
+
users = _get_admin_users_from_organisation(datarequest_dict)
|
|
185
|
+
users.discard(context['auth_user_obj'].id)
|
|
186
|
+
_send_mail(users, 'new_datarequest_organisation', datarequest_dict, 'Data Request Created Email')
|
|
187
|
+
|
|
188
|
+
return datarequest_dict
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
# Copied from ckanext.datarequests.actions. Please keep up to date with any action updates
|
|
192
|
+
@tk.chained_action
|
|
193
|
+
def update_datarequest(original_action, context, data_dict):
|
|
194
|
+
"""
|
|
195
|
+
Action to update a data request. The function checks the access rights of
|
|
196
|
+
the user before updating the data request. If the user is not allowed
|
|
197
|
+
a NotAuthorized exception will be risen.
|
|
198
|
+
|
|
199
|
+
In addition, you should note that the parameters will be checked and an
|
|
200
|
+
exception (ValidationError) will be risen if some of these parameters are
|
|
201
|
+
invalid.
|
|
202
|
+
|
|
203
|
+
Data QLD modification
|
|
204
|
+
Will send email notification if organisation was changed to users of assigned organisation with admin access
|
|
205
|
+
|
|
206
|
+
:param id: The ID of the data request to be updated
|
|
207
|
+
:type id: string
|
|
208
|
+
|
|
209
|
+
:param title: The title of the data request
|
|
210
|
+
:type title: string
|
|
211
|
+
|
|
212
|
+
:param description: A brief description for your data request
|
|
213
|
+
:type description: string
|
|
214
|
+
|
|
215
|
+
:param organiztion_id: The ID of the organization you want to asign the
|
|
216
|
+
data request.
|
|
217
|
+
:type organization_id: string
|
|
218
|
+
|
|
219
|
+
:returns: A dict with the data request (id, user_id, title, description,
|
|
220
|
+
organization_id, open_time, accepted_dataset, close_time, closed,
|
|
221
|
+
followers)
|
|
222
|
+
:rtype: dict
|
|
223
|
+
"""
|
|
224
|
+
|
|
225
|
+
model = context['model']
|
|
226
|
+
session = context['session']
|
|
227
|
+
datarequest_id = data_dict.get('id', '')
|
|
228
|
+
|
|
229
|
+
if not datarequest_id:
|
|
230
|
+
raise tk.ValidationError(tk._('Data Request ID has not been included'))
|
|
231
|
+
|
|
232
|
+
# Init the data base
|
|
233
|
+
db.init_db(model)
|
|
234
|
+
|
|
235
|
+
# Check access
|
|
236
|
+
tk.check_access(constants.UPDATE_DATAREQUEST, context, data_dict)
|
|
237
|
+
|
|
238
|
+
# Get the initial data
|
|
239
|
+
result = db.DataRequest.get(id=datarequest_id)
|
|
240
|
+
if not result:
|
|
241
|
+
raise tk.ObjectNotFound(tk._('Data Request %s not found in the data base') % datarequest_id)
|
|
242
|
+
|
|
243
|
+
data_req = result[0]
|
|
244
|
+
|
|
245
|
+
# Avoid the validator to return an error when the user does not change the title
|
|
246
|
+
context['avoid_existing_title_check'] = data_req.title == data_dict['title']
|
|
247
|
+
|
|
248
|
+
# Validate data
|
|
249
|
+
validator.validate_datarequest(context, data_dict)
|
|
250
|
+
|
|
251
|
+
# Data QLD modification
|
|
252
|
+
organisation_updated = data_req.organization_id != data_dict['organization_id']
|
|
253
|
+
if organisation_updated:
|
|
254
|
+
unassigned_organisation_id = data_req.organization_id
|
|
255
|
+
|
|
256
|
+
# Set the data provided by the user in the data_red
|
|
257
|
+
_undictize_datarequest_basic(data_req, data_dict)
|
|
258
|
+
|
|
259
|
+
session.add(data_req)
|
|
260
|
+
session.commit()
|
|
261
|
+
|
|
262
|
+
datarequest_dict = _dictize_datarequest(data_req)
|
|
263
|
+
|
|
264
|
+
if datarequest_dict['organization'] and organisation_updated:
|
|
265
|
+
# Data QLD modification
|
|
266
|
+
# Email Admin users of the assigned organisation
|
|
267
|
+
users = _get_admin_users_from_organisation(datarequest_dict)
|
|
268
|
+
users.discard(context['auth_user_obj'].id)
|
|
269
|
+
_send_mail(users, 'new_datarequest_organisation', datarequest_dict, 'Data Request Assigned Email')
|
|
270
|
+
# Email Admin users of unassigned organisation
|
|
271
|
+
org_dict = {
|
|
272
|
+
'organization': _get_organization(unassigned_organisation_id)
|
|
273
|
+
}
|
|
274
|
+
users = _get_admin_users_from_organisation(org_dict)
|
|
275
|
+
users.discard(context['auth_user_obj'].id)
|
|
276
|
+
_send_mail(users, 'unassigned_datarequest_organisation', datarequest_dict, 'Data Request Unassigned Email')
|
|
277
|
+
|
|
278
|
+
return datarequest_dict
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
# Copied from ckanext.datarequests.actions. Please keep up to date with any action updates
|
|
282
|
+
@tk.chained_action
|
|
283
|
+
def close_datarequest(original_action, context, data_dict):
|
|
284
|
+
"""
|
|
285
|
+
Action to close a data request. Access rights will be checked before
|
|
286
|
+
closing the data request. If the user is not allowed, a NotAuthorized
|
|
287
|
+
exception will be risen.
|
|
288
|
+
|
|
289
|
+
Data QLD modification
|
|
290
|
+
Will send email notification to the data request creator
|
|
291
|
+
|
|
292
|
+
:param id: The ID of the data request to be closed
|
|
293
|
+
:type id: string
|
|
294
|
+
|
|
295
|
+
:param accepted_dataset_id: The ID of the dataset accepted as solution
|
|
296
|
+
for this data request
|
|
297
|
+
:type accepted_dataset_id: string
|
|
298
|
+
|
|
299
|
+
:returns: A dict with the data request (id, user_id, title, description,
|
|
300
|
+
organization_id, open_time, accepted_dataset, close_time, closed,
|
|
301
|
+
followers)
|
|
302
|
+
:rtype: dict
|
|
303
|
+
|
|
304
|
+
"""
|
|
305
|
+
|
|
306
|
+
model = context['model']
|
|
307
|
+
session = context['session']
|
|
308
|
+
datarequest_id = data_dict.get('id', '')
|
|
309
|
+
|
|
310
|
+
# Check id
|
|
311
|
+
if not datarequest_id:
|
|
312
|
+
raise tk.ValidationError(tk._('Data Request ID has not been included'))
|
|
313
|
+
|
|
314
|
+
# Init the data base
|
|
315
|
+
db.init_db(model)
|
|
316
|
+
|
|
317
|
+
# Check access
|
|
318
|
+
tk.check_access(constants.CLOSE_DATAREQUEST, context, data_dict)
|
|
319
|
+
|
|
320
|
+
# Get the data request
|
|
321
|
+
result = db.DataRequest.get(id=datarequest_id)
|
|
322
|
+
if not result:
|
|
323
|
+
raise tk.ObjectNotFound(tk._('Data Request %s not found in the data base') % datarequest_id)
|
|
324
|
+
|
|
325
|
+
# Validate data
|
|
326
|
+
validator.validate_datarequest_closing(context, data_dict)
|
|
327
|
+
|
|
328
|
+
data_req = result[0]
|
|
329
|
+
|
|
330
|
+
# Was the data request previously closed?
|
|
331
|
+
if data_req.closed:
|
|
332
|
+
raise tk.ValidationError([tk._('This Data Request is already closed')])
|
|
333
|
+
|
|
334
|
+
data_req.closed = True
|
|
335
|
+
data_req.accepted_dataset_id = data_dict.get('accepted_dataset_id') or None
|
|
336
|
+
data_req.close_time = datetime.datetime.utcnow()
|
|
337
|
+
_undictize_datarequest_closing_circumstances(data_req, data_dict)
|
|
338
|
+
|
|
339
|
+
session.add(data_req)
|
|
340
|
+
session.commit()
|
|
341
|
+
|
|
342
|
+
datarequest_dict = _dictize_datarequest(data_req)
|
|
343
|
+
|
|
344
|
+
# Mailing
|
|
345
|
+
users = [data_req.user_id]
|
|
346
|
+
_send_mail(users, 'close_datarequest_creator', datarequest_dict, 'Data Request Closed Send Email')
|
|
347
|
+
|
|
348
|
+
return datarequest_dict
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
def open_datarequest(context, data_dict):
|
|
352
|
+
"""
|
|
353
|
+
Action to open a data request. Access rights will be checked before
|
|
354
|
+
opening the data request. If the user is not allowed, a NotAuthorized
|
|
355
|
+
exception will be risen.
|
|
356
|
+
|
|
357
|
+
:param id: The ID of the data request to be closed
|
|
358
|
+
:type id: string
|
|
359
|
+
|
|
360
|
+
:returns: A dict with the data request (id, user_id, title, description,
|
|
361
|
+
organization_id, open_time, accepted_dataset, close_time, closed,
|
|
362
|
+
followers)
|
|
363
|
+
:rtype: dict
|
|
364
|
+
|
|
365
|
+
"""
|
|
366
|
+
|
|
367
|
+
model = context['model']
|
|
368
|
+
session = context['session']
|
|
369
|
+
datarequest_id = data_dict.get('id', '')
|
|
370
|
+
|
|
371
|
+
# Check id
|
|
372
|
+
if not datarequest_id:
|
|
373
|
+
raise tk.ValidationError(tk._('Data Request ID has not been included'))
|
|
374
|
+
|
|
375
|
+
# Init the data base
|
|
376
|
+
db.init_db(model)
|
|
377
|
+
|
|
378
|
+
# Check access
|
|
379
|
+
tk.check_access(constants.OPEN_DATAREQUEST, context, data_dict)
|
|
380
|
+
|
|
381
|
+
# Get the data request
|
|
382
|
+
result = db.DataRequest.get(id=datarequest_id)
|
|
383
|
+
|
|
384
|
+
if not result:
|
|
385
|
+
raise tk.ObjectNotFound(tk._('Data Request %s not found in the data base') % datarequest_id)
|
|
386
|
+
|
|
387
|
+
data_req = result[0]
|
|
388
|
+
data_req.closed = False
|
|
389
|
+
data_req.accepted_dataset_id = None
|
|
390
|
+
data_req.close_time = None
|
|
391
|
+
if tk.h.closing_circumstances_enabled:
|
|
392
|
+
data_req.close_circumstance = None
|
|
393
|
+
data_req.approx_publishing_date = None
|
|
394
|
+
|
|
395
|
+
session.add(data_req)
|
|
396
|
+
session.commit()
|
|
397
|
+
|
|
398
|
+
datarequest_dict = _dictize_datarequest(data_req)
|
|
399
|
+
|
|
400
|
+
# Mailing
|
|
401
|
+
users = [data_req.user_id]
|
|
402
|
+
# Creator email
|
|
403
|
+
_send_mail(users, 'open_datarequest_creator', datarequest_dict, 'Data Request Opened Creator Email')
|
|
404
|
+
if datarequest_dict['organization']:
|
|
405
|
+
users = _get_admin_users_from_organisation(datarequest_dict)
|
|
406
|
+
# Admins of organisation email
|
|
407
|
+
_send_mail(users, 'open_datarequest_organisation', datarequest_dict, 'Data Request Opened Admins Email')
|
|
408
|
+
|
|
409
|
+
return datarequest_dict
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
import ckan.authz as authz
|
|
4
|
+
from ckantoolkit import _
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def has_user_permission_for_some_org(context, data_dict):
|
|
8
|
+
user = context.get('user', '')
|
|
9
|
+
permission = data_dict.get('permission', '')
|
|
10
|
+
if authz.has_user_permission_for_some_org(user, permission):
|
|
11
|
+
return {'success': True}
|
|
12
|
+
else:
|
|
13
|
+
return {'success': False,
|
|
14
|
+
'msg': _('User {0} has no {1} permission for any organisation'.format(user, permission))}
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def has_user_permission_for_org(context, data_dict):
|
|
18
|
+
user = context.get('user', '')
|
|
19
|
+
org_id = data_dict.get('org_id', '')
|
|
20
|
+
permission = data_dict.get('permission', '')
|
|
21
|
+
if authz.has_user_permission_for_group_or_org(org_id, user, permission):
|
|
22
|
+
return {'success': True}
|
|
23
|
+
else:
|
|
24
|
+
return {'success': False,
|
|
25
|
+
'msg': _('User {0} is not authorized to {1} for organisation {2}'.format(user, permission, org_id))}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
from flask import Blueprint
|
|
4
|
+
|
|
5
|
+
from constants import DATAREQUESTS_MAIN_PATH, SCHEMA_MAIN_PATH
|
|
6
|
+
from controller_functions import open_datarequest, show_schema
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
blueprint = Blueprint(
|
|
10
|
+
u'data_qld',
|
|
11
|
+
__name__
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
blueprint.add_url_rule(
|
|
15
|
+
u'/{}/open/<id>'.format(DATAREQUESTS_MAIN_PATH),
|
|
16
|
+
view_func=open_datarequest)
|
|
17
|
+
blueprint.add_url_rule(
|
|
18
|
+
u'/dataset/<dataset_id>resource/<resource_id>/{}/show'.format(SCHEMA_MAIN_PATH),
|
|
19
|
+
view_func=show_schema)
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
import click
|
|
4
|
+
|
|
5
|
+
from ckanext.data_qld import command_functions
|
|
6
|
+
|
|
7
|
+
# Click commands for CKAN 2.9 and above
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@click.group()
|
|
11
|
+
def data_qld():
|
|
12
|
+
""" Queensland Government Open Data commands
|
|
13
|
+
"""
|
|
14
|
+
pass
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@data_qld.command()
|
|
18
|
+
def migrate_extras():
|
|
19
|
+
"""Migrates legacy field values that were added as free extras to datasets
|
|
20
|
+
to their schema counterparts.
|
|
21
|
+
"""
|
|
22
|
+
command_functions.migrate_extras()
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@data_qld.command()
|
|
26
|
+
def demote_publishers():
|
|
27
|
+
"""Demotes any existing 'publisher-*' users from admin to editor
|
|
28
|
+
in their respective organisations.
|
|
29
|
+
"""
|
|
30
|
+
command_functions.demote_publishers()
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@data_qld.command()
|
|
34
|
+
def send_email_dataset_due_to_publishing_notification():
|
|
35
|
+
"""
|
|
36
|
+
Send datasets due to publishing to the contact email.
|
|
37
|
+
"""
|
|
38
|
+
command_functions.send_email_dataset_due_to_publishing_notification()
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@data_qld.command()
|
|
42
|
+
def send_email_dataset_overdue_notification():
|
|
43
|
+
"""
|
|
44
|
+
Send datasets overdue to the contact email.
|
|
45
|
+
"""
|
|
46
|
+
command_functions.send_email_dataset_overdue_notification()
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def get_commands():
|
|
50
|
+
return [data_qld]
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
|
|
5
|
+
from ckantoolkit import get_action, ValidationError
|
|
6
|
+
|
|
7
|
+
from ckanext.data_qld.resource_freshness.helpers import helpers as resource_freshness_helpers
|
|
8
|
+
|
|
9
|
+
log = logging.getLogger(__name__)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def migrate_extras(package_ids):
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
:return:
|
|
16
|
+
"""
|
|
17
|
+
context = {'ignore_auth': True}
|
|
18
|
+
package_ids = [pkg['id'] for pkg in get_action('package_list')(context=context)]
|
|
19
|
+
|
|
20
|
+
package_patch = get_action('package_patch')
|
|
21
|
+
# Step 1: Get all the package IDs.
|
|
22
|
+
for package_id in package_ids:
|
|
23
|
+
# Set some defaults
|
|
24
|
+
default_security_classification = "PUBLIC"
|
|
25
|
+
default_data_driven_application = "NO"
|
|
26
|
+
default_version = "1.0"
|
|
27
|
+
default_author_email = "opendata@qld.gov.au"
|
|
28
|
+
default_update_frequency = "annually"
|
|
29
|
+
default_size = '1' # 1 Byte
|
|
30
|
+
resources = []
|
|
31
|
+
|
|
32
|
+
pkg = get_action('package_show')(context, {
|
|
33
|
+
'id': package_id
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
if pkg['resources']:
|
|
37
|
+
size = default_size
|
|
38
|
+
|
|
39
|
+
for resource in pkg['resources']:
|
|
40
|
+
if 'size' in resource:
|
|
41
|
+
size = resource['size'] if resource['size'] is not None and resource[
|
|
42
|
+
'size'] != '0 bytes' else default_size
|
|
43
|
+
|
|
44
|
+
if 'name' in resource:
|
|
45
|
+
name = resource['name']
|
|
46
|
+
|
|
47
|
+
if 'description' in resource:
|
|
48
|
+
description = resource['description'] or name
|
|
49
|
+
|
|
50
|
+
update_resource = {
|
|
51
|
+
"id": resource['id'],
|
|
52
|
+
"size": size,
|
|
53
|
+
"name": name,
|
|
54
|
+
"description": description,
|
|
55
|
+
"url": resource['url']
|
|
56
|
+
}
|
|
57
|
+
resources.append(update_resource)
|
|
58
|
+
|
|
59
|
+
# Go through the packages and check for presence of 'Security classification'
|
|
60
|
+
# and 'Used in data-driven application' extras
|
|
61
|
+
security_classification = default_security_classification
|
|
62
|
+
data_driven_application = default_data_driven_application
|
|
63
|
+
version = default_version
|
|
64
|
+
author_email = default_author_email
|
|
65
|
+
update_frequency = default_update_frequency
|
|
66
|
+
|
|
67
|
+
if pkg.get('extras', None):
|
|
68
|
+
|
|
69
|
+
for extra in pkg['extras']:
|
|
70
|
+
if extra['key'] == 'Security classification':
|
|
71
|
+
security_classification = extra['value'] or default_security_classification
|
|
72
|
+
elif extra['key'] in ['Used in data-driven application']:
|
|
73
|
+
data_driven_application = extra['value'] or default_data_driven_application
|
|
74
|
+
|
|
75
|
+
if 'version' in pkg:
|
|
76
|
+
version = pkg['version'] or default_version
|
|
77
|
+
|
|
78
|
+
if 'author_email' in pkg:
|
|
79
|
+
author_email = pkg['author_email'] or default_author_email
|
|
80
|
+
|
|
81
|
+
if 'notes' in pkg:
|
|
82
|
+
notes = pkg['notes'] or pkg['title']
|
|
83
|
+
|
|
84
|
+
if 'update_frequency' in pkg:
|
|
85
|
+
update_frequency = pkg['update_frequency'] or default_update_frequency
|
|
86
|
+
|
|
87
|
+
try:
|
|
88
|
+
package_patch(
|
|
89
|
+
context=context,
|
|
90
|
+
data_dict={
|
|
91
|
+
'id': package_id,
|
|
92
|
+
'security_classification': security_classification,
|
|
93
|
+
'data_driven_application': data_driven_application,
|
|
94
|
+
'version': version,
|
|
95
|
+
'author_email': author_email,
|
|
96
|
+
'notes': notes,
|
|
97
|
+
'update_frequency': update_frequency,
|
|
98
|
+
'resources': resources
|
|
99
|
+
})
|
|
100
|
+
except ValidationError as e:
|
|
101
|
+
print('Package Failed: ', package_id, '\n', e.error_dict, )
|
|
102
|
+
print('Package Payload: ', package_id, security_classification, data_driven_application, version, author_email, notes, update_frequency, resources)
|
|
103
|
+
|
|
104
|
+
return 'SUCCESS'
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _get_organizations():
|
|
108
|
+
return get_action('organization_list')(data_dict={'all_fields': True, 'include_users': True})
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _patch_organisation_users(org_id, users):
|
|
112
|
+
return get_action('organization_patch')(data_dict={'id': org_id, 'users': users})
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def demote_publishers(username_prefix):
|
|
116
|
+
"""
|
|
117
|
+
|
|
118
|
+
:return:
|
|
119
|
+
"""
|
|
120
|
+
updates = 0
|
|
121
|
+
|
|
122
|
+
for org in _get_organizations():
|
|
123
|
+
print('- - - - - - - - - - - - - - - - - - - - - - - - -')
|
|
124
|
+
updates_required = False
|
|
125
|
+
users = org.get('users', [])
|
|
126
|
+
print('Processing organisation ID: %s | Name: %s' % (org['id'], org['name']))
|
|
127
|
+
if users:
|
|
128
|
+
for user in org['users']:
|
|
129
|
+
if user['name'].startswith(username_prefix) and user['capacity'] == 'admin':
|
|
130
|
+
print('- Setting capacity for user %s to "editor" in organisation %s' % (user['name'], org['name']))
|
|
131
|
+
user['capacity'] = 'editor'
|
|
132
|
+
updates_required = True
|
|
133
|
+
updates += 1
|
|
134
|
+
if updates_required:
|
|
135
|
+
print('- Updating user capacities for organisation %s' % org['name'])
|
|
136
|
+
_patch_organisation_users(org['id'], users)
|
|
137
|
+
else:
|
|
138
|
+
print('- Nothing to update for organisation %s' % org['name'])
|
|
139
|
+
|
|
140
|
+
print('- - - - - - - - - - - - - - - - - - - - - - - - -')
|
|
141
|
+
|
|
142
|
+
return "COMPLETED. Total updates %s\n" % updates
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def send_email_dataset_due_to_publishing_notification():
|
|
146
|
+
# Need to do CKAN imports and logger after load config
|
|
147
|
+
log.info('Started command SendEmailDatasetDueToPublishingNotification')
|
|
148
|
+
resource_freshness_helpers.process_email_notification_for_dataset_due_to_publishing()
|
|
149
|
+
log.info('Finished command SendEmailDatasetDueToPublishingNotification')
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def send_email_dataset_overdue_notification():
|
|
153
|
+
# Need to do CKAN imports and logger after load config
|
|
154
|
+
log.info('Started command SendEmailDatasetOverdueNotification')
|
|
155
|
+
resource_freshness_helpers.process_email_notification_for_dataset_overdue()
|
|
156
|
+
log.info('Finished command SendEmailDatasetOverdueNotification')
|