Create different files for each controller

This commit is contained in:
Aitor Magán 2014-07-08 12:30:28 +02:00
parent 6a35b8a777
commit 4ddef599a2
7 changed files with 109 additions and 106 deletions

View File

@ -1,12 +1,11 @@
import ckan.lib.base as base
import ckan.lib.helpers as helpers
import ckan.plugins as plugins
import ckan.model as model
import importlib
import logging
import pylons.config as config
from ckan.common import response, _
from ckan.common import response
log = logging.getLogger(__name__)
@ -90,47 +89,3 @@ class AdquiredDatasetsControllerAPI(base.BaseController):
# Return warnings that inform about non-existing datasets
if len(warns) > 0:
return helpers.json.dumps({'warns': warns})
######################################################################
############################ UI CONTROLLER ###########################
######################################################################
class AdquiredDatasetsControllerUI(base.BaseController):
def user_adquired_datasets(self):
c = plugins.toolkit.c
context = {
'model': model,
'session': model.Session,
'user': plugins.toolkit.c.user
}
# Get user information
try:
c.user_dict = plugins.toolkit.get_action('user_show')(context, {'user_obj': c.userobj})
c.user_dict['adquired_datasets'] = []
except plugins.toolkit.ObjectNotFound:
plugins.toolkit.abort(404, _('User not found'))
except plugins.toolkit.NotAuthorized:
plugins.toolkit.abort(401, _('Not authorized to see this page'))
# Get the datasets adquired by the user
query = model.Session.query(model.PackageExtra).filter(
# Select only the allowed_users key
'package_extra.key=\'%s\' AND package_extra.value!=\'\' ' % 'allowed_users' +
# Selec only when the state is 'active'
'AND package_extra.state=\'%s\' ' % 'active' +
# The user name should be contained in the list
'AND regexp_split_to_array(package_extra.value,\',\') @> ARRAY[\'%s\']' % context['user'])
# Get full information about the datasets
for dataset in query:
try:
dataset_dict = plugins.toolkit.get_action('package_show')(context, {'id': dataset.package_id})
c.user_dict['adquired_datasets'].append(dataset_dict)
except Exception:
continue
return plugins.toolkit.render('user/dashboard_adquired.html')

View File

@ -0,0 +1,48 @@
import ckan.lib.base as base
import ckan.model as model
import ckan.plugins as plugins
import logging
from ckan.common import _
log = logging.getLogger(__name__)
class AdquiredDatasetsControllerUI(base.BaseController):
def user_adquired_datasets(self):
c = plugins.toolkit.c
context = {
'model': model,
'session': model.Session,
'user': plugins.toolkit.c.user
}
# Get user information
try:
c.user_dict = plugins.toolkit.get_action('user_show')(context, {'user_obj': c.userobj})
c.user_dict['adquired_datasets'] = []
except plugins.toolkit.ObjectNotFound:
plugins.toolkit.abort(404, _('User not found'))
except plugins.toolkit.NotAuthorized:
plugins.toolkit.abort(401, _('Not authorized to see this page'))
# Get the datasets adquired by the user
query = model.Session.query(model.PackageExtra).filter(
# Select only the allowed_users key
'package_extra.key=\'%s\' AND package_extra.value!=\'\' ' % 'allowed_users' +
# Selec only when the state is 'active'
'AND package_extra.state=\'%s\' ' % 'active' +
# The user name should be contained in the list
'AND regexp_split_to_array(package_extra.value,\',\') @> ARRAY[\'%s\']' % context['user'])
# Get full information about the datasets
for dataset in query:
try:
dataset_dict = plugins.toolkit.get_action('package_show')(context, {'id': dataset.package_id})
c.user_dict['adquired_datasets'].append(dataset_dict)
except Exception:
continue
return plugins.toolkit.render('user/dashboard_adquired.html')

View File

@ -237,10 +237,10 @@ class PrivateDatasets(p.SingletonPlugin, tk.DefaultDatasetForm):
def after_map(self, m):
# DataSet adquired notification
m.connect('/dataset_adquired',
controller='ckanext.privatedatasets.controllers:AdquiredDatasetsControllerAPI',
controller='ckanext.privatedatasets.controllers.api_controller:AdquiredDatasetsControllerAPI',
action='add_users', conditions=dict(method=['POST']))
m.connect('user_adquired_datasets', '/dashboad/adquired', ckan_icon='shopping-cart',
controller='ckanext.privatedatasets.controllers:AdquiredDatasetsControllerUI',
controller='ckanext.privatedatasets.controllers.ui_controller:AdquiredDatasetsControllerUI',
action='user_adquired_datasets', conditions=dict(method=['GET']))
return m

View File

@ -1,4 +1,4 @@
import ckanext.privatedatasets.controllers as controllers
import ckanext.privatedatasets.controllers.api_controller as controller
import json
import unittest
@ -16,27 +16,27 @@ class APIControllerTest(unittest.TestCase):
def setUp(self):
# Get the instance
self.instanceAPI = controllers.AdquiredDatasetsControllerAPI()
self.instanceAPI = controller.AdquiredDatasetsControllerAPI()
# Load the mocks
self._config = controllers.config
controllers.config = MagicMock()
self._config = controller.config
controller.config = MagicMock()
self._importlib = controllers.importlib
controllers.importlib = MagicMock()
self._importlib = controller.importlib
controller.importlib = MagicMock()
self._plugins = controllers.plugins
controllers.plugins = MagicMock()
self._plugins = controller.plugins
controller.plugins = MagicMock()
self._response = controllers.response
controllers.response = MagicMock()
self._response = controller.response
controller.response = MagicMock()
def tearDown(self):
# Unmock
controllers.config = self._config
controllers.importlib = self._importlib
controllers.plugins = self._plugins
controllers.response = self._response
controller.config = self._config
controller.importlib = self._importlib
controller.plugins = self._plugins
controller.response = self._response
@parameterized.expand([
('', None, False, False, '{"errors": ["%s not configured"]}' % PARSER_CONFIG_PROP),
@ -52,29 +52,29 @@ class APIControllerTest(unittest.TestCase):
class_package = class_path
class_package += ':' + class_name if class_name else ''
controllers.config = {PARSER_CONFIG_PROP: class_package}
controller.config = {PARSER_CONFIG_PROP: class_package}
# Configure the mock
package = MagicMock()
if class_name and not class_exist:
delattr(package, class_name)
controllers.importlib.import_module = MagicMock(side_effect=ImportError(IMPORT_ERROR_MSG) if not path_exist else None,
return_value=package if path_exist else None)
controller.importlib.import_module = MagicMock(side_effect=ImportError(IMPORT_ERROR_MSG) if not path_exist else None,
return_value=package if path_exist else None)
# Call the function
result = self.instanceAPI.add_users()
# Checks
self.assertEquals(expected_error, result)
self.assertEquals(0, controllers.plugins.toolkit.get_action.call_count)
self.assertEquals(0, controller.plugins.toolkit.get_action.call_count)
if expected_error:
self.assertEquals(400, controllers.response.status_int)
self.assertEquals(400, controller.response.status_int)
def configure_mocks(self, parse_result, datasets_not_found=[], not_updatable_datasets=[], allowed_users=None):
controllers.config = {PARSER_CONFIG_PROP: 'valid.path:%s' % CLASS_NAME}
controller.config = {PARSER_CONFIG_PROP: 'valid.path:%s' % CLASS_NAME}
# Configure mocks
parser_instance = MagicMock()
@ -82,21 +82,21 @@ class APIControllerTest(unittest.TestCase):
package = MagicMock()
package.parser_class = MagicMock(return_value=parser_instance)
controllers.importlib.import_module = MagicMock(return_value=package)
controller.importlib.import_module = MagicMock(return_value=package)
# We should use the real exceptions
controllers.plugins.toolkit.ObjectNotFound = self._plugins.toolkit.ObjectNotFound
controllers.plugins.toolkit.ValidationError = self._plugins.toolkit.ValidationError
controller.plugins.toolkit.ObjectNotFound = self._plugins.toolkit.ObjectNotFound
controller.plugins.toolkit.ValidationError = self._plugins.toolkit.ValidationError
def _package_show(context, data_dict):
if data_dict['id'] in datasets_not_found:
raise controllers.plugins.toolkit.ObjectNotFound()
raise controller.plugins.toolkit.ObjectNotFound()
else:
return {'id': data_dict['id'], 'allowed_users': allowed_users}
def _package_update(context, data_dict):
if data_dict['id'] in not_updatable_datasets:
raise controllers.plugins.toolkit.ValidationError({'allowed_users': [ADD_USERS_ERROR]})
raise controller.plugins.toolkit.ValidationError({'allowed_users': [ADD_USERS_ERROR]})
package_show = MagicMock(side_effect=_package_show)
package_update = MagicMock(side_effect=_package_update)
@ -107,7 +107,7 @@ class APIControllerTest(unittest.TestCase):
elif action == 'package_show':
return package_show
controllers.plugins.toolkit.get_action = _get_action
controller.plugins.toolkit.get_action = _get_action
return package_show, package_update
@ -127,7 +127,7 @@ class APIControllerTest(unittest.TestCase):
self.assertEquals(0, package_search.call_count)
self.assertEquals(0, package_update.call_count)
self.assertEquals(expected_result, result)
self.assertEquals(400, controllers.response.status_int)
self.assertEquals(400, controller.response.status_int)
@parameterized.expand([
# Simple Test: one user and one dataset

View File

@ -1,4 +1,4 @@
import ckanext.privatedatasets.controllers as controllers
import ckanext.privatedatasets.controllers.ui_controller as controller
import unittest
from mock import MagicMock, ANY
@ -10,52 +10,52 @@ class UIControllerTest(unittest.TestCase):
def setUp(self):
# Get the instance
self.instanceUI = controllers.AdquiredDatasetsControllerUI()
self.instanceUI = controller.AdquiredDatasetsControllerUI()
# Load the mocks
self._plugins = controllers.plugins
controllers.plugins = MagicMock()
self._plugins = controller.plugins
controller.plugins = MagicMock()
self._model = controllers.model
controllers.model = MagicMock()
self._model = controller.model
controller.model = MagicMock()
# Set exceptions
controllers.plugins.toolkit.ObjectNotFound = self._plugins.toolkit.ObjectNotFound
controllers.plugins.toolkit.NotAuthorized = self._plugins.toolkit.NotAuthorized
controller.plugins.toolkit.ObjectNotFound = self._plugins.toolkit.ObjectNotFound
controller.plugins.toolkit.NotAuthorized = self._plugins.toolkit.NotAuthorized
def tearDown(self):
# Unmock
controllers.plugins = self._plugins
controllers.model = self._model
controller.plugins = self._plugins
controller.model = self._model
@parameterized.expand([
(controllers.plugins.toolkit.ObjectNotFound, 404),
(controllers.plugins.toolkit.NotAuthorized, 401)
(controller.plugins.toolkit.ObjectNotFound, 404),
(controller.plugins.toolkit.NotAuthorized, 401)
])
def test_exceptions_loading_users(self, exception, expected_status):
# Configure the mock
user_show = MagicMock(side_effect=exception)
controllers.plugins.toolkit.get_action = MagicMock(return_value=user_show)
controller.plugins.toolkit.get_action = MagicMock(return_value=user_show)
# Call the function
self.instanceUI.user_adquired_datasets()
# Assertations
expected_context = {
'model': controllers.model,
'session': controllers.model.Session,
'user': controllers.plugins.toolkit.c.user
'model': controller.model,
'session': controller.model.Session,
'user': controller.plugins.toolkit.c.user
}
user_show.assert_called_once_with(expected_context, {'user_obj': controllers.plugins.toolkit.c.userobj})
controllers.plugins.toolkit.abort.assert_called_once_with(expected_status, ANY)
user_show.assert_called_once_with(expected_context, {'user_obj': controller.plugins.toolkit.c.userobj})
controller.plugins.toolkit.abort.assert_called_once_with(expected_status, ANY)
def test_no_errors(self):
pkgs_ids = [0, 1, 2, 3]
user = 'example_user_test'
controllers.plugins.toolkit.c.user = user
controller.plugins.toolkit.c.user = user
# get_action mock
user_dict = {'user_name': 'test', 'another_val': 'example value'}
@ -68,7 +68,7 @@ class UIControllerTest(unittest.TestCase):
elif action == 'user_show':
return user_show
controllers.plugins.toolkit.get_action = MagicMock(side_effect=_get_action)
controller.plugins.toolkit.get_action = MagicMock(side_effect=_get_action)
# query mock
query_res = []
@ -79,22 +79,22 @@ class UIControllerTest(unittest.TestCase):
filter_f = MagicMock()
filter_f.filter = MagicMock(return_value=query_res)
controllers.model.Session.query = MagicMock(return_value=filter_f)
controller.model.Session.query = MagicMock(return_value=filter_f)
# Call the function
returned = self.instanceUI.user_adquired_datasets()
# User_show called correctly
expected_context = {
'model': controllers.model,
'session': controllers.model.Session,
'user': controllers.plugins.toolkit.c.user
'model': controller.model,
'session': controller.model.Session,
'user': controller.plugins.toolkit.c.user
}
user_show.assert_called_once_with(expected_context, {'user_obj': controllers.plugins.toolkit.c.userobj})
user_show.assert_called_once_with(expected_context, {'user_obj': controller.plugins.toolkit.c.userobj})
# Query called correctry
controllers.model.Session.query.assert_called_once_with(controllers.model.PackageExtra)
controller.model.Session.query.assert_called_once_with(controller.model.PackageExtra)
# Filter called correctly
filter_f.filter.assert_called_once_with('package_extra.key=\'allowed_users\' AND package_extra.value!=\'\' ' +
@ -111,8 +111,8 @@ class UIControllerTest(unittest.TestCase):
expected_user_dict['adquired_datasets'] = []
for i in pkgs_ids:
expected_user_dict['adquired_datasets'].append(package_show.return_value)
self.assertEquals(expected_user_dict, controllers.plugins.toolkit.c.user_dict)
self.assertEquals(expected_user_dict, controller.plugins.toolkit.c.user_dict)
# Check that the render method has been called and that its result has been returned
self.assertEquals(controllers.plugins.toolkit.render.return_value, returned)
controllers.plugins.toolkit.render.assert_called_once_with('user/dashboard_adquired.html')
self.assertEquals(controller.plugins.toolkit.render.return_value, returned)
controller.plugins.toolkit.render.assert_called_once_with('user/dashboard_adquired.html')

View File

@ -235,10 +235,10 @@ class PluginTest(unittest.TestCase):
# Test that the connect method has been called
m.connect.assert_any_call('/dataset_adquired',
controller='ckanext.privatedatasets.controllers:AdquiredDatasetsControllerAPI',
controller='ckanext.privatedatasets.controllers.api_controller:AdquiredDatasetsControllerAPI',
action='add_users', conditions=dict(method=['POST']))
m.connect.assert_any_call('user_adquired_datasets', '/dashboad/adquired', ckan_icon='shopping-cart',
controller='ckanext.privatedatasets.controllers:AdquiredDatasetsControllerUI',
controller='ckanext.privatedatasets.controllers.ui_controller:AdquiredDatasetsControllerUI',
action='user_adquired_datasets', conditions=dict(method=['GET']))
@parameterized.expand([