Skip to content

Commit

Permalink
[xs] Minor tweak to how group members are accessed by type in Group m…
Browse files Browse the repository at this point in the history
…odel
  • Loading branch information
rossjones committed Feb 13, 2012
1 parent a608d92 commit 9d808a9
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions ckan/model/group.py
Expand Up @@ -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)

Expand Down Expand Up @@ -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.'''
Expand All @@ -59,26 +59,26 @@ 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()

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
Expand Down Expand Up @@ -106,28 +106,33 @@ 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"]
self.approval_status = 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)
Expand All @@ -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)]
Expand All @@ -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

Expand Down Expand Up @@ -210,7 +215,7 @@ def __repr__(self):
return '<Group %s>' % self.name


mapper(Group, group_table,
mapper(Group, group_table,
extension=[vdm.sqlalchemy.Revisioner(group_revision_table),],
)

Expand Down

0 comments on commit 9d808a9

Please sign in to comment.