-
Notifications
You must be signed in to change notification settings - Fork 2k
/
group.py
420 lines (359 loc) · 16.2 KB
/
group.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
import datetime
from sqlalchemy import orm, types, Column, Table, ForeignKey, or_
import vdm.sqlalchemy
import meta
import core
import package as _package
import types as _types
import domain_object
import user as _user
__all__ = ['group_table', 'Group',
'Member', 'GroupRevision', 'MemberRevision',
'member_revision_table', 'member_table']
member_table = Table('member', meta.metadata,
Column('id', types.UnicodeText,
primary_key=True,
default=_types.make_uuid),
Column('table_name', types.UnicodeText,
nullable=False),
Column('table_id', types.UnicodeText,
nullable=False),
Column('capacity', types.UnicodeText,
nullable=False),
Column('group_id', types.UnicodeText,
ForeignKey('group.id')),)
vdm.sqlalchemy.make_table_stateful(member_table)
member_revision_table = core.make_revisioned_table(member_table)
group_table = Table('group', meta.metadata,
Column('id', types.UnicodeText,
primary_key=True,
default=_types.make_uuid),
Column('name', types.UnicodeText,
nullable=False, unique=True),
Column('title', types.UnicodeText),
Column('type', types.UnicodeText,
nullable=False),
Column('description', types.UnicodeText),
Column('image_url', types.UnicodeText),
Column('created', types.DateTime,
default=datetime.datetime.now),
Column('is_organization', types.Boolean, default=False),
Column('approval_status', types.UnicodeText,
default=u"approved"))
vdm.sqlalchemy.make_table_stateful(group_table)
group_revision_table = core.make_revisioned_table(group_table)
class Member(vdm.sqlalchemy.RevisionedObjectMixin,
vdm.sqlalchemy.StatefulObjectMixin,
domain_object.DomainObject):
'''A Member object represents any other object being a 'member' of a
particular Group.
Meanings:
* Package - the Group is a collection of Packages
- capacity is 'public', 'private'
or 'organization' if the Group is an Organization
(see ckan.logic.action.package_owner_org_update)
* User - the User is granted permissions for the Group
- capacity is 'admin', 'editor' or 'member'
* Group - the Group (Member.group_id) is a child of the Group (Member.id)
in a hierarchy.
'''
def __init__(self, group=None, table_id=None, group_id=None,
table_name=None, capacity='public', state='active'):
self.group = group
self.group_id = group_id
self.table_id = table_id
self.table_name = table_name
self.capacity = capacity
self.state = state
@classmethod
def get(cls, reference):
'''Returns a group object referenced by its id or name.'''
query = meta.Session.query(cls).filter(cls.id == reference)
member = query.first()
if member is None:
member = cls.by_name(reference)
return member
def get_related(self, type):
""" TODO: Determine if this is useful
Get all objects that are members of the group of the specified
type.
Should the type be used to get table_name or should we use the
one in the constructor
"""
pass
def related_packages(self):
# TODO do we want to return all related packages or certain ones?
return meta.Session.query(_package.Package).filter_by(
id=self.table_id).all()
def __unicode__(self):
# refer to objects by name, not ID, to help debugging
if self.table_name == 'package':
table_info = 'package=%s' % meta.Session.query(_package.Package).\
get(self.table_id).name
elif self.table_name == 'group':
table_info = 'group=%s' % meta.Session.query(Group).\
get(self.table_id).name
else:
table_info = 'table_name=%s table_id=%s' % (self.table_name,
self.table_id)
return u'<Member group=%s %s capacity=%s state=%s>' % \
(self.group.name if self.group else repr(self.group),
table_info, self.capacity, self.state)
class Group(vdm.sqlalchemy.RevisionedObjectMixin,
vdm.sqlalchemy.StatefulObjectMixin,
domain_object.DomainObject):
def __init__(self, name=u'', title=u'', description=u'', image_url=u'',
type=u'group', approval_status=u'approved'):
self.name = name
self.title = title
self.description = description
self.image_url = image_url
self.type = type
self.approval_status = approval_status
@property
def display_name(self):
if self.title is not None and len(self.title):
return self.title
else:
return self.name
@classmethod
def get(cls, reference):
'''Returns a group object referenced by its id or name.'''
query = meta.Session.query(cls).filter(cls.id == reference)
group = query.first()
if group is None:
group = cls.by_name(reference)
return group
# Todo: Make sure group names can't be changed to look like group IDs?
@classmethod
def all(cls, group_type=None, state=('active',)):
"""
Returns all groups.
"""
q = meta.Session.query(cls)
if state:
q = q.filter(cls.state.in_(state))
if group_type:
q = q.filter(cls.type == group_type)
return q.order_by(cls.title)
def set_approval_status(self, status):
"""
Aproval status can be set on a group, where currently it does
nothing other than act as an indication of whether it was
approved or not. It may be that we want to tie the object
status to the approval status
"""
assert status in ["approved", "pending", "denied"]
self.approval_status = status
if status == "denied":
pass
def get_children_groups(self, type='group'):
'''Returns the groups one level underneath this group in the hierarchy.
Groups come in a list of dicts, each keyed by "id", "name" and "title".
'''
# The original intention of this method was to provide the full depth
# of the tree, but the CTE was incorrect. This new query does what that
# old CTE actually did, but is now far simpler.
results = meta.Session.query(Group.id, Group.name, Group.title).\
filter_by(type=type).\
filter_by(state='active').\
join(Member, Member.table_id == Group.id).\
filter_by(group=self).\
filter_by(table_name='group').\
filter_by(state='active').\
all()
return [{'id': id_, 'name': name, 'title': title}
for id_, name, title in results]
def get_children_group_hierarchy(self, type='group'):
'''Returns the groups in all levels underneath this group in the
hierarchy. The ordering is such that children always come after their
parent.
:rtype: a list of tuples, each one a Group and the ID its their parent
group.
e.g. >>> dept-health.get_children_group_hierarchy()
[(<Group nhs>, u'8a163ba7-5146-4325-90c8-fe53b25e28d0'),
(<Group wirral-ccg>, u'06e6dbf5-d801-40a1-9dc0-6785340b2ab4'),
(<Group newport-ccg>, u'd2e25b41-720c-4ba7-bc8f-bb34b185b3dd')]
'''
results = meta.Session.query(Group, 'parent_id').\
from_statement(HIERARCHY_DOWNWARDS_CTE).\
params(id=self.id, type=type).all()
return results
def get_parent_group_hierarchy(self, type='group'):
'''Returns this group's parent, parent's parent, parent's parent's
parent etc.. Sorted with the top level parent first.'''
return meta.Session.query(Group).\
from_statement(HIERARCHY_UPWARDS_CTE).\
params(id=self.id, type=type).all()
@classmethod
def get_top_level_groups(cls, type='group'):
'''Returns a list of the groups (of the specified type) which have
no parent groups. Groups are sorted by title.
'''
return meta.Session.query(cls).\
outerjoin(Member, Member.table_id == Group.id and
Member.table_name == 'group' and
Member.state == 'active').\
filter(Member.id == None).\
filter(Group.type == type).\
order_by(Group.title).all()
def packages(self, with_private=False, limit=None,
return_query=False, context=None):
'''Return this group's active and pending packages.
Returns all packages in this group with VDM revision state ACTIVE or
PENDING.
:param with_private: if True, include the group's private packages
:type with_private: boolean
:param limit: the maximum number of packages to return
:type limit: int
:param return_query: if True, return the SQLAlchemy query object
instead of the list of Packages resulting from the query
:type return_query: boolean
:returns: a list of this group's packages
:rtype: list of ckan.model.package.Package objects
'''
user_is_org_member = False
context = context or {}
user_is_admin = context.get('user_is_admin', False)
user_id = context.get('user_id')
if user_is_admin:
user_is_org_member = True
elif self.is_organization and user_id:
query = meta.Session.query(Member) \
.filter(Member.state == 'active') \
.filter(Member.table_name == 'user') \
.filter(Member.group_id == self.id) \
.filter(Member.table_id == user_id)
user_is_org_member = len(query.all()) != 0
query = meta.Session.query(_package.Package).\
filter(
or_(_package.Package.state == vdm.sqlalchemy.State.ACTIVE,
_package.Package.state == vdm.sqlalchemy.State.PENDING)). \
filter(group_table.c.id == self.id).\
filter(member_table.c.state == 'active')
# orgs do not show private datasets unless the user is a member
if self.is_organization and not user_is_org_member:
query = query.filter(_package.Package.private == False)
# groups (not orgs) never show private datasets
if not self.is_organization:
query = query.filter(_package.Package.private == False)
query = query.join(member_table,
member_table.c.table_id == _package.Package.id)
query = query.join(group_table,
group_table.c.id == member_table.c.group_id)
if limit is not None:
query = query.limit(limit)
if return_query:
return query
else:
return query.all()
@classmethod
def search_by_name_or_title(cls, text_query, group_type=None, is_org=False):
text_query = text_query.strip().lower()
q = meta.Session.query(cls) \
.filter(or_(cls.name.contains(text_query),
cls.title.ilike('%' + text_query + '%')))
if is_org:
q = q.filter(cls.type == 'organization')
else:
q = q.filter(cls.type != 'organization')
if group_type:
q = q.filter(cls.type == group_type)
q = q.filter(cls.state == 'active')
return q.order_by(cls.title)
def add_package_by_name(self, package_name):
if not package_name:
return
package = _package.Package.by_name(package_name)
assert package
if not package in self.packages():
member = Member(group=self, table_id=package.id,
table_name='package')
meta.Session.add(member)
def get_groups(self, group_type=None, capacity=None):
""" Get all groups that this group is within """
import ckan.model as model
# DR: Why is this cached? Surely the members can change in the
# lifetime of this Group?
if '_groups' not in self.__dict__:
self._groups = meta.Session.query(model.Group).\
join(model.Member, model.Member.group_id == model.Group.id and
model.Member.table_name == 'group').\
filter(model.Member.state == 'active').\
filter(model.Member.table_id == self.id).all()
groups = self._groups
if group_type:
groups = [g for g in groups if g.type == group_type]
if capacity:
groups = [g for g in groups if g.capacity == capacity]
return groups
@property
def all_related_revisions(self):
'''Returns chronological list of all object revisions related to
this group. Ordered by most recent first.
'''
results = {}
from group_extra import GroupExtra
for grp_rev in self.all_revisions:
if not grp_rev.revision in results:
results[grp_rev.revision] = []
results[grp_rev.revision].append(grp_rev)
for class_ in [Member, GroupExtra]:
rev_class = class_.__revision_class__
obj_revisions = meta.Session.query(rev_class).\
filter_by(group_id=self.id).all()
for obj_rev in obj_revisions:
if not obj_rev.revision in results:
results[obj_rev.revision] = []
results[obj_rev.revision].append(obj_rev)
result_list = results.items()
ourcmp = lambda rev_tuple1, rev_tuple2: \
cmp(rev_tuple2[0].timestamp, rev_tuple1[0].timestamp)
return sorted(result_list, cmp=ourcmp)
def __repr__(self):
return '<Group %s>' % self.name
meta.mapper(Group, group_table,
extension=[vdm.sqlalchemy.Revisioner(group_revision_table), ], )
vdm.sqlalchemy.modify_base_object_mapper(Group, core.Revision, core.State)
GroupRevision = vdm.sqlalchemy.create_object_version(meta.mapper, Group,
group_revision_table)
meta.mapper(Member, member_table, properties={
'group': orm.relation(Group,
backref=orm.backref('member_all',
cascade='all, delete-orphan')),
},
extension=[vdm.sqlalchemy.Revisioner(member_revision_table), ],
)
vdm.sqlalchemy.modify_base_object_mapper(Member, core.Revision, core.State)
MemberRevision = vdm.sqlalchemy.create_object_version(meta.mapper, Member,
member_revision_table)
#TODO
MemberRevision.related_packages = lambda self: [self.continuity.package]
HIERARCHY_DOWNWARDS_CTE = """WITH RECURSIVE child AS
(
-- non-recursive term
SELECT * FROM member
WHERE group_id = :id AND table_name = 'group' AND state = 'active'
UNION ALL
-- recursive term
SELECT m.* FROM member AS m, child AS c
WHERE m.group_id = c.table_id AND m.table_name = 'group'
AND m.state = 'active'
)
SELECT G.*, child.group_id as parent_id FROM child
INNER JOIN public.group G ON G.id = child.table_id
WHERE G.type = :type AND G.state='active';"""
HIERARCHY_UPWARDS_CTE = """WITH RECURSIVE parenttree(depth) AS (
-- non-recursive term
SELECT 0, M.* FROM public.member AS M
WHERE table_id = :id AND M.table_name = 'group' AND M.state = 'active'
UNION
-- recursive term
SELECT PG.depth + 1, M.* FROM parenttree PG, public.member M
WHERE PG.group_id = M.table_id AND M.table_name = 'group'
AND M.state = 'active'
)
SELECT G.*, PT.depth FROM parenttree AS PT
INNER JOIN public.group G ON G.id = PT.group_id
WHERE G.type = :type AND G.state='active'
ORDER BY PT.depth DESC;"""