# HG changeset patch # User Aurelien Campeas # Date 1417689438 -3600 # Node ID 78ab2b14048a6f6f79813163c24ded5eecf5f6ca # Parent 6fd167b140cd8e65227c8e132c3a7ed421b3d0c8 [utils] provide a function to return an admin connection from an appid Closes #4723580. diff -r 6fd167b140cd -r 78ab2b14048a doc/3.20.rst --- a/doc/3.20.rst Fri Dec 05 12:08:13 2014 +0100 +++ b/doc/3.20.rst Thu Dec 04 11:37:18 2014 +0100 @@ -15,6 +15,9 @@ Both of these features are described in `CWEP-002`_, and the updated "Data model" chapter of the CubicWeb book. +* cubicweb-ctl plugins can use the ``cubicweb.utils.admincnx`` function + to get a Connection object from an instance name. + .. _CWEP-002: http://hg.logilab.org/review/cwep/file/tip/CWEP-002.rst diff -r 6fd167b140cd -r 78ab2b14048a utils.py --- a/utils.py Fri Dec 05 12:08:13 2014 +0100 +++ b/utils.py Thu Dec 04 11:37:18 2014 +0100 @@ -1,4 +1,4 @@ -# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr # # This file is part of CubicWeb. @@ -47,6 +47,20 @@ # initialize random seed from current time random.seed() +def admincnx(appid): + from cubicweb.cwconfig import CubicWebConfiguration + from cubicweb.server.repository import Repository + from cubicweb.server.utils import TasksManager + config = CubicWebConfiguration.config_for(appid) + + login = config.default_admin_config['login'] + password = config.default_admin_config['password'] + + repo = Repository(config, TasksManager()) + session = repo.new_session(login, password=password) + return session.new_cnx() + + def make_uid(key=None): """Return a unique identifier string.