root/management/bs-admin/trunk/sync_elgg_trac.py

Revision 184, 1.7 kB (checked in by gallardj, 10 months ago)

Fixed a postgres quoting problem

  • Property svn:executable set to *
Line 
1 #!/usr/bin/python
2 import psycopg2
3
4 try:
5   conn = psycopg2.connect("dbname='elgg' user='user' host='localhost' password='changeme'")
6 except:
7   print "Unable to connect to the db"
8
9 try:
10   conn2 = psycopg2.connect("dbname='project_meta' host='localhost' user='user' password='changeme'")
11 except:
12   print "Unable to connect to the db"
13
14 sql = "SELECT username FROM elgg_users"
15 sql2 = "SELECT short_name FROM project_request_project"
16
17 cursor = conn.cursor()
18 cursor2 = conn2.cursor()
19
20 elgg_shortnames = []
21 pm_shortnames = []
22 diff_shortnames = []
23
24 cursor.execute(sql)
25 rows = cursor.fetchall()
26
27 for row in rows:
28   elgg_shortnames.append(row[0])
29
30 cursor2.execute(sql2)
31
32 rows2 = cursor2.fetchall()
33
34 for row in rows2:
35   pm_shortnames.append(row[0])
36
37 diff = set(pm_shortnames).difference(set(elgg_shortnames))
38
39 for each in diff:
40   diff_shortnames.append(each)
41
42 for shortname in diff_shortnames:
43   username_sql = "SELECT username FROM auth_user WHERE id=(SELECT owner_id FROM project_request_project WHERE short_name='%s')" % (shortname)
44   project_name_sql = "SELECT name FROM project_request_project WHERE short_name='%s'" % (shortname)
45   cursor2.execute(username_sql)
46   rows = cursor2.fetchall()
47   username = rows[0][0]
48   cursor2.execute(project_name_sql)
49   rows = cursor2.fetchall()
50   project_name = rows[0][0]
51
52   sql = "SELECT ident FROM elgg_users WHERE username='%s'" % (username)
53   cursor.execute(sql)
54   value = cursor.fetchall()
55   try:
56     ownerid = value[0][0]
57   except:
58     print value
59     print username
60     continue
61   sql = "INSERT INTO elgg_users (username, name, active, owner, user_type, moderation) VALUES ('%s', '%s', 'yes', '%s', 'project', 'no');" % (shortname, project_name, ownerid)
62   cursor.execute(sql)
63   conn.commit()
64  
Note: See TracBrowser for help on using the browser.