Skip to content

Commit

Permalink
Move group_package_show SQLAlchemy into model
Browse files Browse the repository at this point in the history
Move the SQLAlchemy query that the group_package_show() action function
uses into the model. I need this for architectural reasons for upcoming
commits, and we're supposed to encapsulate SQLAlchemy in the model
anyway.

I made the new model function support the 'return_query' option but note
that there are no tests covering this, all the tests pass even without
this option.
  • Loading branch information
Sean Hammond authored and tobes committed Nov 27, 2012
1 parent 0235f1a commit 5847bd0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
28 changes: 8 additions & 20 deletions ckan/logic/action/get.py
Expand Up @@ -750,34 +750,22 @@ def group_package_show(context, data_dict):
:rtype: list of dictionaries
'''
model = context["model"]
user = context["user"]
id = _get_or_bust(data_dict, 'id')
limit = data_dict.get("limit")
model = context['model']
group_id = _get_or_bust(data_dict, 'id')

group = model.Group.get(id)
# FIXME: What if limit is not an int? Schema and validation needed.
limit = data_dict.get('limit')

group = model.Group.get(group_id)
context['group'] = group
if group is None:
raise NotFound

_check_access('group_show', context, data_dict)

query = model.Session.query(model.PackageRevision)\
.filter(model.PackageRevision.state=='active')\
.filter(model.PackageRevision.current==True)\
.join(model.Member, model.Member.table_id==model.PackageRevision.id)\
.join(model.Group, model.Group.id==model.Member.group_id)\
.filter_by(id=group.id)\
.order_by(model.PackageRevision.name)

if limit:
query = query.limit(limit)

if context.get('return_query'):
return query

result = []
for pkg_rev in query.all():
for pkg_rev in group.get_package_revisions(limit=limit,
return_query=context.get('return_query')):
result.append(model_dictize.package_dictize(pkg_rev, context))

return result
Expand Down
27 changes: 27 additions & 0 deletions ckan/model/group.py
Expand Up @@ -199,6 +199,33 @@ def active_packages(self, load_eager=True, with_private=False):

return query

def get_package_revisions(self, limit=None, return_query=False):
'''Return a group's packages.
:param limit: the maximum number of packages to return
:type limit: int
:returns: a list of the group's packages, sorted by name
:rtype: list of PackageRevision objects
'''
import ckan.model as model
q = model.Session.query(model.PackageRevision)
q = q.filter(model.PackageRevision.state == 'active')
q = q.filter(model.PackageRevision.current == True)
q = q.join(model.Member,
model.Member.table_id == model.PackageRevision.id)
q = q.join(model.Group, model.Group.id == model.Member.group_id)
q = q.filter_by(id=self.id)
q = q.order_by(model.PackageRevision.name)
if limit is not None:
q = q.limit(limit)
if return_query:
return q
else:
return q.all()


@classmethod
def search_by_name_or_title(cls, text_query, group_type=None):
text_query = text_query.strip().lower()
Expand Down

0 comments on commit 5847bd0

Please sign in to comment.