You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.9 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2015-2016 Camptocamp SA
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from openerp import models, fields, api
  5. class ResPartner(models.Model):
  6. _inherit = 'res.partner'
  7. changeset_ids = fields.One2many(comodel_name='res.partner.changeset',
  8. inverse_name='partner_id',
  9. string='Changesets',
  10. readonly=True)
  11. count_pending_changesets = fields.Integer(
  12. string='Changes',
  13. compute='_count_pending_changesets',
  14. search='_search_count_pending_changesets')
  15. @api.one
  16. @api.depends('changeset_ids', 'changeset_ids.state')
  17. def _count_pending_changesets(self):
  18. changesets = self.changeset_ids.filtered(
  19. lambda rev: rev.state == 'draft' and rev.partner_id == self
  20. )
  21. self.count_pending_changesets = len(changesets)
  22. @api.multi
  23. def write(self, values):
  24. if self.env.context.get('__no_changeset'):
  25. return super(ResPartner, self).write(values)
  26. else:
  27. changeset_model = self.env['res.partner.changeset']
  28. for record in self:
  29. local_values = changeset_model.add_changeset(record, values)
  30. super(ResPartner, record).write(local_values)
  31. return True
  32. def _search_count_pending_changesets(self, operator, value):
  33. if operator not in ('=', '!=', '<', '<=', '>', '>=', 'in', 'not in'):
  34. return []
  35. query = ("SELECT p.id "
  36. "FROM res_partner p "
  37. "INNER JOIN res_partner_changeset r ON r.partner_id = p.id "
  38. "WHERE r.state = 'draft' "
  39. "GROUP BY p.id "
  40. "HAVING COUNT(r.id) %s %%s ") % operator
  41. self.env.cr.execute(query, (value,))
  42. ids = [row[0] for row in self.env.cr.fetchall()]
  43. return [('id', 'in', ids)]