1 .. -*- coding: utf-8 -*- |
|
2 |
|
3 Cook book |
|
4 ========= |
|
5 |
|
6 We gathered together some of our tricks and scripts that could make |
|
7 life easier. |
|
8 |
|
9 |
|
10 * How to import LDAP users in *CubicWeb*? |
|
11 |
|
12 [XXX distribute this script with cubicweb instead] |
|
13 |
|
14 Here is a very useful script which enables you to import LDAP users |
|
15 into your *CubicWeb* instance by running the following: |
|
16 |
|
17 .. sourcecode:: python |
|
18 |
|
19 import os |
|
20 import pwd |
|
21 import sys |
|
22 |
|
23 from logilab.common.db import get_connection |
|
24 |
|
25 def getlogin(): |
|
26 """avoid usinng os.getlogin() because of strange tty / stdin problems |
|
27 (man 3 getlogin) |
|
28 Another solution would be to use $LOGNAME, $USER or $USERNAME |
|
29 """ |
|
30 return pwd.getpwuid(os.getuid())[0] |
|
31 |
|
32 |
|
33 try: |
|
34 database = sys.argv[1] |
|
35 except IndexError: |
|
36 print 'USAGE: python ldap2system.py <database>' |
|
37 sys.exit(1) |
|
38 |
|
39 if raw_input('update %s db ? [y/n]: ' % database).strip().lower().startswith('y'): |
|
40 cnx = get_connection(user=getlogin(), database=database) |
|
41 cursor = cnx.cursor() |
|
42 |
|
43 insert = ('INSERT INTO euser (creation_date, eid, modification_date, login, firstname, surname, last_login_time, upassword) ' |
|
44 "VALUES (%(mtime)s, %(eid)s, %(mtime)s, %(login)s, %(firstname)s, %(surname)s, %(mtime)s, './fqEz5LeZnT6');") |
|
45 update = "UPDATE entities SET source='system' WHERE eid=%(eid)s;" |
|
46 cursor.execute("SELECT eid,type,source,extid,mtime FROM entities WHERE source!='system'") |
|
47 for eid, type, source, extid, mtime in cursor.fetchall(): |
|
48 if type != 'CWUser': |
|
49 print "don't know what to do with entity type", type |
|
50 continue |
|
51 if source != 'ldapuser': |
|
52 print "don't know what to do with source type", source |
|
53 continue |
|
54 ldapinfos = dict(x.strip().split('=') for x in extid.split(',')) |
|
55 login = ldapinfos['uid'] |
|
56 firstname = ldapinfos['uid'][0].upper() |
|
57 surname = ldapinfos['uid'][1:].capitalize() |
|
58 if login != 'jcuissinat': |
|
59 args = dict(eid=eid, type=type, source=source, login=login, |
|
60 firstname=firstname, surname=surname, mtime=mtime) |
|
61 print args |
|
62 cursor.execute(insert, args) |
|
63 cursor.execute(update, args) |
|
64 |
|
65 cnx.commit() |
|
66 cnx.close() |
|
67 |
|
68 |
|
69 * How to load data from a script? |
|
70 |
|
71 The following script aims at loading data within a script assuming pyro-nsd is |
|
72 running and your instance is configured with ``pyro-server=yes``, otherwise |
|
73 you would not be able to use dbapi. |
|
74 |
|
75 .. sourcecode:: python |
|
76 |
|
77 from cubicweb import dbapi |
|
78 |
|
79 cnx = dbapi.connection(database='instance-id', user='admin', password='admin') |
|
80 cur = cnx.cursor() |
|
81 for name in ('Personal', 'Professional', 'Computers'): |
|
82 cur.execute('INSERT Blog B: B name %s', name) |
|
83 cnx.commit() |
|
84 |
|
85 |
|