Initial commit
authorLudovic CHEVALIER <ludovic.chevalier@heureux-cyclage.org>
Fri, 17 Feb 2017 15:43:04 +0000 (16:43 +0100)
committerLudovic CHEVALIER <ludovic.chevalier@heureux-cyclage.org>
Fri, 17 Feb 2017 15:43:04 +0000 (16:43 +0100)
alcatel_export.py [new file with mode: 0644]

diff --git a/alcatel_export.py b/alcatel_export.py
new file mode 100644 (file)
index 0000000..01ea6d9
--- /dev/null
@@ -0,0 +1,62 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+import psycopg2
+from lxml import etree
+
+# variables
+db_name = "heureuxcyclage"
+db_user = "bikecoop"
+db_host = "localhost"
+limit = 0
+
+
+# sql connect
+conn = psycopg2.connect("dbname=%s user=%s host=%s" % (db_name, db_user, db_host))
+cur = conn.cursor()
+
+# import datas in dicos
+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 is_company = True"
+cur.execute(query)
+
+root = etree.Element('directory')
+group = etree.SubElement(root, 'DIR_GROUP')
+
+line = 0
+for row in cur:
+    line += 1
+    fields = {
+        'LINE_NUMBER': '0',
+        'BLOCK': '0',
+        'RINGER': '0',
+    }
+    if row[1]:
+        fields['NUMBER_WORK'] = unicode(str(row[1]), 'utf-8')
+    if row[2]:
+        fields['NUMBER_MOBILE'] = unicode(str(row[2]), 'utf-8')
+    if row[3]:
+        fields['NAME_LAST'] = unicode(row[0], 'utf-8')
+    else:
+        name = unicode(row[0], 'utf-8')
+        try:
+            pspace = name.index(' ')
+            fields['NAME_FIRST'] = name[:pspace]
+            fields['NAME_LAST'] = name[pspace+1:]
+        except ValueError:
+            fields['NAME_FIRST'] = name
+
+    entry = etree.SubElement(group, 'DIR_ENTRY')
+
+    for field in fields:
+        entry_field = etree.SubElement(entry, 'DIR_ENTRY_%s' % field.upper())
+        entry_field.text = fields[field]
+
+    if limit:
+        if line == limit:
+            break
+
+# sql disconnect
+cur.close()
+conn.close()
+
+content = '<?xml version="1.0"?>\n%s' % etree.tostring(root, pretty_print=True)
+print(content)