product_brand.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # -*- encoding: utf-8 -*-
  2. ###############################################################################
  3. # #
  4. # product_brand for Odoo #
  5. # Copyright (C) 2009 NetAndCo (<http://www.netandco.net>). #
  6. # Copyright (C) 2011 Akretion Benoît Guillot <benoit.guillot@akretion.com> #
  7. # Copyright (C) 2014 prisnet.ch Seraphine Lantible <s.lantible@gmail.com> #
  8. # Copyright (C) 2015 Leonardo Donelli #
  9. # Contributors #
  10. # Mathieu Lemercier, mathieu@netandco.net #
  11. # Franck Bret, franck@netandco.net #
  12. # Seraphine Lantible, s.lantible@gmail.com, http://www.prisnet.ch #
  13. # Leonardo Donelli, donelli@webmonks.it, http://www.wearemonk.com #
  14. # #
  15. # This program is free software: you can redistribute it and/or modify #
  16. # it under the terms of the GNU Affero General Public License as #
  17. # published by the Free Software Foundation, either version 3 of the #
  18. # License, or (at your option) any later version. #
  19. # #
  20. # This program is distributed in the hope that it will be useful, #
  21. # but WITHOUT ANY WARRANTY; without even the implied warranty of #
  22. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  23. # GNU Affero General Public License for more details. #
  24. # #
  25. # You should have received a copy of the GNU Affero General Public License #
  26. # along with this program. If not, see <http://www.gnu.org/licenses/>. #
  27. # #
  28. ###############################################################################
  29. ###############################################################################
  30. # Product Brand is an Openobject module wich enable Brand management for #
  31. # products #
  32. ###############################################################################
  33. from openerp import models, fields, api
  34. class ProductBrand(models.Model):
  35. _name = 'product.brand'
  36. name = fields.Char('Brand Name', required=True)
  37. description = fields.Text('Description', translate=True)
  38. partner_id = fields.Many2one(
  39. 'res.partner',
  40. string='Partner',
  41. help='Select a partner for this brand if it exists',
  42. ondelete='restrict'
  43. )
  44. logo = fields.Binary('Logo File')
  45. product_ids = fields.One2many(
  46. 'product.template',
  47. 'product_brand_id',
  48. string='Brand Products',
  49. )
  50. products_count = fields.Integer(
  51. string='Number of products',
  52. compute='_get_products_count',
  53. )
  54. @api.one
  55. @api.depends('product_ids')
  56. def _get_products_count(self):
  57. self.products_count = len(self.product_ids)
  58. class ProductTemplate(models.Model):
  59. _inherit = 'product.template'
  60. product_brand_id = fields.Many2one(
  61. 'product.brand',
  62. string='Brand',
  63. help='Select a brand for this product'
  64. )
  65. @api.multi
  66. def name_get(self):
  67. res = super(ProductTemplate, self).name_get()
  68. res2 = []
  69. for name_tuple in res:
  70. product = self.browse(name_tuple[0])
  71. if not product.product_brand_id:
  72. res2.append(name_tuple)
  73. continue
  74. res2.append((
  75. name_tuple[0],
  76. u'{} ({})'.format(name_tuple[1], product.product_brand_id.name)
  77. ))
  78. return res2
  79. class ProductProduct(models.Model):
  80. _inherit = 'product.product'
  81. @api.multi
  82. def name_get(self):
  83. res = super(ProductProduct, self).name_get()
  84. res2 = []
  85. for name_tuple in res:
  86. product = self.browse(name_tuple[0])
  87. if not product.product_brand_id:
  88. res2.append(name_tuple)
  89. continue
  90. res2.append((
  91. name_tuple[0],
  92. u'{} ({})'.format(name_tuple[1], product.product_brand_id.name)
  93. ))
  94. return res2