-
Notifications
You must be signed in to change notification settings - Fork 2k
/
create.py
333 lines (253 loc) · 10.6 KB
/
create.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
import logging
import ckan.rating as ratings
from ckan.plugins import (PluginImplementations,
IGroupController,
IPackageController)
from ckan.logic import NotFound, ValidationError
from ckan.logic import check_access
from ckan.lib.base import _
import ckan.lib.dictization
from ckan.lib.dictization.model_dictize import (package_to_api1,
package_to_api2,
group_to_api1,
group_to_api2)
from ckan.lib.dictization.model_save import (group_api_to_dict,
group_dict_save,
package_api_to_dict,
package_dict_save,
user_dict_save,
activity_dict_save)
from ckan.lib.dictization.model_dictize import (group_dictize,
package_dictize,
user_dictize,
activity_dictize)
from ckan.logic.schema import default_create_package_schema, default_resource_schema
from ckan.logic.schema import default_group_schema, default_user_schema
from ckan.lib.navl.dictization_functions import validate
from ckan.logic.action.update import (_update_package_relationship,
package_error_summary,
group_error_summary)
log = logging.getLogger(__name__)
def package_create(context, data_dict):
model = context['model']
user = context['user']
schema = context.get('schema') or default_create_package_schema()
model.Session.remove()
model.Session()._context = context
check_access('package_create',context,data_dict)
data, errors = validate(data_dict, schema, context)
if errors:
model.Session.rollback()
raise ValidationError(errors, package_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: Create object %s') % data.get("name")
pkg = package_dict_save(data, context)
admins = []
if user:
admins = [model.User.by_name(user.decode('utf8'))]
model.setup_default_user_roles(pkg, admins)
# Needed to let extensions know the package id
model.Session.flush()
for item in PluginImplementations(IPackageController):
item.create(pkg)
if not context.get('defer_commit'):
model.repo.commit()
## need to let rest api create
context["package"] = pkg
## this is added so that the rest controller can make a new location
context["id"] = pkg.id
log.debug('Created object %s' % str(pkg.name))
return package_dictize(pkg, context)
def package_create_validate(context, data_dict):
model = context['model']
user = context['user']
schema = context.get('schema') or default_create_package_schema()
model.Session.remove()
model.Session()._context = context
check_access('package_create',context,data_dict)
data, errors = validate(data_dict, schema, context)
if errors:
model.Session.rollback()
raise ValidationError(errors, package_error_summary(errors))
else:
return data
def resource_create(context, data_dict):
#TODO This doesn't actually do anything
model = context['model']
user = context['user']
data, errors = validate(data_dict,
default_resource_schema(),
context)
def package_relationship_create(context, data_dict):
model = context['model']
user = context['user']
id = data_dict["id"]
id2 = data_dict["id2"]
rel_type = data_dict["rel"]
api = context.get('api_version') or '1'
ref_package_by = 'id' if api == '2' else 'name'
# Create a Package Relationship.
pkg1 = model.Package.get(id)
pkg2 = model.Package.get(id2)
if not pkg1:
raise NotFound('First package named in address was not found.')
if not pkg2:
return NotFound('Second package named in address was not found.')
check_access('package_relationship_create', context, data_dict)
##FIXME should have schema
comment = data_dict.get('comment', u'')
existing_rels = pkg1.get_relationships_with(pkg2, rel_type)
if existing_rels:
return _update_package_relationship(existing_rels[0],
comment, context)
rev = model.repo.new_revision()
rev.author = user
rev.message = _(u'REST API: Create package relationship: %s %s %s') % (pkg1, rel_type, pkg2)
rel = pkg1.add_relationship(rel_type, pkg2, comment=comment)
if not context.get('defer_commit'):
model.repo.commit_and_remove()
relationship_dicts = rel.as_dict(ref_package_by=ref_package_by)
return relationship_dicts
def group_create(context, data_dict):
model = context['model']
user = context['user']
schema = context.get('schema') or default_group_schema()
check_access('group_create',context,data_dict)
data, errors = validate(data_dict, schema, context)
if errors:
model.Session.rollback()
raise ValidationError(errors, group_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: Create object %s') % data.get("name")
group = group_dict_save(data, context)
if user:
admins = [model.User.by_name(user.decode('utf8'))]
else:
admins = []
model.setup_default_user_roles(group, admins)
# Needed to let extensions know the group id
model.Session.flush()
for item in PluginImplementations(IGroupController):
item.create(group)
activity_dict = {
'user_id': model.User.by_name(user.decode('utf8')).id,
'object_id': group.id,
'activity_type': 'new group',
}
activity_dict['data'] = {
'group': ckan.lib.dictization.table_dictize(group, context)
}
activity_create(context, activity_dict)
if not context.get('defer_commit'):
model.repo.commit()
context["group"] = group
context["id"] = group.id
log.debug('Created object %s' % str(group.name))
return group_dictize(group, context)
def rating_create(context, data_dict):
model = context['model']
user = context.get("user")
package_ref = data_dict.get('package')
rating = data_dict.get('rating')
opts_err = None
if not package_ref:
opts_err = _('You must supply a package id or name (parameter "package").')
elif not rating:
opts_err = _('You must supply a rating (parameter "rating").')
else:
try:
rating_int = int(rating)
except ValueError:
opts_err = _('Rating must be an integer value.')
else:
package = model.Package.get(package_ref)
if rating < ratings.MIN_RATING or rating > ratings.MAX_RATING:
opts_err = _('Rating must be between %i and %i.') % (ratings.MIN_RATING, ratings.MAX_RATING)
elif not package:
opts_err = _('Package with name %r does not exist.') % package_ref
if opts_err:
raise ValidationError(opts_err)
user = model.User.by_name(user)
ratings.set_rating(user, package, rating_int)
package = model.Package.get(package_ref)
ret_dict = {'rating average':package.get_average_rating(),
'rating count': len(package.ratings)}
return ret_dict
def user_create(context, data_dict):
'''Creates a new user'''
model = context['model']
schema = context.get('schema') or default_user_schema()
check_access('user_create', context, data_dict)
data, errors = validate(data_dict, schema, context)
if errors:
model.Session.rollback()
raise ValidationError(errors, group_error_summary(errors))
user = user_dict_save(data, context)
if not context.get('defer_commit'):
model.repo.commit()
activity_dict = {
'user_id': user.id,
'object_id': user.id,
'activity_type': 'new user',
}
activity_create(context, activity_dict)
context['user'] = user
context['id'] = user.id
log.debug('Created user %s' % str(user.name))
return user_dictize(user, context)
## Modifications for rest api
def package_create_rest(context, data_dict):
api = context.get('api_version') or '1'
check_access('package_create_rest', context, data_dict)
dictized_package = package_api_to_dict(data_dict, context)
dictized_after = package_create(context, dictized_package)
pkg = context['package']
if api == '1':
package_dict = package_to_api1(pkg, context)
else:
package_dict = package_to_api2(pkg, context)
data_dict['id'] = pkg.id
return package_dict
def group_create_rest(context, data_dict):
api = context.get('api_version') or '1'
check_access('group_create_rest', context, data_dict)
dictized_group = group_api_to_dict(data_dict, context)
dictized_after = group_create(context, dictized_group)
group = context['group']
if api == '1':
group_dict = group_to_api1(group, context)
else:
group_dict = group_to_api2(group, context)
data_dict['id'] = group.id
return group_dict
def activity_create(context, activity_dict):
'''Create an activity stream event and add it to the model. Return a
dictionary representation of the new event.
This function doesn't do any authorisation or validation yet, it's only
meant to be used internally and not yet ready to be exposed e.g. via the
API.
Arguments:
context -- Pylons context dict with at least the 'model' key.
activity_dict -- A dict containing the values for the activity stream event
to be created. Should have keys 'user_id', 'object_id', 'activity_type' and
(optionally) 'data'.
'''
model = context['model']
assert not activity_dict.has_key('revision_id')
if getattr(model.Session, "revision", None):
activity_dict['revision_id'] = model.Session.revision.id
else:
activity_dict['revision_id'] = None
activity = activity_dict_save(activity_dict, context)
if not context.get('defer_commit'):
model.repo.commit()
log.debug("Created '%s' activity" % activity.activity_type)
return activity_dictize(activity, context)