[FIX] base_contact: Modify the migration script
bzr revid: stw@openerp.com-20111209165531-uiqfpmlfzba6wr81
This commit is contained in:
@@ -20,7 +20,7 @@
|
||||
##############################################################################
|
||||
|
||||
import base_contact
|
||||
import base_contact_installer
|
||||
#import base_contact_installer
|
||||
|
||||
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
||||
|
||||
|
||||
@@ -48,8 +48,8 @@ Pay attention that this module converts the existing addresses into "addresses +
|
||||
'security/base_contact_security.xml',
|
||||
'security/ir.model.access.csv',
|
||||
'base_contact_view.xml',
|
||||
'base_contact_installer_view.xml',
|
||||
#'process/base_contact_process.xml'
|
||||
# 'base_contact_installer_view.xml',
|
||||
'process/base_contact_process.xml'
|
||||
],
|
||||
'demo_xml': ['base_contact_demo.xml'],
|
||||
'test': [
|
||||
|
||||
@@ -61,31 +61,6 @@ class res_partner_contact(osv.osv):
|
||||
|
||||
_order = "name,first_name"
|
||||
|
||||
def name_get2(self, cr, user, ids, context=None):
|
||||
|
||||
""" will return name and first_name.......
|
||||
@param self: The object pointer
|
||||
@param cr: the current row, from the database cursor,
|
||||
@param user: the current user’s ID for security checks,
|
||||
@param ids: List of create menu’s IDs
|
||||
@return: name and first_name
|
||||
@param context: A standard dictionary for contextual values
|
||||
"""
|
||||
|
||||
if not len(ids):
|
||||
return []
|
||||
res = []
|
||||
for contact in self.browse(cr, user, ids, context=context):
|
||||
_contact = ""
|
||||
if contact.title:
|
||||
_contact += "%s "%(contact.title.name)
|
||||
_contact += contact.name or ""
|
||||
if contact.name and contact.first_name:
|
||||
_contact += " "
|
||||
_contact += contact.first_name or ""
|
||||
res.append((contact.id, _contact))
|
||||
return res
|
||||
|
||||
def name_search(self, cr, uid, name='', args=None, operator='ilike', context=None, limit=None):
|
||||
if not args:
|
||||
args = []
|
||||
@@ -111,16 +86,33 @@ res_partner()
|
||||
class res_partner_location(osv.osv):
|
||||
_name = 'res.partner.location'
|
||||
_inherit = 'res.partner.address'
|
||||
#_table = 'res_partner_address'
|
||||
_columns = {
|
||||
'job_ids': fields.one2many('res.partner.address', 'location_id', 'Contacts'),
|
||||
}
|
||||
|
||||
def _auto_init(self, cr, context=None):
|
||||
def table_exists(view_name):
|
||||
cr.execute('SELECT count(relname) FROM pg_class WHERE relname = %s', (view_name,))
|
||||
value = cr.fetchone()[0]
|
||||
return bool(value == 1)
|
||||
|
||||
exists = table_exists(self._table)
|
||||
super(res_partner_location, self)._auto_init(cr, context)
|
||||
|
||||
if not exists:
|
||||
sequence_name = self.pool.get('res.partner.address')._sequence
|
||||
cr.execute("SELECT last_value FROM " + sequence_name)
|
||||
last_sequence = cr.fetchone()[0]
|
||||
|
||||
cr.execute("INSERT INTO res_partner_location SELECT * FROM res_partner_address")
|
||||
cr.execute("ALTER SEQUENCE " + self._sequence + " RESTART WITH " + str(last_sequence))
|
||||
|
||||
|
||||
res_partner_location()
|
||||
|
||||
class res_partner_address(osv.osv):
|
||||
_name = 'res.partner.address'
|
||||
_inherits = { 'res.partner.location' : 'location_id' }
|
||||
_table = 'res_partner_job'
|
||||
|
||||
_columns = {
|
||||
'location_id' : fields.many2one('res.partner.location', 'Location'),
|
||||
@@ -141,6 +133,19 @@ class res_partner_address(osv.osv):
|
||||
'state': 'current',
|
||||
}
|
||||
|
||||
def _auto_init(self, cr, context=None):
|
||||
def column_exists(column):
|
||||
cr.execute("select count(attname) from pg_attribute where attrelid = \
|
||||
(select oid from pg_class where relname = %s) \
|
||||
and attname = %s", (self._table, column,))
|
||||
value = cr.fetchone()[0]
|
||||
return bool(value == 1)
|
||||
|
||||
exists = column_exists('location_id')
|
||||
super(res_partner_address, self)._auto_init(cr, context)
|
||||
|
||||
if not exists:
|
||||
cr.execute("UPDATE res_partner_address SET location_id = id")
|
||||
|
||||
res_partner_address()
|
||||
|
||||
|
||||
@@ -34,33 +34,41 @@ class base_contact_installer(osv.osv_memory):
|
||||
"""
|
||||
This function is used to create contact and address from existing partner address
|
||||
"""
|
||||
obj = self.pool.get("base.contact.installer").browse(cr, uid, uid, context=context)
|
||||
if obj.migrate:
|
||||
# Enable PL/pgSQL if not enabled yet in the database
|
||||
cr.execute("SELECT 1 FROM pg_language WHERE lanname = 'plpgsql'")
|
||||
if not cr.fetchone():
|
||||
cr.execute("CREATE LANGUAGE plpgsql;")
|
||||
obj = self.browse(cr, uid, uid, context=context)
|
||||
if not obj.migrate:
|
||||
return
|
||||
|
||||
cr.execute("""DROP TRIGGER IF EXISTS contactjob on res_partner_contact;
|
||||
CREATE OR REPLACE FUNCTION add_to_job() RETURNS TRIGGER AS $contactjob$
|
||||
DECLARE
|
||||
new_name varchar;
|
||||
new_phonenum varchar;
|
||||
BEGIN
|
||||
IF(TG_OP='INSERT') THEN
|
||||
INSERT INTO res_partner_job(contact_id, address_id, function, state) VALUES(NEW.id, NEW.website::integer,NEW.first_name, 'current');
|
||||
UPDATE res_partner_contact set first_name=Null, website=Null, active=True where id=NEW.id;
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$contactjob$ LANGUAGE plpgsql;
|
||||
CREATE TRIGGER contactjob AFTER INSERT ON res_partner_contact FOR EACH ROW EXECUTE PROCEDURE add_to_job();""")
|
||||
# We translate address to location
|
||||
# Risk to have a shift with the current data
|
||||
cr.execute("INSERT INTO res_partner_location SELECT * FROM res_partner_address")
|
||||
|
||||
cr.execute("INSERT into res_partner_contact (name, title, email, first_name, website) (SELECT coalesce(name, 'Noname'), title, email, function , to_char(id, '99999999') from res_partner_address)")
|
||||
cr.execute("ALTER TABLE res_partner_address RENAME TO res_parter_location")
|
||||
|
||||
cr.execute("DROP TRIGGER IF EXISTS contactjob on res_partner_contact")
|
||||
# Enable PL/pgSQL if not enabled yet in the database
|
||||
cr.execute("SELECT 1 FROM pg_language WHERE lanname = 'plpgsql'")
|
||||
if not cr.fetchone():
|
||||
cr.execute("CREATE LANGUAGE plpgsql;")
|
||||
|
||||
cr.execute("DROP FUNCTION IF EXISTS add_to_job()")
|
||||
cr.execute("""DROP TRIGGER IF EXISTS contactjob on res_partner_contact;
|
||||
CREATE OR REPLACE FUNCTION add_to_job() RETURNS TRIGGER AS $contactjob$
|
||||
DECLARE
|
||||
new_name varchar;
|
||||
new_phonenum varchar;
|
||||
BEGIN
|
||||
IF(TG_OP='INSERT') THEN
|
||||
INSERT INTO res_partner_job(contact_id, address_id, function, state) VALUES(NEW.id, NEW.website::integer,NEW.first_name, 'current');
|
||||
UPDATE res_partner_contact set first_name=Null, website=Null, active=True where id=NEW.id;
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$contactjob$ LANGUAGE plpgsql;
|
||||
CREATE TRIGGER contactjob AFTER INSERT ON res_partner_contact FOR EACH ROW EXECUTE PROCEDURE add_to_job();""")
|
||||
|
||||
cr.execute("INSERT into res_partner_contact (name, title, email, first_name, website) (SELECT coalesce(name, 'Noname'), title, email, function , to_char(id, '99999999') from res_partner_address)")
|
||||
|
||||
cr.execute("DROP TRIGGER IF EXISTS contactjob on res_partner_contact")
|
||||
|
||||
cr.execute("DROP FUNCTION IF EXISTS add_to_job()")
|
||||
|
||||
base_contact_installer()
|
||||
|
||||
|
||||
@@ -165,7 +165,7 @@
|
||||
<field name="fax"/>
|
||||
<field name="email" widget="email"/>
|
||||
</group>
|
||||
<field name="job_ids" mode="tree,form" nolabel="1" colspan="4" context="{'default_is_job' : True}" default_get="{'is_job' : True}">
|
||||
<field name="job_ids" mode="tree,form" nolabel="1" colspan="4">
|
||||
<tree string="Contacts" editable="top">
|
||||
<field name="contact_id"/>
|
||||
<field name="function"/>
|
||||
|
||||
Reference in New Issue
Block a user