Skip to content
This repository has been archived by the owner on Jan 8, 2021. It is now read-only.

Commit

Permalink
Hide organizations without datasets from organization page.
Browse files Browse the repository at this point in the history
  • Loading branch information
razz0 committed Nov 17, 2015
1 parent 550e7d5 commit 9cb1a20
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
56 changes: 56 additions & 0 deletions ckanext/kata/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import os

from ckan.controllers.api import ApiController
from ckan.controllers.organization import OrganizationController
from ckan.controllers.package import PackageController
from ckan.controllers.user import UserController
from ckan.controllers.storage import StorageController
Expand Down Expand Up @@ -1135,3 +1136,58 @@ def index(self):
c.num_discipline = 0

return render("home/index.html", cache_force=True)

class KataOrganizationController(OrganizationController):

def index(self):
'''
Modified version of ckan.controllers.group:GroupController.index to hide datasets without organizations.
'''
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', '')
sort_by = c.sort_by_selected = request.params.get('sort')
try:
self._check_access('site_read', context)
except NotAuthorized:
abort(401, _('Not authorized to see this page'))

# pass user info to context as needed to view private datasets of
# orgs correctly
if c.userobj:
context['user_id'] = c.userobj.id
context['user_is_admin'] = c.userobj.sysadmin

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

global_results = [org for org in global_results if org.get('package_count', 0) > 0]

page_results_start = items_per_page * (page - 1)
page_results_end = page_results_start + items_per_page

page_results = global_results[page_results_start:page_results_end]

c.page = h.Page(
collection=[org.get('name') for org in global_results],
page=page,
url=h.pager_url,
items_per_page=items_per_page,
)

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

4 changes: 4 additions & 0 deletions ckanext/kata/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ def before_map(self, map):
'/',
controller="ckanext.kata.controllers:KataHomeController",
action="index")
map.connect('organizations_index',
'/organization',
controller='ckanext.kata.controllers:KataOrganizationController',
action='index')

# Hide resource_read page
map.redirect('/dataset/{id}/resource/{resource_id}', '/dataset/{id}')
Expand Down
44 changes: 42 additions & 2 deletions ckanext/kata/tests/functional/test_wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def test_edit_not_available(self):
'''
Test that edit page is not available for random user
'''
offset = offset = url_for("/dataset/edit/annakarenina")
offset = url_for("/dataset/edit/annakarenina")

extra_environ = {'REMOTE_USER': 'russianfan'}
res = self.app.get(offset, extra_environ=extra_environ, status=401)
Expand All @@ -261,7 +261,7 @@ def test_delete_not_available(self):
Test that deletion of a dataset is not available
for an unauthorised user
'''
offset = offset = url_for("/dataset/delete/annakarenina")
offset = url_for("/dataset/delete/annakarenina")
extra_environ = {'REMOTE_USER': 'russianfan'}
res = self.app.get(offset, extra_environ=extra_environ, status=401)

Expand Down Expand Up @@ -387,3 +387,43 @@ def test_02_formpage(self):
assert 'resource-upload-field' in res

get_action('package_delete')({'user': 'testsysadmin'}, package)


class TestKataOrganizationController(KataWsgiTestCase):
'''Test organization controller'''

def test_organization_hiding_pagination(self):
model.User(name="test_sysadmin", sysadmin=True).save()

orgs = []

for x in range(50): # Create 50 organizations
org_id = 'org_{x}'.format(x=str(x))
org_title = 'Organisation {x}'.format(x=str(x))
orgs.append(get_action('organization_create')({'user': 'test_sysadmin'},
{'name': org_id, 'title': org_title}))

for org in orgs[::2]: # Create 25 datasets to distinct organizations
data = copy.deepcopy(TEST_DATADICT)
data['owner_org'] = org['name']
data['name'] = org['id']
get_action('package_create')({'user': 'test_sysadmin'}, data)

res = self.app.get(url_for("/organization"), status=200)

# Page 1:

assert res.body.count('>0 Datasets<') == 0
assert res.body.count('>1 Dataset<') == 21

assert res.body.count('25 organizations found')

# Page 2:

res = self.app.get(url_for("/organization?sort=&q=&page=2"), status=200)

assert res.body.count('>0 Datasets<') == 0
assert res.body.count('>1 Dataset<') == 4

assert res.body.count('25 organizations found')

0 comments on commit 9cb1a20

Please sign in to comment.