~camptocamp/partner-contact-management/partner_firstname_vre_view_change

« back to all changes in this revision

Viewing changes to partner_firstname/partner.py

  • Committer: nicolas.bessi at camptocamp
  • Date: 2013-01-14 14:12:29 UTC
  • mfrom: (4.1.1 partner-contact-management)
  • Revision ID: nicolas.bessi@camptocamp.com-20130114141229-9fvz9cvrh1jtxysc
[FIX] encoding declaration

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    Author: Nicolas Bessi. Copyright Camptocamp SA
 
5
#
 
6
#    This program is free software: you can redistribute it and/or modify
 
7
#    it under the terms of the GNU Affero General Public License as
 
8
#    published by the Free Software Foundation, either version 3 of the
 
9
#    License, or (at your option) any later version.
 
10
#
 
11
#    This program is distributed in the hope that it will be useful,
 
12
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
#    GNU Affero General Public License for more details.
 
15
#
 
16
#    You should have received a copy of the GNU Affero General Public License
 
17
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
#
 
19
##############################################################################
 
20
from openerp.osv.orm import Model, fields
 
21
 
 
22
 
 
23
class ResPartner(Model):
 
24
    """Adds lastname and firstname, name become a stored function field"""
 
25
 
 
26
    _inherit = 'res.partner'
 
27
 
 
28
    def init(self, cursor):
 
29
        cursor.execute('SELECT id FROM res_partner WHERE lastname IS NOT NULL')
 
30
        if not cursor.fetchone():
 
31
            cursor.execute('UPDATE res_partner set lastname = name WHERE name IS NOT NULL')
 
32
 
 
33
    def _compute_name_custom(self, cursor, uid, ids, fname, arg, context=None):
 
34
        res = {}
 
35
        for rec in self.read(cursor, uid, ids, ['firstname', 'lastname']):
 
36
            name = rec['lastname'] + (u" " + rec['firstname'] if rec['firstname'] else u"")
 
37
            res[rec['id']] = name
 
38
        return res
 
39
 
 
40
    _columns = {'name': fields.function(_compute_name_custom, string="Name",
 
41
                                        type="char", store=True,
 
42
                                        select=True, readonly=True),
 
43
 
 
44
                'firstname': fields.char("Firstname"),
 
45
                'lastname': fields.char("Lastname", required=True)}
 
46
 
 
47
    def create(self, cursor, uid, vals, context=None):
 
48
        """To support data backward compatibility"""
 
49
        to_use = vals
 
50
        if vals.get('name'):
 
51
            corr_vals = vals.copy()
 
52
            corr_vals['lastname'] = corr_vals['name']
 
53
            del(corr_vals['name'])
 
54
            to_use = corr_vals
 
55
        return super(ResPartner, self).create(cursor, uid, to_use, context=context)
 
56
 
 
57
    def write(self, cursor, uid, ids, vals, context=None):
 
58
        """To support data backward compatibility"""
 
59
        to_use = vals
 
60
        if vals.get('name'):
 
61
            corr_vals = vals.copy()
 
62
            corr_vals['lastname'] = corr_vals['name']
 
63
            del(corr_vals['name'])
 
64
            to_use = corr_vals
 
65
        return super(ResPartner, self).write(cursor, uid, ids, to_use, context=context)