~openerp-commiter/openobject-addons/trunk-extra-addons

« back to all changes in this revision

Viewing changes to base_external_referentials/report.py

  • Committer: Guewen Baconnier @ Camptocamp
  • Date: 2012-05-16 12:33:06 UTC
  • Revision ID: guewen.baconnier@camptocamp.com-20120516123306-mxbb2jz4bhsnix7s
[IMP] base_external_referentials: remove superfluous state from external report logs, improved usage of transactions when importing resources

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
    _order = 'date desc'
36
36
 
37
37
    _columns = {
38
 
        'state': fields.selection((('success', 'Success'),
39
 
                                   ('fail', 'Failed')),
40
 
                                   'Status', required=True, readonly=True),
41
38
        'res_model': fields.char('Resource Object', size=64,
42
39
                                 required=True, readonly=True),
43
40
        'res_id': fields.integer('Resource Id', readonly=True),
59
56
        "date": lambda *a: time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
60
57
    }
61
58
 
62
 
    def _prepare_log_vals(self, cr, uid, state, model, action, res_id,
 
59
    def _prepare_log_vals(self, cr, uid, model, action, res_id,
63
60
        external_id, referential_id, data_record, context=None):
64
61
        return {
65
 
            'state': state,
66
62
            'res_model': model,
67
63
            'action': action,
68
64
            'res_id': res_id,
71
67
            'data_record': data_record,
72
68
        }
73
69
 
74
 
    def _prepare_log_info(self, cr, uid, state, origin_defaults, origin_context, context=None):
75
 
        vals = {
76
 
            'state': state,
 
70
    def _prepare_log_info(self, cr, uid, origin_defaults, origin_context, context=None):
 
71
        exc_type, exc_value, exc_traceback = sys.exc_info()
 
72
        return {
77
73
            'date': time.strftime(DEFAULT_SERVER_DATETIME_FORMAT),
78
74
            'origin_defaults': origin_defaults,
79
 
            'origin_context': origin_context
 
75
            'origin_context': origin_context,
 
76
            'exception_type': exc_type,
 
77
            'error_message': exc_value,
 
78
            'traceback': ''.join(traceback.format_exception(
 
79
                exc_type, exc_value, exc_traceback)),
80
80
        }
81
 
        if state == 'fail':
82
 
            exc_type, exc_value, exc_traceback = sys.exc_info()
83
 
            vals.update({
84
 
                'exception_type': exc_type,
85
 
                'error_message': exc_value,
86
 
                'traceback': ''.join(traceback.format_exception(
87
 
                    exc_type, exc_value, exc_traceback)),
88
 
            })
89
 
        return vals
90
81
 
91
 
    def _log_base(self, cr, uid, model, action, referential_id, state=None,
 
82
    def log_failed(self, cr, uid, model, action, referential_id,
92
83
                  res_id=None, external_id=None,
93
84
                  data_record=None, defaults=None, context=None):
94
85
        defaults = defaults or {}
99
90
        # This ensure a backward compatibility, synchro will continue to
100
91
        # work exactly the same way if use_external_log is not in context
101
92
        if not(existing_line_id or context.get('use_external_log', False)):
102
 
            if state == 'fail':
103
 
                raise
104
 
            return False
 
93
            raise
105
94
 
106
95
        log_cr = pooler.get_db(cr.dbname).cursor()
107
96
 
115
104
            if origin_context.get('conn_obj', False):
116
105
                del origin_context['conn_obj']
117
106
            info = self._prepare_log_info(
118
 
                log_cr, uid, state, origin_defaults, origin_context, context=context)
 
107
                log_cr, uid, origin_defaults, origin_context, context=context)
119
108
            if existing_line_id:
120
109
                self.write(
121
110
                    log_cr, uid,
124
113
                    context=context)
125
114
            else:
126
115
                vals = self._prepare_log_vals(
127
 
                    log_cr, uid, state, model, action, res_id, external_id,
 
116
                    log_cr, uid, model, action, res_id, external_id,
128
117
                    referential_id, data_record, context=context)
129
118
                vals.update(info)
130
119
                existing_line_id = self.create(
139
128
            log_cr.close()
140
129
        return existing_line_id
141
130
 
142
 
    def log_failed(self, cr, uid, model, action, referential_id,
143
 
           res_id=None, external_id=None,
144
 
           data_record=None, defaults=None, context=None):
145
 
        return self._log_base(
146
 
            cr, uid, model, action, referential_id, 'fail', res_id=res_id,
147
 
            external_id=external_id,
148
 
            data_record=data_record, defaults=defaults,
149
 
            context=context)
150
 
 
151
131
    def log_success(self, cr, uid, model, action, referential_id,
152
132
            res_id=None, external_id=None, context=None):
153
133
        if res_id is None and external_id is None:
161
141
            domain += ('res_id', '=', res_id),
162
142
        if external_id is not None:
163
143
            domain += ('external_id', '=', external_id),
164
 
        log_ids = self.search(
165
 
            cr, uid, domain, context=context)
166
 
        self.unlink(cr, uid, log_ids, context=context)
 
144
        log_cr = pooler.get_db(cr.dbname).cursor()
 
145
        try:
 
146
            log_ids = self.search(
 
147
                log_cr, uid, domain, context=context)
 
148
            self.unlink(log_cr, uid, log_ids, context=context)
 
149
        except:
 
150
            log_cr.rollback()
 
151
            raise
 
152
        else:
 
153
            log_cr.commit()
 
154
        finally:
 
155
            log_cr.close()
167
156
        return True
168
157
 
169
158
    def retry(self, cr, uid, ids, context=None):