#!/usr/bin/python # -*- encoding: utf-8 -*- import xmlrpc.client import argparse parser = argparse.ArgumentParser() parser.add_argument('--db', help='database') parser.add_argument('--name', help='lead name') parser.add_argument('--url', help='comanany url') parser.add_argument('--tags', help='tag') parser.add_argument('--desc', help='Notes or description') args = parser.parse_args() arg = vars(args) lead_data = {} lead_data['name'] = arg['name'] lead_data['tag'] = arg['tags'] lead_data['email'] = "" lead_data['phone'] = "" lead_data['url'] = arg['url'] lead_data['desc'] = arg['desc'] tag = arg['tags'] db = arg['db'] # see models # @api.model # def methods(self) # /data/apps/odoo/server/odoo/odoo/server/odoo/models.py # cat /data/apps/odoo/server/odoo/odoo/server/odoo/models.py | grep -C 1 "@api" | grep "def _" # @api.multi # def method(self, args): # may be called in both record and traditional styles, like: # recs = model.browse(cr, uid, ids, context) # recs.method(args) # model.method(cr, uid, ids, args, context=context) # db = 'tornae01' # db = 'test01' password = 'xxxx' url_odoo = 'http://perp05.lxd:8069' username = 'agalan@tornae.com' def test(): print("test") info = xmlrpc.client.ServerProxy('https://demo.odoo.com/start').start() url, db, username, password = info['host'], info['database'], info[ 'user'], info['password'] print(url, db, username, password) def odoo_auth(url, db, username, password): # loging in common = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(url)) # print(common.version()) uid = common.authenticate(db, username, password, {}) return uid # print(username) def odoo_call(uid): models = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url_odoo)) #print(db,uid,password) models.execute_kw(db, uid, password, 'res.partner', 'check_access_rights', ['read'], {'raise_exception': False}) a = models.execute_kw( db, uid, password, 'res.partner', 'search', [[['is_company', '=', True], ['customer', '=', True]]]) models.execute_kw(db, uid, password, 'res.partner', 'fields_get', [], {'attributes': ['string', 'help', 'type']}) #print(a) def odoo_update_crm_lead(uid, partner_id): models = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url_odoo)) models.execute_kw(db, uid, password, 'crm.lead', 'write', [[partner_id], { "tags_id": [(6, 0, [20])] }]) def odoo_create_crm_tag(uid, partner_id, tags_id): models = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url_odoo)) models.execute_kw(db, uid, password, 'crm.lead', 'write', [[partner_id], { "tag_ids": [(6, 0, [tags_id])] }]) def odoo_create_crm_lead(uid, data): models = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url_odoo)) #print(db,uid,password) models.execute_kw(db, uid, password, 'crm.lead', 'create', [{ 'name': data['name'], 'email_from': data['email'], 'phone': data['phone'], 'x_website': data['url'], 'description': data['desc'], 'user_id': 1 }]) def odoo_exist_crm_lead(uid, data): models = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url_odoo)) # Search if exist on active print(data['name']) a = models.execute_kw(db, uid, password, 'crm.lead', 'search', [[['name', '=', data['name']]]]) # Search if not active b = models.execute_kw( db, uid, password, 'crm.lead', 'search', [[['name', '=', data['name']], ['active', '=', False]]]) if (len(a) > 0): return a[0] else: if (len(b) > 0): return b[0] else: return False def odoo_exist_tag(uid, tag): models = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url_odoo)) a = models.execute_kw(db, uid, password, 'crm.lead.tag', 'search', [[['name', '=', tag]]]) if (len(a) > 0): return a[0] else: return False def odoo_create_tag(uid, tag, parent_id): models = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url_odoo)) models.execute_kw(db, uid, password, 'crm.lead.tag', 'create', [{ 'name': tag }]) uid = odoo_auth(url_odoo, db, username, password) if (odoo_exist_crm_lead(uid, lead_data)): print("already created") else: odoo_create_crm_lead(uid, lead_data) lead_id = odoo_exist_crm_lead(uid, lead_data) print(lead_id) if (odoo_exist_tag(uid, tag)): print(odoo_exist_tag(uid, tag)) else: odoo_create_tag(uid, tag, lead_id) tags_id = odoo_exist_tag(uid, tag) print(uid, lead_id, tags_id) odoo_create_crm_tag(uid, lead_id, tags_id) # models = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url_odoo))