/
update.py
710 lines (555 loc) · 24 KB
/
update.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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
import logging
import datetime
from ckan.plugins import PluginImplementations, IGroupController, IPackageController
from ckan.logic import NotFound, ValidationError, ParameterError, NotAuthorized
from ckan.logic import get_action, check_access
from lib.plugins import lookup_package_plugin
from ckan.lib.base import _
from vdm.sqlalchemy.base import SQLAlchemySession
import ckan.lib.dictization
from ckan.lib.dictization.model_dictize import (package_dictize,
package_to_api1,
package_to_api2,
resource_dictize,
task_status_dictize,
group_dictize,
group_to_api1,
group_to_api2,
user_dictize,
vocabulary_dictize)
from ckan.lib.dictization.model_save import (group_api_to_dict,
package_api_to_dict,
group_dict_save,
user_dict_save,
task_status_dict_save,
package_dict_save,
resource_dict_save,
vocabulary_dict_update)
from ckan.logic.schema import (default_update_group_schema,
default_update_package_schema,
default_update_user_schema,
default_update_resource_schema,
default_task_status_schema,
default_update_relationship_schema,
default_update_vocabulary_schema)
from ckan.lib.navl.dictization_functions import validate
import ckan.lib.navl.validators as validators
from ckan.lib.navl.dictization_functions import DataError
from ckan.logic.action import rename_keys, get_domain_object, error_summary
from ckan.logic.action.get import roles_show
log = logging.getLogger(__name__)
def _make_latest_rev_active(context, q):
session = context['model'].Session
old_current = q.filter_by(current=True).first()
if old_current:
old_current.current = False
session.add(old_current)
latest_rev = q.filter_by(expired_timestamp=datetime.datetime(9999, 12, 31)).one()
latest_rev.current = True
if latest_rev.state in ('pending-deleted', 'deleted'):
latest_rev.state = 'deleted'
latest_rev.continuity.state = 'deleted'
else:
latest_rev.continuity.state = 'active'
latest_rev.state = 'active'
session.add(latest_rev)
##this is just a way to get the latest revision that changed
##in order to timestamp
old_latest = context.get('latest_revision_date')
if old_latest:
if latest_rev.revision_timestamp > old_latest:
context['latest_revision_date'] = latest_rev.revision_timestamp
context['latest_revision'] = latest_rev.revision_id
else:
context['latest_revision_date'] = latest_rev.revision_timestamp
context['latest_revision'] = latest_rev.revision_id
def make_latest_pending_package_active(context, data_dict):
model = context['model']
session = model.Session
SQLAlchemySession.setattr(session, 'revisioning_disabled', True)
id = data_dict["id"]
pkg = model.Package.get(id)
check_access('make_latest_pending_package_active', context, data_dict)
#packages
q = session.query(model.PackageRevision).filter_by(id=pkg.id)
_make_latest_rev_active(context, q)
#resources
for resource in pkg.resource_groups_all[0].resources_all:
q = session.query(model.ResourceRevision).filter_by(id=resource.id)
_make_latest_rev_active(context, q)
#tags
for tag in pkg.package_tag_all:
q = session.query(model.PackageTagRevision).filter_by(id=tag.id)
_make_latest_rev_active(context, q)
#extras
for extra in pkg.extras_list:
q = session.query(model.PackageExtraRevision).filter_by(id=extra.id)
_make_latest_rev_active(context, q)
latest_revision = context.get('latest_revision')
if not latest_revision:
return
q = session.query(model.Revision).filter_by(id=latest_revision)
revision = q.first()
revision.approved_timestamp = datetime.datetime.now()
session.add(revision)
if not context.get('defer_commit'):
session.commit()
session.remove()
def resource_update(context, data_dict):
model = context['model']
user = context['user']
id = data_dict["id"]
schema = context.get('schema') or default_update_resource_schema()
model.Session.remove()
resource = model.Resource.get(id)
context["resource"] = resource
if not resource:
logging.error('Could not find resource ' + id)
raise NotFound(_('Resource was not found.'))
check_access('resource_update', context, data_dict)
data, errors = validate(data_dict, schema, context)
if errors:
model.Session.rollback()
raise ValidationError(errors, error_summary(errors))
rev = model.repo.new_revision()
rev.author = user
if 'message' in context:
rev.message = context['message']
else:
rev.message = _(u'REST API: Update object %s') % data.get("name", "")
resource = resource_dict_save(data, context)
if not context.get('defer_commit'):
model.repo.commit()
return resource_dictize(resource, context)
def package_update(context, data_dict):
model = context['model']
user = context['user']
name_or_id = data_dict.get("id") or data_dict['name_or_id']
model.Session.remove()
model.Session()._context = context
pkg = model.Package.get(name_or_id)
if pkg is None:
raise NotFound(_('Package was not found.'))
context["package"] = pkg
data_dict["id"] = pkg.id
check_access('package_update', context, data_dict)
# get the schema
package_plugin = lookup_package_plugin(pkg.type)
try:
schema = package_plugin.form_to_db_schema_options({'type':'update',
'api':'api_version' in context})
except AttributeError:
schema = package_plugin.form_to_db_schema()
if 'api_version' not in context:
package_plugin.check_data_dict(data_dict, schema)
data, errors = validate(data_dict, schema, context)
if errors:
model.Session.rollback()
raise ValidationError(errors, error_summary(errors))
rev = model.repo.new_revision()
rev.author = user
if 'message' in context:
rev.message = context['message']
else:
rev.message = _(u'REST API: Update object %s') % data.get("name")
pkg = package_dict_save(data, context)
for item in PluginImplementations(IPackageController):
item.edit(pkg)
if not context.get('defer_commit'):
model.repo.commit()
return package_dictize(pkg, context)
def package_update_validate(context, data_dict):
model = context['model']
user = context['user']
id = data_dict["id"]
model.Session.remove()
model.Session()._context = context
pkg = model.Package.get(id)
context["package"] = pkg
if pkg is None:
raise NotFound(_('Package was not found.'))
data_dict["id"] = pkg.id
# get the schema
package_plugin = lookup_package_plugin(pkg.type)
try:
schema = package_plugin.form_to_db_schema_options({'type':'update',
'api':'api_version' in context})
except AttributeError:
schema = package_plugin.form_to_db_schema()
check_access('package_update', context, data_dict)
data, errors = validate(data_dict, schema, context)
if errors:
model.Session.rollback()
raise ValidationError(errors, error_summary(errors))
return data
def _update_package_relationship(relationship, comment, context):
model = context['model']
api = context.get('api_version') or '1'
ref_package_by = 'id' if api == '2' else 'name'
is_changed = relationship.comment != comment
if is_changed:
rev = model.repo.new_revision()
rev.author = context["user"]
rev.message = (_(u'REST API: Update package relationship: %s %s %s') %
(relationship.subject, relationship.type, relationship.object))
relationship.comment = comment
if not context.get('defer_commit'):
model.repo.commit_and_remove()
rel_dict = relationship.as_dict(package=relationship.subject,
ref_package_by=ref_package_by)
return rel_dict
def package_relationship_update(context, data_dict):
model = context['model']
user = context['user']
schema = context.get('schema') or default_update_relationship_schema()
api = context.get('api_version') or '1'
id = data_dict['subject']
id2 = data_dict['object']
rel = data_dict['type']
ref_package_by = 'id' if api == '2' else 'name'
pkg1 = model.Package.get(id)
pkg2 = model.Package.get(id2)
if not pkg1:
raise NotFound('Subject package %r was not found.' % id)
if not pkg2:
return NotFound('Object package %r was not found.' % id2)
data, errors = validate(data_dict, schema, context)
if errors:
model.Session.rollback()
raise ValidationError(errors, error_summary(errors))
check_access('package_relationship_update', context, data_dict)
existing_rels = pkg1.get_relationships_with(pkg2, rel)
if not existing_rels:
raise NotFound('This relationship between the packages was not found.')
entity = existing_rels[0]
comment = data_dict.get('comment', u'')
context['relationship'] = entity
return _update_package_relationship(entity, comment, context)
def group_update(context, data_dict):
model = context['model']
user = context['user']
session = context['session']
schema = context.get('schema') or default_update_group_schema()
id = data_dict['id']
parent = context.get('parent', None)
group = model.Group.get(id)
context["group"] = group
if group is None:
raise NotFound('Group was not found.')
check_access('group_update', context, data_dict)
data, errors = validate(data_dict, schema, context)
if errors:
session.rollback()
raise ValidationError(errors, error_summary(errors))
rev = model.repo.new_revision()
rev.author = user
if 'message' in context:
rev.message = context['message']
else:
rev.message = _(u'REST API: Update object %s') % data.get("name")
group = group_dict_save(data, context)
if parent:
parent_group = model.Group.get( parent )
if parent_group and not parent_group in group.get_groups(group.type):
# Delete all of this groups memberships
current = session.query(model.Member).\
filter(model.Member.table_id == group.id).\
filter(model.Member.table_name == "group").all()
for c in current:
session.delete(c)
member = model.Member(group=parent_group, table_id=group.id, table_name='group')
session.add(member)
for item in PluginImplementations(IGroupController):
item.edit(group)
activity_dict = {
'user_id': model.User.by_name(user.decode('utf8')).id,
'object_id': group.id,
'activity_type': 'changed group',
}
# Handle 'deleted' groups.
# When the user marks a group as deleted this comes through here as
# a 'changed' group activity. We detect this and change it to a 'deleted'
# activity.
if group.state == u'deleted':
if session.query(ckan.model.Activity).filter_by(
object_id=group.id, activity_type='deleted').all():
# A 'deleted group' activity for this group has already been
# emitted.
# FIXME: What if the group was deleted and then activated again?
activity_dict = None
else:
# We will emit a 'deleted group' activity.
activity_dict['activity_type'] = 'deleted group'
if activity_dict is not None:
activity_dict['data'] = {
'group': ckan.lib.dictization.table_dictize(group, context)
}
from ckan.logic.action.create import activity_create
activity_create_context = {
'model': model,
'user': user,
'defer_commit':True,
'session': session
}
activity_create(activity_create_context, activity_dict,
ignore_auth=True)
# TODO: Also create an activity detail recording what exactly changed
# in the group.
if not context.get('defer_commit'):
model.repo.commit()
return group_dictize(group, context)
def user_update(context, data_dict):
'''Updates the user\'s details'''
model = context['model']
user = context['user']
session = context['session']
schema = context.get('schema') or default_update_user_schema()
id = data_dict['id']
user_obj = model.User.get(id)
context['user_obj'] = user_obj
if user_obj is None:
raise NotFound('User was not found.')
check_access('user_update', context, data_dict)
data, errors = validate(data_dict, schema, context)
if errors:
session.rollback()
raise ValidationError(errors, error_summary(errors))
user = user_dict_save(data, context)
activity_dict = {
'user_id': user.id,
'object_id': user.id,
'activity_type': 'changed user',
}
activity_create_context = {
'model': model,
'user': user,
'defer_commit':True,
'session': session
}
from ckan.logic.action.create import activity_create
activity_create(activity_create_context, activity_dict, ignore_auth=True)
# TODO: Also create an activity detail recording what exactly changed in
# the user.
if not context.get('defer_commit'):
model.repo.commit()
return user_dictize(user, context)
def task_status_update(context, data_dict):
model = context['model']
session = model.meta.create_local_session()
context['session'] = session
user = context['user']
id = data_dict.get("id")
schema = context.get('schema') or default_task_status_schema()
if id:
task_status = model.TaskStatus.get(id)
context["task_status"] = task_status
if task_status is None:
raise NotFound(_('TaskStatus was not found.'))
check_access('task_status_update', context, data_dict)
data, errors = validate(data_dict, schema, context)
if errors:
session.rollback()
raise ValidationError(errors, error_summary(errors))
task_status = task_status_dict_save(data, context)
session.commit()
session.close()
return task_status_dictize(task_status, context)
def task_status_update_many(context, data_dict):
results = []
model = context['model']
deferred = context.get('defer_commit')
context['defer_commit'] = True
for data in data_dict['data']:
results.append(task_status_update(context, data))
if not deferred:
context.pop('defer_commit')
if not context.get('defer_commit'):
model.Session.commit()
return {'results': results}
def term_translation_update(context, data_dict):
model = context['model']
check_access('term_translation_update', context, data_dict)
schema = {'term': [validators.not_empty, unicode],
'term_translation': [validators.not_empty, unicode],
'lang_code': [validators.not_empty, unicode]}
data, errors = validate(data_dict, schema, context)
if errors:
model.Session.rollback()
raise ValidationError(errors)
trans_table = model.term_translation_table
update = trans_table.update()
update = update.where(trans_table.c.term == data['term'])
update = update.where(trans_table.c.lang_code == data['lang_code'])
update = update.values(term_translation = data['term_translation'])
conn = model.Session.connection()
result = conn.execute(update)
# insert if not updated
if not result.rowcount:
conn.execute(trans_table.insert().values(**data))
if not context.get('defer_commit'):
model.Session.commit()
return data
def term_translation_update_many(context, data_dict):
model = context['model']
if not data_dict.get('data') and isinstance(data_dict, list):
raise ValidationError(
{'error':
'term_translation_update_many needs to have a list of dicts in field data'}
)
context['defer_commit'] = True
for num, row in enumerate(data_dict['data']):
term_translation_update(context, row)
model.Session.commit()
return {'success': '%s rows updated' % (num + 1)}
## Modifications for rest api
def package_update_rest(context, data_dict):
model = context['model']
id = data_dict.get("id")
request_id = context['id']
api = context.get('api_version') or '1'
pkg = model.Package.get(request_id)
if not pkg:
raise NotFound
if id and id != pkg.id:
pkg_from_data = model.Package.get(id)
if pkg_from_data != pkg:
error_dict = {id:('Cannot change value of key from %s to %s. '
'This key is read-only') % (pkg.id, id)}
raise ValidationError(error_dict)
context["package"] = pkg
context["allow_partial_update"] = True
dictized_package = package_api_to_dict(data_dict, context)
check_access('package_update_rest', context, dictized_package)
dictized_after = get_action('package_update')(context, dictized_package)
pkg = context['package']
if api == '1':
package_dict = package_to_api1(pkg, context)
else:
package_dict = package_to_api2(pkg, context)
return package_dict
def group_update_rest(context, data_dict):
model = context['model']
id = data_dict["id"]
api = context.get('api_version') or '1'
group = model.Group.get(id)
context["group"] = group
context["allow_partial_update"] = True
dictized_group = group_api_to_dict(data_dict, context)
check_access('group_update_rest', context, dictized_group)
dictized_after = group_update(context, dictized_group)
group = context['group']
if api == '1':
group_dict = group_to_api1(group, context)
else:
group_dict = group_to_api2(group, context)
return group_dict
def vocabulary_update(context, data_dict):
model = context['model']
vocab_id = data_dict.get('id')
if not vocab_id:
raise ValidationError({'id': _('id not in data')})
vocab = model.vocabulary.Vocabulary.get(vocab_id)
if vocab is None:
raise NotFound(_('Could not find vocabulary "%s"') % vocab_id)
data_dict['id'] = vocab.id
if data_dict.has_key('name'):
if data_dict['name'] == vocab.name:
del data_dict['name']
check_access('vocabulary_update', context, data_dict)
schema = context.get('schema') or default_update_vocabulary_schema()
data, errors = validate(data_dict, schema, context)
if errors:
model.Session.rollback()
raise ValidationError(errors)
updated_vocab = vocabulary_dict_update(data, context)
if not context.get('defer_commit'):
model.repo.commit()
return vocabulary_dictize(updated_vocab, context)
def package_relationship_update_rest(context, data_dict):
# rename keys
key_map = {'id': 'subject',
'id2': 'object',
'rel': 'type'}
# We want 'destructive', so that the value of the subject,
# object and rel in the URI overwrite any values for these
# in params. This is because you are not allowed to change
# these values.
data_dict = rename_keys(data_dict, key_map, destructive=True)
relationship_dict = package_relationship_update(context, data_dict)
return relationship_dict
def user_role_update(context, data_dict):
'''
For a named user (or authz group), set his/her authz roles on a domain_object.
'''
model = context['model']
user = context['user'] # the current user, who is making the authz change
new_user_ref = data_dict.get('user') # the user who is being given the new role
new_authgroup_ref = data_dict.get('authorization_group') # the authgroup who is being given the new role
if bool(new_user_ref) == bool(new_authgroup_ref):
raise ParameterError('You must provide either "user" or "authorization_group" parameter.')
domain_object_ref = data_dict['domain_object']
if not isinstance(data_dict['roles'], (list, tuple)):
raise ParameterError('Parameter "%s" must be of type: "%s"' % ('role', 'list'))
desired_roles = set(data_dict['roles'])
if new_user_ref:
user_object = model.User.get(new_user_ref)
if not user_object:
raise NotFound('Cannot find user %r' % new_user_ref)
data_dict['user'] = user_object.id
add_user_to_role_func = model.add_user_to_role
remove_user_from_role_func = model.remove_user_from_role
else:
user_object = model.AuthorizationGroup.get(new_authgroup_ref)
if not user_object:
raise NotFound('Cannot find authorization group %r' % new_authgroup_ref)
data_dict['authorization_group'] = user_object.id
add_user_to_role_func = model.add_authorization_group_to_role
remove_user_from_role_func = model.remove_authorization_group_from_role
domain_object = get_domain_object(model, domain_object_ref)
data_dict['id'] = domain_object.id
if isinstance(domain_object, model.Package):
check_access('package_edit_permissions', context, data_dict)
elif isinstance(domain_object, model.Group):
check_access('group_edit_permissions', context, data_dict)
elif isinstance(domain_object, model.AuthorizationGroup):
check_access('authorization_group_edit_permissions', context, data_dict)
# Todo: 'system' object
else:
raise ParameterError('Not possible to update roles for domain object type %s' % type(domain_object))
# current_uors: in order to avoid either creating a role twice or
# deleting one which is non-existent, we need to get the users\'
# current roles (if any)
current_role_dicts = roles_show(context, data_dict)['roles']
current_roles = set([role_dict['role'] for role_dict in current_role_dicts])
# Whenever our desired state is different from our current state,
# change it.
for role in (desired_roles - current_roles):
add_user_to_role_func(user_object, role, domain_object)
for role in (current_roles - desired_roles):
remove_user_from_role_func(user_object, role, domain_object)
# and finally commit all these changes to the database
if not (current_roles == desired_roles):
model.repo.commit_and_remove()
return roles_show(context, data_dict)
def user_role_bulk_update(context, data_dict):
'''
For a given domain_object, update authz roles that several users have on it.
To delete all roles for a user on a domain object, set {roles: []}.
'''
for user_or_authgroup in ('user', 'authorization_group'):
# Collate all the roles for each user
roles_by_user = {} # user:roles
for user_role_dict in data_dict['user_roles']:
user = user_role_dict.get(user_or_authgroup)
if user:
roles = user_role_dict['roles']
if user not in roles_by_user:
roles_by_user[user] = []
roles_by_user[user].extend(roles)
# For each user, update its roles
for user in roles_by_user:
uro_data_dict = {user_or_authgroup: user,
'roles': roles_by_user[user],
'domain_object': data_dict['domain_object']}
user_role_update(context, uro_data_dict)
return roles_show(context, data_dict)