Skip to content

Commit

Permalink
Don't dictize each tags packages on tag index page
Browse files Browse the repository at this point in the history
This makes the tag index page load much faster when there are a large
number of tags.

tag_list() logic action function calls tag_list_dicitize() instead of
tag_dictize(), the difference is that tag_list_dicitize() doesn't
dictize all of the tag's packages as well. Extended tag_list_dicitize()
to have some of tag_dictize()'s features that it was missing.
  • Loading branch information
Sean Hammond committed May 17, 2012
1 parent 6bcce4a commit 968690e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
10 changes: 4 additions & 6 deletions ckan/controllers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,13 +540,11 @@ def tag_counts(self, ver=None):
context = {'model': model, 'session': model.Session,
'user': c.user or c.author}

data_dict = {'all_fields': True}

tag_list = get_action('tag_list')(context, data_dict)
tag_names = get_action('tag_list')(context, {})
results = []
for tag in tag_list:
tag_count = len(tag['packages'])
results.append((tag['name'], tag_count))
for tag_name in tag_names:
tag_count = len(context['model'].Tag.get(tag_name).packages)
results.append((tag_name, tag_count))
return self._finish_ok(results)

def throughput(self, ver=None):
Expand Down
13 changes: 13 additions & 0 deletions ckan/lib/dictization/model_dictize.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,19 @@ def tag_list_dictize(tag_list, context):
dictized = d.table_dictize(tag, context, capacity=capacity)
else:
dictized = d.table_dictize(tag, context)

# Add display_names to tag dicts. At first a tag's display_name is just
# the same as its name, but the display_name might get changed later
# (e.g. translated into another language by the multilingual
# extension).
assert not dictized.has_key('display_name')
dictized['display_name'] = dictized['name']

if context.get('for_view'):
for item in plugins.PluginImplementations(
plugins.ITagController):
dictized = item.before_view(dictized)

result_list.append(dictized)

return result_list
Expand Down
9 changes: 4 additions & 5 deletions ckan/logic/action/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,14 +334,13 @@ def tag_list(context, data_dict):
else:
tags = model.Tag.all(vocab_id_or_name)

tag_list = []
if tags:
if all_fields:
for tag in tags:
result_dict = model_dictize.tag_dictize(tag, context)
tag_list.append(result_dict)
tag_list = model_dictize.tag_list_dictize(tags, context)
else:
tag_list.extend([tag.name for tag in tags])
tag_list = [tag.name for tag in tags]
else:
tag_list = []

return tag_list

Expand Down
2 changes: 1 addition & 1 deletion ckan/tests/lib/test_dictization.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ 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': [{'capacity': 'public', 'name': u'russian'}],
'tags': [{'capacity': u'public', 'display_name': u'russian', 'name': u'russian'}],
'groups': [{'description': u'',
'capacity' : 'public',
'display_name': u'simple',
Expand Down
12 changes: 0 additions & 12 deletions ckan/tests/logic/test_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,6 @@ def test_06a_tag_list(self):
assert res_obj['result'][russian_index]['name'] == 'russian'
assert res_obj['result'][tolstoy_index]['name'] == 'tolstoy'

# The "moo" package may part of the retrieved packages, depending
# upon whether this test is run in isolation from the rest of the
# test suite or not.
number_of_russian_packages = len(res_obj['result'][russian_index]['packages']) # warandpeace, annakarenina (moo?)
number_of_tolstoy_packages = len(res_obj['result'][tolstoy_index]['packages']) # annakarenina
number_of_flexible_packages = len(res_obj['result'][flexible_index]['packages']) # warandpeace, annakarenina (moo?)

# Assert we have the correct number of packages, independantly of
# whether the "moo" package may exist or not.
assert number_of_russian_packages - number_of_tolstoy_packages == 1
assert number_of_flexible_packages == (number_of_russian_packages - number_of_tolstoy_packages) + 1

assert 'id' in res_obj['result'][0]
assert 'id' in res_obj['result'][1]
assert 'id' in res_obj['result'][2]
Expand Down

0 comments on commit 968690e

Please sign in to comment.