Skip to content

Instantly share code, notes, and snippets.

@amercader
Created June 12, 2018 11:57
Show Gist options
  • Save amercader/269cc25414c06d5f47aa0fbf743a1721 to your computer and use it in GitHub Desktop.
Save amercader/269cc25414c06d5f47aa0fbf743a1721 to your computer and use it in GitHub Desktop.
diff --git a/.circleci-matrix.yml b/.circleci-matrix.yml
index 8e548dc..6141f1e 100644
--- a/.circleci-matrix.yml
+++ b/.circleci-matrix.yml
@@ -6,4 +6,4 @@ env:
command:
- mkdir -p $CIRCLE_TEST_REPORTS/nose
-- nosetests --ckan --reset-db --with-pylons=test-core.ini --nologcapture --with-coverage --cover-package=ckan --cover-package=ckanext --with-xunit --xunit-file=$CIRCLE_TEST_REPORTS/nose/junit.xml --segments=$SEGMENTS ckan ckanext
+- nosetests --ckan --reset-db --with-pylons=test-core.ini --nologcapture --with-coverage --cover-package=ckan --cover-package=ckanext --with-xunit --xunit-file=$CIRCLE_TEST_REPORTS/nose/junit.xml --segments=$SEGMENTS -v ckan ckanext
diff --git a/ckan/config/environment.py b/ckan/config/environment.py
index ffee1bb..52d28b3 100644
--- a/ckan/config/environment.py
+++ b/ckan/config/environment.py
@@ -14,6 +14,7 @@ import formencode
import ckan.config.routing as routing
import ckan.model as model
import ckan.plugins as p
+import ckan.lib.plugins as lib_plugins
import ckan.lib.helpers as helpers
import ckan.lib.app_globals as app_globals
from ckan.lib.redis import is_redis_available
@@ -222,6 +223,10 @@ def update_config():
search.check_solr_schema_version()
routes_map = routing.make_map()
+
+ lib_plugins.reset_package_plugins()
+ lib_plugins.set_default_package_plugin()
+
config['routes.map'] = routes_map
# The RoutesMiddleware needs its mapper updating if it exists
if 'routes.middleware' in config:
@@ -235,6 +240,7 @@ def update_config():
helpers.load_plugin_helpers()
config['pylons.h'] = helpers.helper_functions
+
# Templates and CSS loading from configuration
valid_base_templates_folder_names = ['templates', 'templates-bs2']
templates = config.get('ckan.base_templates_folder', 'templates')
diff --git a/ckan/config/middleware/__init__.py b/ckan/config/middleware/__init__.py
index 082372d..192e981 100644
--- a/ckan/config/middleware/__init__.py
+++ b/ckan/config/middleware/__init__.py
@@ -13,6 +13,8 @@ from ckan.config.environment import load_environment
from ckan.config.middleware.flask_app import make_flask_stack
from ckan.config.middleware.pylons_app import make_pylons_stack
from ckan.common import config
+
+import ckan.lib.plugins as lib_plugins
from ckan.lib.i18n import get_locales_from_config
import logging
@@ -58,9 +60,12 @@ def make_app(conf, full_stack=True, static_files=True, **app_conf):
**app_conf)
flask_app = make_flask_stack(conf, **app_conf)
+
app = AskAppDispatcherMiddleware({'pylons_app': pylons_app,
'flask_app': flask_app})
+ lib_plugins.register_package_plugins(flask_app._wsgi_app)
+
# Set this internal test request context with the configured environment so
# it can be used when calling url_for from tests
global _internal_test_request_context
diff --git a/ckan/config/routing.py b/ckan/config/routing.py
index b3b11c2..b46890b 100644
--- a/ckan/config/routing.py
+++ b/ckan/config/routing.py
@@ -86,7 +86,6 @@ def make_map():
OPTIONS = dict(method=['OPTIONS'])
import ckan.lib.plugins as lib_plugins
- lib_plugins.reset_package_plugins()
lib_plugins.reset_group_plugins()
map = Mapper(directory=config['pylons.paths']['controllers'],
@@ -218,7 +217,6 @@ def make_map():
'/organization/bulk_process/{id}',
action='bulk_process', ckan_icon='sitemap')
- lib_plugins.register_package_plugins(current_app)
lib_plugins.register_group_plugins(map)
diff --git a/ckan/lib/plugins.py b/ckan/lib/plugins.py
index 27b892a..a849527 100644
--- a/ckan/lib/plugins.py
+++ b/ckan/lib/plugins.py
@@ -54,6 +54,7 @@ def lookup_package_plugin(package_type=None):
If the package type is None or cannot be found in the mapping, then the
fallback behaviour is used.
+
"""
if package_type is None:
return _default_package_plugin
@@ -91,18 +92,13 @@ def register_package_plugins(app):
"""
global _default_package_plugin
- # This function should have not effect if called more than once.
- # This should not occur in normal deployment, but it may happen when
- # running unit tests.
- if _default_package_plugin is not None:
- return
- from ckan.views import dataset
- from ckan.views import resource
+ from ckan.views.dataset import dataset, register_dataset_plugin_rules
+ from ckan.views.resource import resource, register_dataset_plugin_rules as dataset_resource_rules
# Create the mappings and register the fallback behaviour if one is found.
for plugin in plugins.PluginImplementations(plugins.IDatasetForm):
if plugin.is_fallback():
- if _default_package_plugin is not None:
+ if _default_package_plugin is not None and not isinstance(_default_package_plugin, DefaultDatasetForm):
raise ValueError("More than one fallback "
"IDatasetForm has been registered")
_default_package_plugin = plugin
@@ -111,18 +107,18 @@ def register_package_plugins(app):
# package controller
dataset_blueprint = Blueprint(
package_type,
- dataset.dataset.import_name,
+ dataset.import_name,
url_prefix='/{}'.format(package_type),
url_defaults={'package_type': package_type})
- dataset.register_dataset_plugin_rules(dataset_blueprint)
+ register_dataset_plugin_rules(dataset_blueprint)
app.register_blueprint(dataset_blueprint)
resource_blueprint = Blueprint(
u'{}_resource'.format(package_type),
- resource.resource.import_name,
+ resource.import_name,
url_prefix=u'/{}/<id>/resource'.format(package_type),
url_defaults={u'package_type': package_type})
- resource.register_dataset_plugin_rules(resource_blueprint)
+ dataset_resource_rules(resource_blueprint)
app.register_blueprint(resource_blueprint)
if package_type in _package_plugins:
@@ -132,6 +128,11 @@ def register_package_plugins(app):
_package_plugins[package_type] = plugin
# Setup the fallback behaviour if one hasn't been defined.
+ set_default_package_plugin()
+
+
+def set_default_package_plugin():
+ global _default_package_plugin
if _default_package_plugin is None:
_default_package_plugin = DefaultDatasetForm()
diff --git a/ckan/model/modification.py b/ckan/model/modification.py
index 2e1201d..4c31b19 100644
--- a/ckan/model/modification.py
+++ b/ckan/model/modification.py
@@ -2,17 +2,12 @@
import logging
-from sqlalchemy.orm.exc import UnmappedInstanceError
-
from ckan.lib.search import SearchIndexError
import ckan.plugins as plugins
-from ckan.common import g
-import ckan.model as model
-
-domain_object = model.domain_object
-_package = model.package
-resource = model.resource
+import domain_object
+import package as _package
+import resource
log = logging.getLogger(__name__)
@@ -82,17 +77,6 @@ class DomainObjectModificationExtension(plugins.SingletonPlugin):
observer.notify(entity, operation)
except SearchIndexError as search_error:
log.exception(search_error)
-
- # userobj must be available inside rendered error template,
- # though it become unbounded after session rollback because
- # of this error. Expunge will prevent `UnboundedInstanceError`
- # raised from error template.
- try:
- model.Session.expunge(g.userobj)
- # AttributeError - there is no such prop in `g`
- # UnmappedInstanceError - g.userobj is None or empty string.
- except (AttributeError, UnmappedInstanceError):
- pass
# Reraise, since it's pretty crucial to ckan if it can't index
# a dataset
raise
diff --git a/ckan/tests/config/test_environment.py b/ckan/tests/config/test_environment.py
index 4950189..ce22e20 100644
--- a/ckan/tests/config/test_environment.py
+++ b/ckan/tests/config/test_environment.py
@@ -101,7 +101,9 @@ class TestSiteUrlMandatory(object):
@helpers.change_config('ckan.site_url', 'http://demo.ckan.org/')
def test_siteurl_removes_backslash(self):
- environment.update_config()
+ app = helpers._get_test_app()
+ with app.flask_app.test_request_context():
+ environment.update_config()
nosetools.assert_equals(config['ckan.site_url'],
'http://demo.ckan.org')
diff --git a/ckan/tests/logic/action/test_get.py b/ckan/tests/logic/action/test_get.py
index 4263f7f..e564c20 100644
--- a/ckan/tests/logic/action/test_get.py
+++ b/ckan/tests/logic/action/test_get.py
@@ -1299,8 +1299,6 @@ class TestPackageAutocompleteWithDatasetForm(helpers.FunctionalTestBase):
eq(query['results'][0]['extras'][0]['key'], 'custom_text')
eq(query['results'][0]['extras'][0]['value'], 'foo')
- p.unload('example_idatasetform')
-
def test_local_parameters_not_supported(self):
nose.tools.assert_raises(
diff --git a/ckan/views/dataset.py b/ckan/views/dataset.py
index c88e0ea..84e7ce6 100644
--- a/ckan/views/dataset.py
+++ b/ckan/views/dataset.py
@@ -341,7 +341,7 @@ def search(package_type):
extra_vars[u'dataset_type'] = package_type
# TODO: remove
- for key, value in extra_vars.items():
+ for key, value in extra_vars.iteritems():
setattr(g, key, value)
return base.render(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment