~camptocamp/partner-contact-management/7.0-add_birthdate_nbi

« back to all changes in this revision

Viewing changes to partner_address_ldap/wizard/wiz_import_adresses.py

  • Committer: Joël Grand-Guillaume
  • Date: 2011-08-12 12:33:12 UTC
  • Revision ID: joel.grandguillaume@camptocamp.com-20110812123312-b4tbq3s80ztvn5d4
[ADD] First commit of the first generic modules to move in our new public branch
(lp:c2c-addons/6.1  rev 1)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
# Copyright (c) 2010 Camptocamp SA (http://www.camptocamp.com)
 
5
# All Right Reserved
 
6
#
 
7
# Author : Vincent Renaville
 
8
#
 
9
# WARNING: This program as such is intended to be used by professional
 
10
# programmers who take the whole responsability of assessing all potential
 
11
# consequences resulting from its eventual inadequacies and bugs
 
12
# End users who are looking for a ready-to-use solution with commercial
 
13
# garantees and support are strongly adviced to contract a Free Software
 
14
# Service Company
 
15
#
 
16
# This program is Free Software; you can redistribute it and/or
 
17
# modify it under the terms of the GNU General Public License
 
18
# as published by the Free Software Foundation; either version 2
 
19
# of the License, or (at your option) any later version.
 
20
#
 
21
# This program is distributed in the hope that it will be useful,
 
22
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
23
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
24
# GNU General Public License for more details.
 
25
#
 
26
# You should have received a copy of the GNU General Public License
 
27
# along with this program; if not, write to the Free Software
 
28
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
29
#
 
30
##############################################################################
 
31
 
 
32
#  init_import.py
 
33
#
 
34
#  Created by Nicolas Bessi on 28.04.09.
 
35
#  Copyright (c) 2009 CamptoCamp. All rights reserved.
 
36
#
 
37
 
 
38
import wizard
 
39
import pooler
 
40
import base64
 
41
import unicodedata
 
42
import netsvc
 
43
import re
 
44
_FORM = '''<?xml version="1.0"?>
 
45
<form string="Export adresses to ldap">
 
46
</form>'''
 
47
 
 
48
 
 
49
_FORM1 = """<?xml version="1.0"?>
 
50
<form string="Export log">
 
51
<separator colspan="4" string="Clic on 'Save as' to save the log file :" />
 
52
    <field name="errors"/>
 
53
</form>
 
54
"""
 
55
 
 
56
_FIELDS = {
 
57
    'errors': {
 
58
        'string': 'Error report',
 
59
        'type': 'binary',
 
60
        'readonly': True,
 
61
    },
 
62
}
 
63
 
 
64
### As this is a bulck batch wizzard the performance process was not reallay taken in account ###
 
65
##The ideal way of doing would be to modify the  connexion settings in order to have a connexion singelton
 
66
## in the file partner.py it will avoid connexion renegotiation for each partner.
 
67
def _action_import_adresses(self, cr, uid, data, context):
 
68
    """ This function create or update each adresses present in the database.
 
69
    It will also genreate an error report"""
 
70
    logger = netsvc.Logger()
 
71
    error_report = [u'Error report']
 
72
    add_obj = pooler.get_pool(cr.dbname).get('res.partner.address')
 
73
    add_ids = add_obj.search(cr,uid,[])
 
74
    addresses = add_obj.browse(cr, uid, add_ids)
 
75
    phone_fields = ['phone','fax','mobile','private_phone']
 
76
    for add in addresses :
 
77
        vals = {}
 
78
        vals['partner_id'] = add.partner_id.id
 
79
        vals['email'] = add.email
 
80
        vals['phone'] = add.phone
 
81
        vals['fax'] = add.fax
 
82
        vals['mobile'] = add.mobile
 
83
        vals['firstname'] = add.firstname
 
84
        vals['lastname'] = add.lastname
 
85
        vals['private_phone'] = add.private_phone
 
86
        vals['street'] = add.street
 
87
        vals['street2'] = add.street2
 
88
        vals['city'] = add.city
 
89
        # Validating the mail
 
90
        if add.email :
 
91
            if re.match(
 
92
                    "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", add.email) is None or\
 
93
               re.search(u"[éèàêöüäï&]", add.email) is not None:
 
94
                msg=u'Addresse %s for partner  %s has email that is invalid %s'%(
 
95
                    unicode(vals['firstname']) +' '+ unicode(vals['lastname']),
 
96
                    add.partner_id.name,
 
97
                    unicode(add.email)
 
98
                    )
 
99
                logger.notifyChannel('ldap export', netsvc.LOG_INFO, msg)
 
100
                error_report.append(msg)
 
101
                vals['email'] = False
 
102
        # Validating the Phone
 
103
        for key in phone_fields :
 
104
            if not unicode(vals[key]).startswith('+')  or unicode(vals[key]).find("\n") != -1\
 
105
            or re.search(u"[éèàêöüä#&]", unicode(vals[key])) is not None:
 
106
                vals[key] = False
 
107
                msg = u'Addresse %s for partner  %s has %s that is invalid '%(
 
108
                    unicode(vals['firstname']) +' '+ unicode(vals['lastname']),
 
109
                    add.partner_id.name,
 
110
                    key
 
111
                    )
 
112
                logger.notifyChannel('ldap export', netsvc.LOG_INFO, msg)
 
113
                error_report.append(msg)
 
114
        # Validating the CN
 
115
        if not add.lastname and add.firstname:
 
116
            msg = u'!!! Addresse %s for partner  %s has no last name and first name that is valid partner name was used'%(
 
117
                unicode(add.id),
 
118
                add.partner_id.name,
 
119
                )
 
120
            logger.notifyChannel('ldap export', netsvc.LOG_INFO, msg)
 
121
            error_report.append(msg)
 
122
        # We save to LDAP
 
123
        add.write(vals, {'init_mode':True})
 
124
    #we by pass the encoding errors
 
125
    map(lambda x: unicodedata.normalize("NFKD",x).encode('ascii','ignore'), error_report)
 
126
    error_report = "\n".join(error_report)
 
127
    logger.notifyChannel("MY TOPIC", netsvc.LOG_ERROR, error_report)
 
128
    try:
 
129
        data= base64.encodestring(error_report.encode())
 
130
    except Exception, e:
 
131
        data= base64.encodestring("Could not generate report file. Please look in the log for details")
 
132
 
 
133
    return {'errors': data}
 
134
 
 
135
class Wiz_import_addresses(wizard.interface):
 
136
    states = {
 
137
        'init': {
 
138
            'actions': [],
 
139
            'result': {
 
140
                        'type': 'form',
 
141
                        'arch':_FORM,
 
142
                        'fields':{},
 
143
                        'state':[
 
144
                                    ('end','Cancel'),
 
145
                                    ('importadd','Export adresses into company LDAP')
 
146
                                ]
 
147
                        }
 
148
        },
 
149
        'importadd': {
 
150
            'actions': [_action_import_adresses],
 
151
            'result': {
 
152
                        'state':[('end', 'OK', 'gtk-ok', True)],
 
153
                        'arch' : _FORM1,
 
154
                        'fields' : _FIELDS,
 
155
                        'type':'form'
 
156
                    }
 
157
        }
 
158
    }
 
159
Wiz_import_addresses('ldap.import_adresses')