Skip to content

Commit

Permalink
[#1531] fix so capacities get output on dictization
Browse files Browse the repository at this point in the history
  • Loading branch information
kindly committed Jan 3, 2012
1 parent 2e116fd commit b6971f9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
11 changes: 9 additions & 2 deletions ckan/lib/dictization/__init__.py
Expand Up @@ -9,7 +9,7 @@
# these functions. Copy code from here as needed.


def table_dictize(obj, context):
def table_dictize(obj, context, **kw):
'''Get any model object and represent it as a dict'''

result_dict = {}
Expand Down Expand Up @@ -44,6 +44,8 @@ def table_dictize(obj, context):
else:
result_dict[name] = unicode(value)

result_dict.update(kw)

return result_dict


Expand All @@ -54,9 +56,14 @@ def obj_list_dictize(obj_list, context, sort_key=lambda x:x):
active = context.get('active', True)

for obj in obj_list:
if context.get('with_capacity'):
obj, capacity = obj
dictized = table_dictize(obj, context, capacity=capacity)
else:
dictized = table_dictize(obj, context)
if active and obj.state not in ('active', 'pending'):
continue
result_list.append(table_dictize(obj, context))
result_list.append(dictized)

return sorted(result_list, key=sort_key)

Expand Down
24 changes: 20 additions & 4 deletions ckan/lib/dictization/model_dictize.py
Expand Up @@ -19,7 +19,11 @@ def group_list_dictize(obj_list, context,
result_list = []

for obj in obj_list:
group_dict = table_dictize(obj, context)
if context.get('with_capacity'):
obj, capacity = obj
group_dict = table_dictize(obj, context, capacity=capacity)
else:
group_dict = table_dictize(obj, context)
group_dict.pop('created')
if active and obj.state not in ('active', 'pending'):
continue
Expand Down Expand Up @@ -188,7 +192,7 @@ def _get_members(context, group, member_type):

model = context['model']
Entity = getattr(model, member_type[:-1].capitalize())
return model.Session.query(Entity).\
return model.Session.query(Entity, model.Member.capacity).\
join(model.Member, model.Member.table_id == Entity.id).\
filter(model.Member.group_id == group.id).\
filter(model.Member.state == 'active').\
Expand All @@ -204,6 +208,8 @@ def group_dictize(group, context):
result_dict['extras'] = extras_dict_dictize(
group._extras, context)

context['with_capacity'] = True

result_dict['packages'] = obj_list_dictize(
_get_members(context, group, 'packages'),
context)
Expand All @@ -220,14 +226,20 @@ def group_dictize(group, context):
_get_members(context, group, 'users'),
context)

context['with_capacity'] = False

return result_dict

def tag_list_dictize(tag_list, context):

result_list = []
for tag in tag_list:
result_list.append(table_dictize(tag, context))
if context.get('with_capacity'):
tag, capacity = tag
dictized = table_dictize(tag, context, capacity=capacity)
else:
dictized = table_dictize(tag, context)
result_list.append(dictized)

return result_list

Expand All @@ -254,7 +266,11 @@ def user_list_dictize(obj_list, context,

def user_dictize(user, context):

result_dict = table_dictize(user, context)
if context.get('with_capacity'):
user, capacity = user
result_dict = table_dictize(user, context, capacity=capacity)
else:
result_dict = table_dictize(user, context)

del result_dict['password']

Expand Down
9 changes: 7 additions & 2 deletions ckan/tests/lib/test_dictization.py
Expand Up @@ -834,7 +834,7 @@ def test_16_group_dictized(self):
'title': 'help',
'extras': [{'key': 'genre', 'value': u'"horror"'},
{'key': 'media', 'value': u'"dvd"'}],
'packages':[{'name': 'annakarenina2'}, {'id': pkg.id}],
'packages':[{'name': 'annakarenina2'}, {'id': pkg.id, 'capacity': 'in'}],
'users':[{'name': 'annafan'}],
'groups':[{'name': 'simple'}],
'tags':[{'name': 'russian'}]
Expand All @@ -855,8 +855,9 @@ def test_16_group_dictized(self):
expected = {'description': u'',
'extras': [{'key': u'genre', 'state': u'active', 'value': u'"horror"'},
{'key': u'media', 'state': u'active', 'value': u'"dvd"'}],
'tags': [{'name': u'russian'}],
'tags': [{'capacity': 'member', 'name': u'russian'}],
'groups': [{'description': u'',
'capacity' : 'member',
'display_name': u'simple',
'name': u'simple',
'packages': 0,
Expand All @@ -865,6 +866,7 @@ def test_16_group_dictized(self):
'type': u'publisher'}],
'users': [{'about': u'I love reading Annakarenina. My site: <a href="http://anna.com">anna.com</a>',
'display_name': u'annafan',
'capacity' : 'member',
'email': None,
'email_hash': 'd41d8cd98f00b204e9800998ecf8427e',
'fullname': None,
Expand All @@ -882,11 +884,14 @@ def test_16_group_dictized(self):
'name': u'annakarenina3',
'notes': u'Some test notes\n\n### A 3rd level heading\n\n**Some bolded text.**\n\n*Some italicized text.*\n\nForeign characters:\nu with umlaut \xfc\n66-style quote \u201c\nforeign word: th\xfcmb\n \nNeeds escaping:\nleft arrow <\n\n<http://ckan.net/>\n\n',
'state': u'active',
'capacity' : 'in',
'title': u'A Novel By Tolstoy',
'url': u'http://www.annakarenina.com',
'version': u'0.7a'},
{'author': None,
'author_email': None,
'capacity' : 'member',
'title': u'A Novel By Tolstoy',
'license_id': u'other-open',
'maintainer': None,
'maintainer_email': None,
Expand Down

0 comments on commit b6971f9

Please sign in to comment.