[utils] provide a function to return an admin connection from an appid
authorAurelien Campeas <aurelien.campeas@logilab.fr>
Thu, 04 Dec 2014 11:37:18 +0100
changeset 10118 78ab2b14048a
parent 10115 6fd167b140cd
child 10119 3d7e500a0202
[utils] provide a function to return an admin connection from an appid Closes #4723580.
doc/3.20.rst
utils.py
--- 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
 
 
--- 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.