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.

95 lines
3.4 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Copyright (c) 2010-2013 Camptocamp SA (http://www.camptocamp.com)
  5. # All Right Reserved
  6. #
  7. # Author : Nicolas Bessi (Camptocamp), Joel Grand-Guillaume
  8. #
  9. # This program is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU Affero General Public License as
  11. # published by the Free Software Foundation, either version 3 of the
  12. # License, or (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU Affero General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU Affero General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. #
  22. #
  23. ##############################################################################
  24. from openerp.osv import osv, fields, orm
  25. class ResPartnerAdressCategory(orm.Model):
  26. def name_get(self, cr, uid, ids, context=None):
  27. if not len(ids):
  28. return []
  29. reads = self.read(cr, uid, ids, ['name', 'parent_id'], context)
  30. res = []
  31. for record in reads:
  32. name = record['name']
  33. if record['parent_id']:
  34. name = '%s / %s ' % (record['parent_id'][1], name)
  35. res.append((record['id'], name))
  36. return res
  37. def _name_get_fnc(self, cr, uid, ids, prop, unknow_none, unknow_dict):
  38. res = self.name_get(cr, uid, ids)
  39. return dict(res)
  40. def _check_recursion(self, cr, uid, ids):
  41. level = 100
  42. while ids:
  43. cr.execute('select distinct parent_id '
  44. 'from res_partner_address_category '
  45. 'where id in %s', ids)
  46. ids = [parent_id for (parent_id,) in cr.fetchall() if parent_id]
  47. if not level:
  48. return False
  49. level -= 1
  50. return True
  51. _description='Partner address Categories'
  52. _name = 'res.partner.address.category'
  53. _columns = {
  54. 'name': fields.char('Category Name', required=True, size=64),
  55. 'parent_id': fields.many2one('res.partner.address.category',
  56. 'Parent Category',
  57. select=True),
  58. 'complete_name': fields.function(_name_get_fnc,
  59. type="char",
  60. string='Name'),
  61. 'child_ids': fields.one2many('res.partner.address.category',
  62. 'parent_id',
  63. 'Children Category'),
  64. 'active' : fields.boolean('Active'),
  65. }
  66. _constraints = [
  67. (_check_recursion,
  68. 'Error: you can not create recursive categories.',
  69. ['parent_id'])
  70. ]
  71. _defaults = {
  72. 'active' : lambda *a: 1,
  73. }
  74. _order = 'parent_id,name'
  75. class ResPartnerAddress(orm.Model):
  76. _inherit = "res.partner.address"
  77. _columns = {
  78. 'category_id': fields.many2many('res.partner.address.category',
  79. 'res_partner_address_category_rel',
  80. 'adress_id',
  81. 'category_id',
  82. 'Address categories'),
  83. }