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.

64 lines
2.3 KiB

10 years ago
10 years ago
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # licence AGPL version 3 or later
  5. # see licence in __openerp__.py or http://www.gnu.org/licenses/agpl-3.0.txt
  6. # Copyright (C) 2014 Akretion (http://www.akretion.com).
  7. # @author David BEAL <david.beal@akretion.com>
  8. #
  9. ##############################################################################
  10. from openerp.osv import orm
  11. from lxml import etree
  12. import os
  13. """ procedural code is executed even if the module is not installed
  14. TRY TO SWITCH to _register_hook() in the model to avoid
  15. execution when not installed in V 8.0 version
  16. """
  17. module_name = os.path.basename(os.path.dirname(os.path.abspath(__file__)))
  18. DUMMY_MODEL = 'module.%s.installed' % module_name.replace('_', '.')
  19. class DummyModel(orm.Model):
  20. """ Allow to check if module is installed or not
  21. in fields_view_get method to avoid code execution if not
  22. Only executed if the module is installed
  23. """
  24. _name = DUMMY_MODEL
  25. fields_view_get_orginal = orm.BaseModel.fields_view_get
  26. def fields_view_get(self, cr, uid, view_id=None, view_type='form',
  27. context=None, toolbar=False, submenu=False):
  28. res = fields_view_get_orginal(
  29. self, cr, uid, view_id=view_id, view_type=view_type, context=context,
  30. toolbar=toolbar, submenu=submenu)
  31. if view_type == 'tree':
  32. compatible_tree = res.get('field_parent', True) is False
  33. # Tree views with res['field_parent'] different from False
  34. # looks like 'Products by Category'.
  35. # We don't modify these views
  36. # to avoid to break them (js error)
  37. if '_transient_max_count' in self.pool[res['model']].__dict__.keys():
  38. # model with '_transient_max_count' key are transient model
  39. # transient models haven't 'id' column in mostly case
  40. compatible_tree = False
  41. if compatible_tree and DUMMY_MODEL in self.pool.models.keys():
  42. doc = etree.XML(res['arch'])
  43. if (doc.xpath("//tree") and
  44. len(doc.xpath("//field[@name='id']"))) == 0:
  45. node = doc.xpath("//tree")[0]
  46. node.append(etree.Element("field", name="id", string="Id"))
  47. res['arch'] = etree.tostring(node)
  48. return res
  49. orm.BaseModel.fields_view_get = fields_view_get