diff --git a/ckan/model/group.py b/ckan/model/group.py index 6636236f921..89eb7d513e7 100644 --- a/ckan/model/group.py +++ b/ckan/model/group.py @@ -21,7 +21,7 @@ Column('capacity', UnicodeText, nullable=False), Column('group_id', UnicodeText, ForeignKey('group.id')), ) - + vdm.sqlalchemy.make_table_stateful(member_table) member_revision_table = make_revisioned_table(member_table) @@ -50,7 +50,7 @@ def __init__(self, group=None, table_id=None, group_id=None, 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.''' @@ -59,17 +59,17 @@ def get(cls, reference): if member == 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 + + 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 Session.query(Package).filter_by(id=self.table_id).all() @@ -77,8 +77,8 @@ def related_packages(self): class Group(vdm.sqlalchemy.RevisionedObjectMixin, vdm.sqlalchemy.StatefulObjectMixin, DomainObject): - - def __init__(self, name=u'', title=u'', description=u'', + + def __init__(self, name=u'', title=u'', description=u'', type=u'group', approval_status=u'approved' ): self.name = name self.title = title @@ -106,8 +106,8 @@ def get(cls, reference): 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 + 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"] @@ -115,19 +115,24 @@ def set_approval_status(self, status): if status == "denied": pass - def members_of_type(self, object_type): + def members_of_type(self, object_type, capacity=None): object_type_string = object_type.__name__.lower() query = Session.query(object_type).\ filter_by(state=vdm.sqlalchemy.State.ACTIVE).\ filter(group_table.c.id == self.id).\ filter(member_table.c.state == 'active').\ - filter(member_table.c.table_name == object_type_string).\ - join(member_table, member_table.c.table_id == getattr(object_type,'id') ).\ + filter(member_table.c.table_name == object_type_string) + + if capacity: + query = query.filter(member_table.c.capacity == capacity) + + query = query.join(member_table, member_table.c.table_id == getattr(object_type,'id') ).\ join(group_table, group_table.c.id == member_table.c.group_id) + return query def add_child(self, object_instance): - object_type_string = object_instance.__class__.__name__.lower() + object_type_string = object_instance.__class__.__name__.lower() if not object_instance in self.members_of_type(object_instance.__class__).all(): member = Member(group=self, table_id=getattr(object_instance,'id'), table_name=object_type_string) Session.add(member) @@ -149,7 +154,7 @@ def search_by_name(cls, text_query): def as_dict(self, ref_package_by='name'): _dict = DomainObject.as_dict(self) - _dict['packages'] = [getattr(package, ref_package_by) for package in self.packages] + _dict['packages'] = [getattr(package, ref_package_by) for package in self.packages] _dict['extras'] = dict([(key, value) for key, value in self.extras.items()]) if ( self.type == 'publisher' ): _dict['users'] = [getattr(user, "name") for user in self.members_of_type(User)] @@ -175,9 +180,9 @@ def get_groups(self, group_type=None, capacity=None): filter(model.Member.table_id == self.id).all() groups = self._groups - if group_type: + if group_type: groups = [g for g in groups if g.type == group_type] - if capacity: + if capacity: groups = [g for g in groups if g.capacity == capacity] return groups @@ -210,7 +215,7 @@ def __repr__(self): return '' % self.name -mapper(Group, group_table, +mapper(Group, group_table, extension=[vdm.sqlalchemy.Revisioner(group_revision_table),], )