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.

120 lines
4.6 KiB

10 years ago
10 years ago
10 years ago
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) 2010-2013 OpenERP s.a. (<http://openerp.com>).
  6. # Copyright (C) 2014 initOS GmbH & Co. KG (<http://www.initos.com>).
  7. # Author Markus Schneider <markus.schneider at initos.com>
  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. from openerp.osv import orm, fields
  24. import random
  25. class tile(orm.Model):
  26. _name = 'tile.tile'
  27. def _get_tile_info(self, cr, uid, ids, fields, args, context=None):
  28. ima_obj = self.pool['ir.model.access']
  29. res = {}
  30. records = self.browse(cr, uid, ids, context=context)
  31. for r in records:
  32. if ima_obj.check(
  33. cr, uid, r.model_id.model, 'read', False, context):
  34. model = self.pool.get(r.model_id.model)
  35. res[r.id] = {
  36. 'active': True,
  37. 'count': model.search_count(
  38. cr, uid, eval(r.domain), context),
  39. }
  40. else:
  41. res[r.id] = {'active': False, 'count': 0}
  42. return res
  43. def _search_active(self, cr, uid, obj, name, arg, context=None):
  44. ima_obj = self.pool['ir.model.access']
  45. ids = []
  46. cr.execute("""
  47. SELECT tt.id, im.model
  48. FROM tile_tile tt
  49. INNER JOIN ir_model im
  50. ON tt.model_id = im.id""")
  51. for result in cr.fetchall():
  52. if (ima_obj.check(cr, uid, result[1], 'read', False) ==
  53. arg[0][2]):
  54. ids.append(result[0])
  55. return [('id', 'in', ids)]
  56. _columns = {
  57. 'name': fields.char('Tile Name'),
  58. 'model_id': fields.many2one('ir.model', 'Model'),
  59. 'user_id': fields.many2one('res.users', 'User'),
  60. 'domain': fields.text('Domain'),
  61. 'action_id': fields.many2one('ir.actions.act_window', 'Action'),
  62. 'count': fields.function(
  63. _get_tile_info, type='int', string='Count',
  64. multi='tile_info', readonly=True),
  65. 'active': fields.function(
  66. _get_tile_info, type='boolean', string='Active',
  67. multi='tile_info', readonly=True, fnct_search=_search_active),
  68. 'color': fields.char('Kanban Color'),
  69. }
  70. _defaults = {
  71. 'domain': '[]',
  72. 'color': 0,
  73. }
  74. def open_link(self, cr, uid, ids, context=None):
  75. tile_id = ids[0]
  76. tile_object = self.browse(cr, uid, tile_id, context=context)
  77. if tile_object.action_id:
  78. act_obj = self.pool.get('ir.actions.act_window')
  79. result = act_obj.read(cr, uid, [tile_object.action_id.id],
  80. context=context)[0]
  81. # FIXME: restore original Domain + Filter would be better
  82. result['domain'] = tile_object.domain
  83. return result
  84. # we have no action_id stored,
  85. # so try to load a default tree view
  86. return {
  87. 'name': tile_object.name,
  88. 'view_type': 'form',
  89. 'view_mode': 'tree',
  90. 'view_id': [False],
  91. 'res_model': tile_object.model_id.model,
  92. 'type': 'ir.actions.act_window',
  93. 'context': context,
  94. 'nodestroy': True,
  95. 'target': 'current',
  96. 'domain': tile_object.domain,
  97. }
  98. def add(self, cr, uid, vals, context=None):
  99. # TODO: check if string
  100. if 'model_id' in vals:
  101. # need to replace model_name with its id
  102. model_ids = self.pool.get('ir.model').search(cr, uid,
  103. [('model', '=',
  104. vals['model_id'])])
  105. vals['model_id'] = model_ids[0]
  106. if 'color' not in vals:
  107. vals['color'] = random.randint(1, 10)
  108. return self.create(cr, uid, vals, context)