Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI commands require a Flask test request context #3760

Closed
amercader opened this issue Aug 17, 2017 · 0 comments
Closed

CLI commands require a Flask test request context #3760

amercader opened this issue Aug 17, 2017 · 0 comments
Assignees

Comments

@amercader
Copy link
Member

On latest master, running a paster command that involves URL generation (like paster search-index rebuild) will throw the infamous RuntimeError: Attempted to generate a URL without the application context being pushed exception:

paster search-index rebuild                                       
2017-08-15 15:57:43,166 INFO  [ckan.config.environment] Loading static files from public
2017-08-15 15:57:43,190 INFO  [ckan.config.environment] Loading templates from /home/adria/dev/pyenvs/ckan/src/ckan/ckan/templates
2017-08-15 15:57:43,499 INFO  [ckan.config.environment] Loading templates from /home/adria/dev/pyenvs/ckan/src/ckan/ckan/templates
2017-08-15 15:57:43,595 INFO  [ckan.lib.search] Rebuilding search index...
2017-08-15 15:57:43,597 INFO  [ckan.lib.search] Rebuilding the whole index...
Indexing dataset 5/122017-08-15 15:57:43,744 ERROR [ckan.lib.search] Error while indexing dataset a5a0a16f-a013-4075-8ea4-40592cf92217: RuntimeError('Attempted to generate a URL without the application context being pushed. This has to be executed when application context is available.',)
Traceback (most recent call last):
  File "/home/adria/dev/pyenvs/ckan/bin/paster", line 11, in <module>
    sys.exit(run())
  File "/home/adria/dev/pyenvs/ckan/local/lib/python2.7/site-packages/paste/script/command.py", line 102, in run
    invoke(command, command_name, options, args[1:])
  File "/home/adria/dev/pyenvs/ckan/local/lib/python2.7/site-packages/paste/script/command.py", line 141, in invoke
    exit_code = runner.run(args)
  File "/home/adria/dev/pyenvs/ckan/local/lib/python2.7/site-packages/paste/script/command.py", line 236, in run
    result = self.command()
  File "/home/adria/dev/pyenvs/ckan/src/ckan/ckan/lib/cli.py", line 558, in command
    self.rebuild()
  File "/home/adria/dev/pyenvs/ckan/src/ckan/ckan/lib/cli.py", line 581, in rebuild
    quiet=self.options.quiet)
  File "/home/adria/dev/pyenvs/ckan/src/ckan/ckan/lib/search/__init__.py", line 192, in rebuild
    {'id': pkg_id}
  File "/home/adria/dev/pyenvs/ckan/src/ckan/ckan/logic/__init__.py", line 457, in wrapped
    result = _action(context, data_dict, **kw)
  File "/home/adria/dev/pyenvs/ckan/src/ckan/ckan/logic/action/get.py", line 1012, in package_show
    package_dict = model_dictize.package_dictize(pkg, context)
  File "/home/adria/dev/pyenvs/ckan/src/ckan/ckan/lib/dictization/model_dictize.py", line 204, in package_dictize
    result_dict["resources"] = resource_list_dictize(result, context)
  File "/home/adria/dev/pyenvs/ckan/src/ckan/ckan/lib/dictization/model_dictize.py", line 68, in resource_list_dictize
    resource_dict = resource_dictize(res, context)
  File "/home/adria/dev/pyenvs/ckan/src/ckan/ckan/lib/dictization/model_dictize.py", line 117, in resource_dictize
    qualified=True)
  File "/home/adria/dev/pyenvs/ckan/src/ckan/ckan/lib/helpers.py", line 257, in url_for
    my_url = _url_for_flask(*args, **kw)
  File "/home/adria/dev/pyenvs/ckan/src/ckan/ckan/lib/helpers.py", line 312, in _url_for_flask
    my_url = _flask_default_url_for(*args, **kw)
  File "/home/adria/dev/pyenvs/ckan/local/lib/python2.7/site-packages/flask/helpers.py", line 270, in url_for
    raise RuntimeError('Attempted to generate a URL without the '
RuntimeError: Attempted to generate a URL without the application context being pushed. This has to be executed when application context is available.

Why this is happening is explained here. As per Flask documentation, we need to create a test request context.

@amercader amercader self-assigned this Aug 17, 2017
amercader added a commit that referenced this issue Aug 23, 2017
This is an attempt to try to avoid having to explicitly generate a Flask
request context on all tests that involve url_for.

There are two scenarios when url_for is called outside the context of a
web request: tests and CLI.

The idea is to provide a fallback test request context on url_for if there
isn't one available. On tests, we have the added complexity that tests can
modify the configuration and plugins loaded so we can't reuse the same.

This is an ugly patch, as it uses to global variables. For CLI commands
there's a test request context created on `load_config`, which all
commands should run on startup.

For tests, the only safe place to create it is at the lowest level, on
`make_app`, as soon as the environment is loaded and there is a Flask
app available. Of course this only works if the tests create a test web
app, but that is the most common scenario (eg `FunctionalTestBase` or
any test that creates its own app).

Other tests can create their own test request context if needeed.
wardi added a commit that referenced this issue Aug 30, 2017
[#3760] Create a test request context automatically if missing in url_for
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant