-
Notifications
You must be signed in to change notification settings - Fork 2k
/
model_dictize.py
365 lines (285 loc) · 13.2 KB
/
model_dictize.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
from pylons import config
from sqlalchemy.sql import select, and_
import datetime
from ckan.lib.dictization import (obj_list_dictize,
obj_dict_dictize,
table_dictize)
from ckan.logic import NotFound
import ckan.misc
from ckan.lib.helpers import json
## package save
def group_list_dictize(obj_list, context,
sort_key=lambda x:x['display_name'], reverse=False):
active = context.get('active', True)
result_list = []
for obj in obj_list:
group_dict = table_dictize(obj, context)
group_dict.pop('created')
if active and obj.state not in ('active', 'pending'):
continue
group_dict['display_name'] = obj.display_name
group_dict['packages'] = len(obj.packages)
result_list.append(group_dict)
return sorted(result_list, key=sort_key, reverse=reverse)
def resource_list_dictize(res_list, context):
active = context.get('active', True)
result_list = []
for res in res_list:
if active and res.state not in ('active', 'pending'):
continue
result_list.append(resource_dictize(res, context))
return sorted(result_list, key=lambda x: x["position"])
def extras_dict_dictize(extras_dict, context):
result_list = []
for name, extra in extras_dict.iteritems():
dictized = table_dictize(extra, context)
if not extra.state == 'active':
continue
value = dictized["value"]
## This is to make sure the frontend does not show a plain string
## as json with brackets.
if not(context.get("extras_as_string") and isinstance(value, basestring)):
dictized["value"] = json.dumps(value)
result_list.append(dictized)
return sorted(result_list, key=lambda x: x["key"])
def extras_list_dictize(extras_list, context):
result_list = []
active = context.get('active', True)
for extra in extras_list:
if active and extra.state not in ('active', 'pending'):
continue
dictized = table_dictize(extra, context)
value = dictized["value"]
if not(context.get("extras_as_string") and isinstance(value, basestring)):
dictized["value"] = json.dumps(value)
result_list.append(dictized)
return sorted(result_list, key=lambda x: x["key"])
def resource_dictize(res, context):
resource = table_dictize(res, context)
extras = resource.pop("extras", None)
if extras:
resource.update(extras)
return resource
def _execute_with_revision(q, rev_table, context):
'''
Raises NotFound if the context['revision_id'] does not exist.
Returns [] if there are no results.
'''
model = context['model']
meta = model.meta
session = model.Session
revision_id = context.get('revision_id')
revision_date = context.get('revision_date')
pending = context.get('pending')
if revision_id:
revision = session.query(context['model'].Revision).filter_by(
id=revision_id).first()
if not revision:
raise NotFound
revision_date = revision.timestamp
if revision_date:
q = q.where(rev_table.c.revision_timestamp <= revision_date)
q = q.where(rev_table.c.expired_timestamp > revision_date)
elif pending:
q = q.where(rev_table.c.expired_timestamp == datetime.datetime(9999, 12, 31))
else:
q = q.where(rev_table.c.current == True)
return session.execute(q)
def package_dictize(pkg, context):
model = context['model']
#package
package_rev = model.package_revision_table
q = select([package_rev]).where(package_rev.c.id == pkg.id)
result = _execute_with_revision(q, package_rev, context).first()
if not result:
raise NotFound
result_dict = table_dictize(result, context)
#resources
res_rev = model.resource_revision_table
resource_group = model.resource_group_table
q = select([res_rev], from_obj = res_rev.join(resource_group,
resource_group.c.id == res_rev.c.resource_group_id))
q = q.where(resource_group.c.package_id == pkg.id)
result = _execute_with_revision(q, res_rev, context)
result_dict["resources"] = resource_list_dictize(result, context)
#tags
tag_rev = model.package_tag_revision_table
tag = model.tag_table
q = select([tag, tag_rev.c.state, tag_rev.c.revision_timestamp],
from_obj=tag_rev.join(tag, tag.c.id == tag_rev.c.tag_id)
).where(tag_rev.c.package_id == pkg.id)
result = _execute_with_revision(q, tag_rev, context)
result_dict["tags"] = obj_list_dictize(result, context, lambda x: x["name"])
#extras
extra_rev = model.extra_revision_table
q = select([extra_rev]).where(extra_rev.c.package_id == pkg.id)
result = _execute_with_revision(q, extra_rev, context)
result_dict["extras"] = extras_list_dictize(result, context)
#groups
group_rev = model.package_group_revision_table
group = model.group_table
q = select([group],
from_obj=group_rev.join(group, group.c.id == group_rev.c.group_id)
).where(group_rev.c.package_id == pkg.id)
result = _execute_with_revision(q, group_rev, context)
result_dict["groups"] = obj_list_dictize(result, context)
#relations
rel_rev = model.package_relationship_revision_table
q = select([rel_rev]).where(rel_rev.c.subject_package_id == pkg.id)
result = _execute_with_revision(q, rel_rev, context)
result_dict["relationships_as_subject"] = obj_list_dictize(result, context)
q = select([rel_rev]).where(rel_rev.c.object_package_id == pkg.id)
result = _execute_with_revision(q, rel_rev, context)
result_dict["relationships_as_object"] = obj_list_dictize(result, context)
#isopen
# Get an actual Package object, not a PackageRevision
pkg_object = model.Package.get(pkg.id)
result_dict['isopen'] = pkg_object.isopen if isinstance(pkg_object.isopen,bool) else pkg_object.isopen()
return result_dict
def group_dictize(group, context):
result_dict = table_dictize(group, context)
result_dict['display_name'] = group.display_name
result_dict['extras'] = extras_dict_dictize(
group._extras, context)
result_dict['packages'] = obj_list_dictize(
group.packages, context)
return result_dict
def tag_dictize(tag, context):
result_dict = table_dictize(tag, context)
result_dict["packages"] = obj_list_dictize(
tag.packages_ordered, context)
return result_dict
def user_dictize(user, context):
result_dict = table_dictize(user, context)
del result_dict['password']
result_dict['display_name'] = user.display_name
result_dict['email_hash'] = user.email_hash
result_dict['number_of_edits'] = user.number_of_edits()
result_dict['number_administered_packages'] = user.number_administered_packages()
return result_dict
def task_status_dictize(task_status, context):
return table_dictize(task_status, context)
## conversion to api
def group_to_api1(group, context):
dictized = group_dictize(group, context)
dictized["extras"] = dict((extra["key"], json.loads(extra["value"]))
for extra in dictized["extras"])
dictized["packages"] = sorted([package["name"] for package in dictized["packages"]])
return dictized
def group_to_api2(group, context):
dictized = group_dictize(group, context)
dictized["extras"] = dict((extra["key"], json.loads(extra["value"]))
for extra in dictized["extras"])
dictized["packages"] = sorted([package["id"] for package in dictized["packages"]])
return dictized
def tag_to_api1(tag, context):
dictized = tag_dictize(tag, context)
return sorted([package["name"] for package in dictized["packages"]])
def tag_to_api2(tag, context):
dictized = tag_dictize(tag, context)
return sorted([package["id"] for package in dictized["packages"]])
def resource_dict_to_api(res_dict, package_id, context):
res_dict.pop("revision_id")
res_dict.pop("state")
res_dict.pop("revision_timestamp")
res_dict["package_id"] = package_id
def package_to_api1(pkg, context):
dictized = package_dictize(pkg, context)
dictized.pop("revision_timestamp")
dictized["groups"] = [group["name"] for group in dictized["groups"]]
dictized["tags"] = [tag["name"] for tag in dictized["tags"]]
dictized["extras"] = dict((extra["key"], json.loads(extra["value"]))
for extra in dictized["extras"])
dictized['notes_rendered'] = ckan.misc.MarkdownFormat().to_html(pkg.notes)
resources = dictized["resources"]
for resource in resources:
resource_dict_to_api(resource, pkg.id, context)
if pkg.resources:
dictized['download_url'] = pkg.resources[0].url
dictized['license'] = pkg.license.title if pkg.license else None
dictized['ratings_average'] = pkg.get_average_rating()
dictized['ratings_count'] = len(pkg.ratings)
site_url = config.get('ckan.site_url', None)
if site_url:
dictized['ckan_url'] = '%s/dataset/%s' % (site_url, pkg.name)
dictized['metadata_modified'] = pkg.metadata_modified.isoformat() \
if pkg.metadata_modified else None
dictized['metadata_created'] = pkg.metadata_created.isoformat() \
if pkg.metadata_created else None
subjects = dictized.pop("relationships_as_subject")
objects = dictized.pop("relationships_as_object")
relationships = []
for relationship in objects:
model = context['model']
swap_types = model.PackageRelationship.forward_to_reverse_type
type = swap_types(relationship['type'])
relationships.append({'subject': pkg.get(relationship['object_package_id']).name,
'type': type,
'object': pkg.get(relationship['subject_package_id']).name,
'comment': relationship["comment"]})
for relationship in subjects:
model = context['model']
relationships.append({'subject': pkg.get(relationship['subject_package_id']).name,
'type': relationship['type'],
'object': pkg.get(relationship['object_package_id']).name,
'comment': relationship["comment"]})
dictized['relationships'] = relationships
return dictized
def package_to_api2(pkg, context):
dictized = package_dictize(pkg, context)
dictized["groups"] = [group["id"] for group in dictized["groups"]]
dictized.pop("revision_timestamp")
dictized["tags"] = [tag["name"] for tag in dictized["tags"]]
dictized["extras"] = dict((extra["key"], json.loads(extra["value"]))
for extra in dictized["extras"])
resources = dictized["resources"]
for resource in resources:
resource_dict_to_api(resource,pkg.id, context)
dictized['license'] = pkg.license.title if pkg.license else None
dictized['ratings_average'] = pkg.get_average_rating()
dictized['ratings_count'] = len(pkg.ratings)
site_url = config.get('ckan.site_url', None)
if site_url:
dictized['ckan_url'] = '%s/dataset/%s' % (site_url, pkg.name)
dictized['metadata_modified'] = pkg.metadata_modified.isoformat() \
if pkg.metadata_modified else None
dictized['metadata_created'] = pkg.metadata_created.isoformat() \
if pkg.metadata_created else None
dictized['notes_rendered'] = ckan.misc.MarkdownFormat().to_html(pkg.notes)
subjects = dictized.pop("relationships_as_subject")
objects = dictized.pop("relationships_as_object")
relationships = []
for relationship in objects:
model = context['model']
swap_types = model.PackageRelationship.forward_to_reverse_type
type = swap_types(relationship['type'])
relationships.append({'subject': relationship['object_package_id'],
'type': type,
'object': relationship['subject_package_id'],
'comment': relationship["comment"]})
for relationship in subjects:
model = context['model']
relationships.append({'subject': relationship['subject_package_id'],
'type': relationship['type'],
'object': relationship['object_package_id'],
'comment': relationship["comment"]})
dictized['relationships'] = relationships
return dictized
def activity_dictize(activity, context):
activity_dict = table_dictize(activity, context)
return activity_dict
def activity_list_dictize(activity_list, context):
activity_dicts = []
for activity in activity_list:
activity_dict = activity_dictize(activity, context)
activity_dicts.append(activity_dict)
return activity_dicts
def activity_detail_dictize(activity_detail, context):
return table_dictize(activity_detail, context)
def activity_detail_list_dictize(activity_detail_list, context):
activity_detail_dicts = []
for activity_detail in activity_detail_list:
activity_detail_dict = activity_detail_dictize(activity_detail,
context)
activity_detail_dicts.append(activity_detail_dict)
return activity_detail_dicts