From a7382b2309e1f7a02aebab52c1c17fd77b5c5541 Mon Sep 17 00:00:00 2001 From: Ludovic CHEVALIER Date: Fri, 17 Feb 2017 16:43:04 +0100 Subject: [PATCH] Initial commit --- alcatel_export.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 alcatel_export.py diff --git a/alcatel_export.py b/alcatel_export.py new file mode 100644 index 0000000..01ea6d9 --- /dev/null +++ b/alcatel_export.py @@ -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 = '\n%s' % etree.tostring(root, pretty_print=True) +print(content) -- 2.20.1