Skip to Content
Menu
This question has been flagged
1 Reply
3430 Views

With our team, we would like to implement a feature where the user is warned whenever the record he/she is updating have also been updated by one of his collegue since he/she opened the record.

We dug into the source code because we did not find any official documentation, only some module that did not fit our Odoo version (11).

We found in the file /odoo/odoo/models.py the method def _check_concurrency(self): with the following code:

    @api.multi
    def _check_concurrency(self):
        if not (self._log_access and self._context.get(self.CONCURRENCY_CHECK_FIELD)):
            return
        check_clause = "(id = %s AND %s < COALESCE(write_date, create_date, (now() at time zone 'UTC'))::timestamp)"
        for sub_ids in self._cr.split_for_in_conditions(self.ids):
            nclauses = 0
            params = []
            for id in sub_ids:
                id_ref = "%s,%s" % (self._name, id)
                update_date = self._context[self.CONCURRENCY_CHECK_FIELD].pop(id_ref, None)
                if update_date:
                    nclauses += 1
                    params.extend([id, update_date])
            if not nclauses:
                continue
            query = "SELECT id FROM %s WHERE %s" % (self._table, " OR ".join([check_clause] * nclauses))
            self._cr.execute(query, tuple(params))
            res = self._cr.fetchone()
            if res:
                # mention the first one only to keep the error message readable
                raise ValidationError(_('A document was modified since you last viewed it (%s:%d)') % (self._description, res[0]))

=> This method is called before any "write". It compares :

  • the '__last_update' value of the record at the time of 'write'

  • with a value of '__last_update' get from the context, which therefore should have been set in the 'context' beforehand

** PROBLEM ** We did not find anywhere in the code (python or javascript) the value set in the context => NOTHING HAPPENS ! THe function returns from the beginning.

When we tried to hardcode it in the context, the function check_concurrency seems to work properly.

QUESTION

Does anyone one know where the __last_update is set or SHOULD BE set in the context ? And how ? I would e.g. imagine setting it somehow when clicking on edit button of a record ??? Or at read time ??

Avatar
Discard
Best Answer

There's a concurrency module for v10, but v11 was a rewrite so it won't work.

For what I remember, you had to modify the client JS code to ask for the __last_update on the read and pass it back on the write. Take care to pass back every __last_update if you have sub-records (like sale.order vs sale.order.line).

Collisions occur rarely enough that read vs edit has little impact. Time spent editing is much longer than the time you look at the record before editing it, unless the read hit the client cache.


Avatar
Discard
Related Posts Replies Views Activity
0
Sep 19
2473
2
Jun 18
6455
0
Sep 18
2849
0
Mar 18
3186
1
Dec 17
4414