~magentoerpconnect-core-editors/magentoerpconnect/trunk-fix-876312

« back to all changes in this revision

Viewing changes to magentoerpconnect/magerp_osv.py

  • Committer: Niels Huylebroeck
  • Date: 2012-05-25 09:01:25 UTC
  • mto: This revision was merged to the branch mainline in revision 634.
  • Revision ID: nh@agaplan.eu-20120525090125-5x30x3ilkfpah0v6
Replaced all netsvc.Logger() calls with the appropriate logging.getLogger() and also adjusted the logging calls themselves.
Some lines that were edited is because of trailing whitespace which my editor automatically removes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
import time
25
25
import datetime
26
26
import xmlrpclib
27
 
import netsvc
 
27
import logging
28
28
import urllib2
29
29
import base64
30
30
import pooler
39
39
    def __init__(self, location, username, password, debug=False):
40
40
        #Append / if not there
41
41
        if not location[-1] == '/':
42
 
            location += '/' 
 
42
            location += '/'
43
43
        self.corelocation = location
44
44
        #Please do not remove the str indeed xmlrpc lib require a string for the location
45
45
        #if an unicode is send it will raise you an error
48
48
        self.password = password
49
49
        self.debug = False
50
50
        self.result = {}
51
 
        self.logger = netsvc.Logger()
52
 
 
53
 
    
 
51
        self.logger = logging.getLogger('Connection(%s)' % self.location)
 
52
 
 
53
 
54
54
    def connect(self):
55
55
        if not self.location[-1] == '/':
56
56
            self.location += '/'
57
57
        if self.debug:
58
 
            self.logger.notifyChannel(_("Magento Connection"), netsvc.LOG_INFO, _("Attempting connection with Settings:%s,%s,%s") % (self.location, self.username, self.password))
 
58
            self.logger.info(_("Attempting connection with Settings:%s,%s,%s"), self.location, self.username, self.password)
59
59
        self.ser = xmlrpclib.ServerProxy(self.location)
60
60
        for sleep_time in [1, 3, 6]:
61
61
            try:
62
62
                self.session = self.ser.login(self.username, self.password)
63
63
                if self.debug:
64
 
                    self.logger.notifyChannel(_("Magento Connection"), netsvc.LOG_INFO, _("Login Successful"))
 
64
                    self.logger.info(_("Login Successful"))
65
65
                return True
66
66
            except IOError, e:
67
 
                self.logger.notifyChannel(_("Magento Connection"), netsvc.LOG_ERROR, _("Error in connecting:%s") % (e))
68
 
                self.logger.notifyChannel(_("Magento Call"), netsvc.LOG_WARNING, _("Webservice Failure, sleeping %s second before next attempt") % (sleep_time))
 
67
                self.logger.error(_("Error in connecting:%s"), e, exc_info=True)
 
68
                self.logger.warn(_("Webservice Failure, sleeping %s second before next attempt"), sleep_time)
69
69
                time.sleep(sleep_time)
70
70
            except Exception,e:
71
 
                self.logger.notifyChannel(_("Magento Connection"), netsvc.LOG_ERROR, _("Error in connecting:%s") % (e))
72
 
                self.logger.notifyChannel(_("Magento Call"), netsvc.LOG_WARNING, _("Webservice Failure, sleeping %s second before next attempt") % (sleep_time))
73
 
                time.sleep(sleep_time)  
74
 
        raise osv.except_osv(_('User Error'), _('Error when try to connect to magento, are your sure that your login is right? Did openerp can access to your magento?'))
75
 
 
76
 
    
77
 
    def call(self, method, *arguments): 
 
71
                self.logger.error(_("Error in connecting:%s"), e, exc_info=True)
 
72
                self.logger.warn(_("Webservice Failure, sleeping %s second before next attempt"), sleep_time)
 
73
                time.sleep(sleep_time)
 
74
        raise osv.except_osv(_('User Error'), _('Error connecting to magento, are your sure that your login is right? Did you configure API user in magento?'))
 
75
 
 
76
 
 
77
    def call(self, method, *arguments):
78
78
        if arguments:
79
79
            arguments = list(arguments)[0]
80
80
        else:
82
82
        for sleep_time in [1, 3, 6]:
83
83
            try:
84
84
                if self.debug:
85
 
                    self.logger.notifyChannel(_("Magento Connection"), netsvc.LOG_INFO, _("Calling Method:%s,Arguments:%s") % (method, arguments))
 
85
                    self.logger.info(_("Calling Method:%s,Arguments:%s"), method, arguments)
86
86
                res = self.ser.call(self.session, method, arguments)
87
87
                if self.debug:
88
88
                    if method=='catalog_product.list':
89
89
                        # the response of the method catalog_product.list can be very very long so it's better to see it only if debug log is activate
90
 
                        self.logger.notifyChannel(_("Magento Connection"), netsvc.LOG_DEBUG, _("Query Returned:%s") % (res))
 
90
                        self.logger.debug(_("Query Returned:%s"), res)
91
91
                    else:
92
 
                        self.logger.notifyChannel(_("Magento Connection"), netsvc.LOG_INFO, _("Query Returned:%s") % (res))
 
92
                        self.logger.info(_("Query Returned:%s"), res)
93
93
                return res
94
94
            except IOError, e:
95
 
                self.logger.notifyChannel(_("Magento Call"), netsvc.LOG_ERROR, _("Method: %s\nArguments:%s\nError:%s") % (method, arguments, e))
96
 
                self.logger.notifyChannel(_("Magento Call"), netsvc.LOG_WARNING, _("Webservice Failure, sleeping %s second before next attempt") % (sleep_time))
 
95
                self.logger.error(_("Method: %s\nArguments:%s\nError:%s"), method, arguments, e, exc_info=True)
 
96
                self.logger.warn(_("Webservice Failure, sleeping %s second before next attempt"), sleep_time)
97
97
                time.sleep(sleep_time)
98
98
        raise
99
99
 
118
118
    _DELETE_METHOD = False
119
119
    _mapping = {}
120
120
    DEBUG = False
121
 
    
 
121
 
122
122
    #TODO deprecated, remove use
123
123
    def mage_to_oe(self, cr, uid, mageid, instance, *arguments):
124
124
        """given a record id in the Magento referential, returns a tuple (id, name) with the id in the OpenERP referential; Magento instance wise"""
143
143
                    read = self.read(cr, uid, oeid, [self._rec_name])
144
144
                    return (read[0]['id'], read[0][self._rec_name])
145
145
        return False
146
 
    
 
146
 
147
147
    #TODO deprecated, remove use
148
148
    def sync_import(self, cr, uid, magento_records, instance, debug=False, defaults=None, *attrs):
149
149
 
178
178
                    'temp_vars':{},
179
179
                    'mage2oe_filters':mage2oe_filters
180
180
                }
181
 
                
 
181
 
182
182
                #now properly mapp known Magento attributes to OpenERP entity columns:
183
183
                for each_valid_key in self._mapping:
184
184
                    if each_valid_key in magento_record.keys():
216
216
                        self.record_save(cr, uid, rec_id, vals, defaults)
217
217
                else:
218
218
                    self.record_save(cr, uid, rec_id, vals, defaults)
219
 
                            
 
219
 
220
220
    def record_save(self, cr, uid, rec_id, vals, defaults):
221
221
        if defaults:
222
222
            for key in defaults.keys():
227
227
        else:
228
228
            #Record is not there, create it
229
229
            self.create(cr, uid, vals,)
230
 
            
 
230
 
231
231
    def cast_string(self, subject):
232
232
        """This function will convert string objects to the data type required. Example "0"/"1" to boolean conversion"""
233
233
        for key in subject.keys():
237
237
                else:
238
238
                    subject[key] = True
239
239
        return subject
240
 
    
 
240
 
241
241
    def mage_import_base(self,cr,uid,conn, external_referential_id, defaults=None, context=None):
242
242
        if context is None:
243
243
            context = {}
259
259
                list_method = self.pool.get('external.mapping').read(cr,uid,mapping_id[0],['external_list_method']).get('external_list_method',False)
260
260
                if list_method:
261
261
                    data = conn.call(list_method, context['ids_or_filter'])
262
 
                    
 
262
 
263
263
                    #it may happen that list method doesn't provide enough information, forcing us to use get_method on each record (case for sale orders)
264
264
                    if context.get('one_by_one', False):
265
265
                        self.mage_import_one_by_one(cr, uid, conn, external_referential_id, mapping_id[0], data, defaults, context)
337
337
                self.sync_import(cr, uid, magento_records, instance, debug, defaults)
338
338
        else:
339
339
            raise osv.except_osv(_('Undefined List method !'), _("list method is undefined for this object!"))
340
 
    
 
340
 
341
341
    #TODO deprecated, remove use
342
342
    def get_all_mage_ids(self, cr, uid, ids, instance=False):
343
343
        search_param = []
350
350
        for each in reads:
351
351
            mageids.append(each[self._MAGE_FIELD])
352
352
        return mageids
353
 
        
 
353