Skip to content

Commit

Permalink
[#1500] Mock pylons context object on paster commands
Browse files Browse the repository at this point in the history
When calling a paster command, we create and register a Pylons context
object (c), and add to site user to it (c.user and c.auth_user_object).
This ensures that the auth will work in exactly the same way as in a web
request. Otherwise you can find problems like the harvest and DataPusher
incompatibility.

We already did a similar thing registering a MockTranslator for the
thread.
  • Loading branch information
amercader committed Jun 13, 2014
1 parent f55d291 commit cb0674c
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions ckan/lib/cli.py
Expand Up @@ -6,6 +6,8 @@
import sys
from pprint import pprint
import re
import ckan.logic as logic
import ckan.model as model
import ckan.include.rjsmin as rjsmin
import ckan.include.rcssmin as rcssmin
import ckan.lib.fanstatic_resources as fanstatic_resources
Expand Down Expand Up @@ -101,6 +103,19 @@ def _load_config(self):
self.translator_obj = MockTranslator()
self.registry.register(pylons.translator, self.translator_obj)

if model.user_table.exists():
# If the DB has already been initialized, create and register
# a pylons context object, and add the site user to it, so the
# auth works as in a normal web request
c = pylons.util.AttribSafeContextObj()

self.registry.register(pylons.c, c)

self.site_user = logic.get_action('get_site_user')({'ignore_auth': True}, {})

pylons.c.user = self.site_user['name']
pylons.c.userobj = model.User.get(self.site_user['name'])

## give routes enough information to run url_for
parsed = urlparse.urlparse(conf.get('ckan.site_url', 'http://0.0.0.0'))
request_config = routes.request_config()
Expand Down Expand Up @@ -144,6 +159,7 @@ def command(self):

cmd = self.args[0]
if cmd == 'init':

model.repo.init_db()
if self.verbose:
print 'Initialising DB: SUCCESS'
Expand Down

0 comments on commit cb0674c

Please sign in to comment.