doc/book/en/annexes/cookbook.rst
author sylvain.thenault@logilab.fr
Thu, 14 May 2009 12:48:11 +0200
changeset 1808 aa09e20dd8c0
parent 1675 doc/book/en/D020-cookbook.txt@26f9d2a1a553
parent 1714 doc/book/en/D020-cookbook.txt@a721966779be
child 2172 cf8f9180e63e
permissions -rw-r--r--
backport tls-sprint

.. -*- coding: utf-8 -*-

Cook book
=========

We gathered together some of our tricks and scripts that could make
life easier.


* How to import LDAP users in `CubicWeb`?

  Here is a very useful script which enables you to import LDAP users
  into your `CubicWeb` application by running the following: ::


    import os
    import pwd
    import sys

    from logilab.common.db import get_connection

    def getlogin():
        """avoid usinng os.getlogin() because of strange tty / stdin problems
        (man 3 getlogin)
        Another solution would be to use $LOGNAME, $USER or $USERNAME
        """
        return pwd.getpwuid(os.getuid())[0]


    try:
        database = sys.argv[1]
    except IndexError:
        print 'USAGE: python ldap2system.py <database>'
        sys.exit(1)

    if raw_input('update %s db ? [y/n]: ' % database).strip().lower().startswith('y'):
        cnx = get_connection(user=getlogin(), database=database)
        cursor = cnx.cursor()

        insert = ('INSERT INTO euser (creation_date, eid, modification_date, login, firstname, surname, last_login_time, upassword) '
                  "VALUES (%(mtime)s, %(eid)s, %(mtime)s, %(login)s, %(firstname)s, %(surname)s, %(mtime)s, './fqEz5LeZnT6');")
        update = "UPDATE entities SET source='system' WHERE eid=%(eid)s;"
        cursor.execute("SELECT eid,type,source,extid,mtime FROM entities WHERE source!='system'")
        for eid, type, source, extid, mtime in cursor.fetchall():
            if type != 'CWUser':
                print "don't know what to do with entity type", type
                continue
            if source != 'ldapuser':
                print "don't know what to do with source type", source
                continue
            ldapinfos = dict(x.strip().split('=') for x in extid.split(','))
            login = ldapinfos['uid']
            firstname = ldapinfos['uid'][0].upper()
            surname = ldapinfos['uid'][1:].capitalize()
            if login != 'jcuissinat':
                args = dict(eid=eid, type=type, source=source, login=login,
                            firstname=firstname, surname=surname, mtime=mtime)
                print args
                cursor.execute(insert, args)
                cursor.execute(update, args)

        cnx.commit()
        cnx.close()


* How to load data from a script?

  The following script aims at loading data within a script assuming pyro-nsd is
  running and your application is configured with ``pyro-server=yes``, otherwise
  you would not be able to use dbapi. ::

    from cubicweb import dbapi
        
    cnx = dbapi.connection(database='instance-id', user='admin', password='admin')
    cur = cnx.cursor()
    for name in ('Personal', 'Professional', 'Computers'):
        cur.execute('INSERT Blog B: B name %s', name)
    cnx.commit()