Cache is updated when the list of allowed users is updated

This commit is contained in:
Aitor Magán 2014-07-17 13:25:03 +02:00
parent 2ee2b077cc
commit 6838221f43
3 changed files with 40 additions and 4 deletions

View File

@ -11,7 +11,7 @@ def private_datasets_metadata_checker(key, data, errors, context):
dataset_id = data.get(('id',))
private_val = data.get(('private',))
#Avoid missing value
# Avoid missing value
if not isinstance(private_val, basestring) and not isinstance(private_val, bool):
private_val = None

View File

@ -1,3 +1,4 @@
import ckan.lib.search as search
import ckan.plugins as p
import ckan.plugins.toolkit as tk
import auth
@ -22,6 +23,9 @@ class PrivateDatasets(p.SingletonPlugin, tk.DefaultDatasetForm):
############################ DATASET FORM ############################
######################################################################
def __init__(self, name=None):
self.indexer = search.PackageSearchIndex()
def _modify_package_schema(self):
return {
# remove datasets_with_no_organization_cannot_be_private validator
@ -150,6 +154,7 @@ class PrivateDatasets(p.SingletonPlugin, tk.DefaultDatasetForm):
def after_create(self, context, pkg_dict):
session = context['session']
update_cache = False
db.init_db(context['model'])
@ -168,6 +173,7 @@ class PrivateDatasets(p.SingletonPlugin, tk.DefaultDatasetForm):
current_users.append(user.user_name)
if user.user_name not in allowed_users:
session.delete(user)
update_cache = True
# Add non existing users
for user_name in allowed_users:
@ -177,6 +183,20 @@ class PrivateDatasets(p.SingletonPlugin, tk.DefaultDatasetForm):
out.user_name = user_name
out.save()
session.add(out)
update_cache = True
session.commit()
# The cache should be updated. Otherwise, the system may return
# outdated information in future requests
if update_cache:
new_pkg_dict = tk.get_action('package_show')(
{'model': context['model'],
'ignore_auth': True,
'validate': False,
'use_cache': False},
{'id': package_id})
self.indexer.update_dict(new_pkg_dict)
return pkg_dict

View File

@ -8,9 +8,6 @@ from nose_parameterized import parameterized
class PluginTest(unittest.TestCase):
def setUp(self):
# Create the plugin
self.privateDatasets = plugin.PrivateDatasets()
# Create mocks
self._tk = plugin.tk
plugin.tk = MagicMock()
@ -18,9 +15,16 @@ class PluginTest(unittest.TestCase):
self._db = plugin.db
plugin.db = MagicMock()
self._search = plugin.search
plugin.search = MagicMock()
# Create the plugin
self.privateDatasets = plugin.PrivateDatasets()
def tearDown(self):
plugin.tk = self._tk
plugin.db = self._db
plugin.search = self._search
@parameterized.expand([
(plugin.p.IDatasetForm,),
@ -233,6 +237,11 @@ class PluginTest(unittest.TestCase):
def _aux_test_after_create_update(self, function, new_users, current_users, users_to_add, users_to_delete):
package_id = 'package_id'
# Configure mocks
default_dict = {'a': '0', 'b': 1, 'm': True}
package_show = MagicMock(return_value=default_dict)
plugin.tk.get_action = MagicMock(return_value=package_show)
# Each time 'AllowedUser' is called, we must get a new instance
# and this is the way to get this behaviour
def constructor():
@ -274,6 +283,13 @@ class PluginTest(unittest.TestCase):
# Check that the method has added the appropiate users
_test_calls(users_to_add, context['session'].add)
if len(users_to_add) == 0 and len(users_to_delete) == 0:
# Check that the cache has not been updated
self.assertEquals(0, self.privateDatasets.indexer.update_dict.call_count)
else:
# Check that the cache has been updated
self.privateDatasets.indexer.update_dict.assert_called_once_with(default_dict)
@parameterized.expand([
# One element
(['a'], [], ['a'], []),