Skip to content

Commit

Permalink
Merge branch 'ckan-2554-improve-group_list'
Browse files Browse the repository at this point in the history
  • Loading branch information
brew committed Aug 25, 2015
2 parents 942a824 + eb30e11 commit 6b82fd4
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 119 deletions.
34 changes: 27 additions & 7 deletions ckan/controllers/group.py
Expand Up @@ -150,15 +150,15 @@ def add_group_type(cls, group_type):
def index(self):
group_type = self._guess_group_type()

page = self._get_page_number(request.params) or 1
items_per_page = 21

context = {'model': model, 'session': model.Session,
'user': c.user or c.author, 'for_view': True,
'with_private': False}

q = c.q = request.params.get('q', '')
data_dict = {'all_fields': True, 'q': q, 'type': group_type or 'group'}
sort_by = c.sort_by_selected = request.params.get('sort')
if sort_by:
data_dict['sort'] = sort_by
try:
self._check_access('site_read', context)
except NotAuthorized:
Expand All @@ -170,14 +170,34 @@ def index(self):
context['user_id'] = c.userobj.id
context['user_is_admin'] = c.userobj.sysadmin

results = self._action('group_list')(context, data_dict)
data_dict_global_results = {
'all_fields': False,
'q': q,
'sort': sort_by,
'type': group_type or 'group',
}
global_results = self._action('group_list')(context,
data_dict_global_results)

data_dict_page_results = {
'all_fields': True,
'q': q,
'sort': sort_by,
'type': group_type or 'group',
'limit': items_per_page,
'offset': items_per_page * (page - 1),
}
page_results = self._action('group_list')(context,
data_dict_page_results)

c.page = h.Page(
collection=results,
page = self._get_page_number(request.params),
collection=global_results,
page=page,
url=h.pager_url,
items_per_page=21
items_per_page=items_per_page,
)

c.page.items = page_results
return render(self._index_template(group_type),
extra_vars={'group_type': group_type})

Expand Down
80 changes: 0 additions & 80 deletions ckan/controllers/home.py
Expand Up @@ -12,9 +12,6 @@

CACHE_PARAMETERS = ['__cache', '__no_cache__']

# horrible hack
dirty_cached_group_stuff = None


class HomeController(base.BaseController):
repo = model.repo
Expand Down Expand Up @@ -74,17 +71,8 @@ def index(self):
'license': _('Licenses'),
}

data_dict = {'sort': 'package_count desc', 'all_fields': 1}
# only give the terms to group dictize that are returned in the
# facets as full results take a lot longer
if 'groups' in c.search_facets:
data_dict['groups'] = [
item['name'] for item in c.search_facets['groups']['items']
]
c.groups = logic.get_action('group_list')(context, data_dict)
except search.SearchError:
c.package_count = 0
c.groups = []

if c.userobj is not None:
msg = None
Expand All @@ -111,74 +99,6 @@ def index(self):
if msg:
h.flash_notice(msg, allow_html=True)

# START OF DIRTINESS
def get_group(id):
def _get_group_type(id):
"""
Given the id of a group it determines the type of a group given
a valid id/name for the group.
"""
group = model.Group.get(id)
if not group:
return None
return group.type

def db_to_form_schema(group_type=None):
from ckan.lib.plugins import lookup_group_plugin
return lookup_group_plugin(group_type).db_to_form_schema()

group_type = _get_group_type(id.split('@')[0])
context = {'model': model, 'session': model.Session,
'ignore_auth': True,
'user': c.user or c.author,
'auth_user_obj': c.userobj,
'schema': db_to_form_schema(group_type=group_type),
'limits': {'packages': 2},
'for_view': True}
data_dict = {'id': id, 'include_datasets': True}

try:
group_dict = logic.get_action('group_show')(context, data_dict)
except logic.NotFound:
return None

return {'group_dict': group_dict}

global dirty_cached_group_stuff
if not dirty_cached_group_stuff:
groups_data = []
groups = config.get('ckan.featured_groups', '').split()

for group_name in groups:
group = get_group(group_name)
if group:
groups_data.append(group)
if len(groups_data) == 2:
break

# c.groups is from the solr query above
if len(groups_data) < 2 and len(c.groups) > 0:
group = get_group(c.groups[0]['name'])
if group:
groups_data.append(group)
if len(groups_data) < 2 and len(c.groups) > 1:
group = get_group(c.groups[1]['name'])
if group:
groups_data.append(group)
# We get all the packages or at least too many so
# limit it to just 2
for group in groups_data:
group['group_dict']['packages'] = \
group['group_dict']['packages'][:2]
#now add blanks so we have two
while len(groups_data) < 2:
groups_data.append({'group_dict': {}})
# cache for later use
dirty_cached_group_stuff = groups_data

c.group_package_stuff = dirty_cached_group_stuff

# END OF DIRTINESS
return base.render('home/index.html', cache_force=True)

def license(self):
Expand Down

0 comments on commit 6b82fd4

Please sign in to comment.