product_curve.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # -*- encoding: utf-8 -*-
  2. from openerp import models, fields, api
  3. class ProductCurve(models.Model):
  4. _name = 'product.curve'
  5. name = fields.Char('Curve Name', required=True)
  6. description = fields.Text('Description', translate=True)
  7. # partner_id = fields.Many2one(
  8. # 'res.partner',
  9. # string='Partner',
  10. # help='Select a partner for this brand if it exists',
  11. # ondelete='restrict'
  12. # )
  13. logo = fields.Binary('Logo File')
  14. product_ids = fields.One2many(
  15. 'product.template',
  16. 'product_curve_id',
  17. string='Curve Products',
  18. )
  19. products_count = fields.Integer(
  20. string='Number of products',
  21. compute='_get_products_count',
  22. )
  23. @api.one
  24. @api.depends('product_ids')
  25. def _get_products_count(self):
  26. self.products_count = len(self.product_ids)
  27. class ProductTemplate(models.Model):
  28. _inherit = 'product.template'
  29. product_curve_id = fields.Many2one(
  30. 'product.curve',
  31. string='Curva',
  32. help='Select a curve for this product'
  33. )
  34. @api.multi
  35. def name_get(self):
  36. res = super(ProductTemplate, self).name_get()
  37. res2 = []
  38. for name_tuple in res:
  39. product = self.browse(name_tuple[0])
  40. if not product.product_curve_id:
  41. res2.append(name_tuple)
  42. continue
  43. res2.append((
  44. name_tuple[0],
  45. u'{} ({})'.format(name_tuple[1], product.product_curve_id.name)
  46. ))
  47. return res2
  48. class ProductProduct(models.Model):
  49. _inherit = 'product.product'
  50. @api.multi
  51. def name_get(self):
  52. res = super(ProductProduct, self).name_get()
  53. res2 = []
  54. for name_tuple in res:
  55. product = self.browse(name_tuple[0])
  56. if not product.product_curve_id:
  57. res2.append(name_tuple)
  58. continue
  59. res2.append((
  60. name_tuple[0],
  61. u'{} ({})'.format(name_tuple[1], product.product_curve_id.name)
  62. ))
  63. return res2