~select usual contacts
[lhc/oe_alcatel_export.git] / alcatel_export.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 import psycopg2
4 from lxml import etree
5
6 # variables
7 db_name = "heureuxcyclage"
8 db_user = "bikecoop"
9 db_host = "localhost"
10 limit = 0
11
12
13 # sql connect
14 conn = psycopg2.connect("dbname=%s user=%s host=%s" % (db_name, db_user, db_host))
15 cur = conn.cursor()
16
17 # import datas in dicos
18 query = "SELECT name, phone, mobile, is_company from res_partner where name != '/' and name is not null and (phone is not null or mobile is not null) and active = True and usual_contact = True"
19 cur.execute(query)
20
21 root = etree.Element('directory')
22 group = etree.SubElement(root, 'DIR_GROUP')
23
24 line = 0
25 for row in cur:
26 line += 1
27 fields = {
28 'LINE_NUMBER': '0',
29 'BLOCK': '0',
30 'RINGER': '0',
31 }
32 if row[1]:
33 fields['NUMBER_WORK'] = unicode(str(row[1]), 'utf-8')
34 if row[2]:
35 fields['NUMBER_MOBILE'] = unicode(str(row[2]), 'utf-8')
36 if row[3]:
37 fields['NAME_LAST'] = unicode(row[0], 'utf-8')
38 else:
39 name = unicode(row[0], 'utf-8')
40 try:
41 pspace = name.index(' ')
42 fields['NAME_FIRST'] = name[:pspace]
43 fields['NAME_LAST'] = name[pspace+1:]
44 except ValueError:
45 fields['NAME_FIRST'] = name
46
47 entry = etree.SubElement(group, 'DIR_ENTRY')
48
49 for field in fields:
50 entry_field = etree.SubElement(entry, 'DIR_ENTRY_%s' % field.upper())
51 entry_field.text = fields[field]
52
53 if limit:
54 if line == limit:
55 break
56
57 # sql disconnect
58 cur.close()
59 conn.close()
60
61 content = '<?xml version="1.0"?>\n%s' % etree.tostring(root, pretty_print=True)
62 print(content)