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.

135 lines
7.4 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Copyright (C) 2012 Agile Business Group sagl (<http://www.agilebg.com>)
  5. # Copyright (C) 2012 Domsense srl (<http://www.domsense.com>)
  6. # All Rights Reserved
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as published
  10. # by the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ##############################################################################
  22. from osv import fields, osv
  23. from tools.translate import _
  24. import netsvc
  25. from mako.template import Template
  26. from datetime import datetime
  27. from openerp import tools
  28. def _models_get(self, cr, uid, context={}):
  29. obj = self.pool.get('ir.model')
  30. ids = obj.search(cr, uid, [])
  31. res = obj.read(cr, uid, ids, ['model', 'name'], context)
  32. return [(r['model'], r['name']) for r in res]
  33. class super_calendar_configurator(osv.osv):
  34. _logger = netsvc.Logger()
  35. _name = 'super.calendar.configurator'
  36. _columns = {
  37. 'name': fields.char('Name', size=64, required=True),
  38. 'line_ids': fields.one2many('super.calendar.configurator.line', 'configurator_id', 'Lines'),
  39. }
  40. def generate_calendar_records(self, cr, uid, ids, context=None):
  41. configurator_ids = self.search(cr, uid, [])
  42. super_calendar_pool = self.pool.get('super.calendar')
  43. # removing old records
  44. super_calendar_ids = super_calendar_pool.search(cr, uid, [])
  45. super_calendar_pool.unlink(cr, uid, super_calendar_ids)
  46. for configurator_id in configurator_ids:
  47. configurator = self.browse(cr, uid, configurator_id)
  48. for line in configurator.line_ids:
  49. current_pool = self.pool.get(line.name.model)
  50. current_record_ids = current_pool.search(cr, uid, line.domain and eval(line.domain) or [])
  51. for current_record_id in current_record_ids:
  52. current_record = current_pool.browse(cr, uid, current_record_id)
  53. if line.user_field_id and \
  54. current_record[line.user_field_id.name] and current_record[line.user_field_id.name]._table_name != 'res.users':
  55. raise osv.except_osv(_('Error'),
  56. _("The 'User' field of record %s (%s) does not refer to res.users")
  57. % (current_record[line.description_field_id.name], line.name.model))
  58. if ((line.description_field_id and current_record[line.description_field_id.name]) or line.description_code) \
  59. and current_record[line.date_start_field_id.name]:
  60. duration = False
  61. if not line.duration_field_id and line.date_stop_field_id and current_record[line.date_start_field_id.name] and current_record[line.date_stop_field_id.name]:
  62. date_start= datetime.strptime(current_record[line.date_start_field_id.name], tools.DEFAULT_SERVER_DATETIME_FORMAT)
  63. date_stop= datetime.strptime(current_record[line.date_stop_field_id.name], tools.DEFAULT_SERVER_DATETIME_FORMAT)
  64. duration = (date_stop - date_start).total_seconds() / 3600
  65. elif line.duration_field_id:
  66. duration = current_record[line.duration_field_id.name]
  67. if line.description_type != 'code':
  68. name = current_record[line.description_field_id.name]
  69. else:
  70. parse_dict = {'o': current_record}
  71. mytemplate = Template(line.description_code)
  72. name= mytemplate.render(**parse_dict)
  73. super_calendar_values = {
  74. 'name': name,
  75. 'model_description': line.description,
  76. 'date_start': current_record[line.date_start_field_id.name],
  77. 'duration': duration,
  78. 'user_id': line.user_field_id and current_record[line.user_field_id.name] and current_record[line.user_field_id.name].id or False,
  79. 'configurator_id': configurator.id,
  80. 'res_id': line.name.model+','+str(current_record['id']),
  81. 'model_id': line.name.id,
  82. }
  83. super_calendar_pool.create(cr, uid, super_calendar_values)
  84. self._logger.notifyChannel('super_calendar', netsvc.LOG_INFO, "Calendar generated")
  85. return True
  86. super_calendar_configurator()
  87. class super_calendar_configurator_line(osv.osv):
  88. _name = 'super.calendar.configurator.line'
  89. _columns = {
  90. 'name': fields.many2one('ir.model', 'Model', required=True),
  91. 'description': fields.char('Description', size=128, required=True),
  92. 'domain': fields.char('Domain', size=512),
  93. 'configurator_id': fields.many2one('super.calendar.configurator', 'Configurator'),
  94. 'description_type': fields.selection([
  95. ('field', 'Field'),
  96. ('code', 'Code'),
  97. ], string="Description Type"),
  98. 'description_field_id': fields.many2one('ir.model.fields', 'Description field',
  99. domain="[('model_id', '=', name),('ttype', '=', 'char')]"),
  100. 'description_code': fields.text('Description field', help="Use '${o}' to refer to the involved object. E.g.: '${o.project_id.name}'"),
  101. 'date_start_field_id': fields.many2one('ir.model.fields', 'Start date field',
  102. domain="['&','|',('ttype', '=', 'datetime'),('ttype', '=', 'date'),('model_id', '=', name)]",
  103. required=True),
  104. 'date_stop_field_id': fields.many2one('ir.model.fields', 'End date field',
  105. domain="['&',('ttype', '=', 'datetime'),('model_id', '=', name)]"),
  106. 'duration_field_id': fields.many2one('ir.model.fields', 'Duration field',
  107. domain="['&',('ttype', '=', 'float'),('model_id', '=', name)]"),
  108. 'user_field_id': fields.many2one('ir.model.fields', 'User field',
  109. domain="['&',('ttype', '=', 'many2one'),('model_id', '=', name)]"),
  110. }
  111. super_calendar_configurator_line()
  112. class super_calendar(osv.osv):
  113. _name = 'super.calendar'
  114. _columns = {
  115. 'name': fields.char('Description', size=512, required=True),
  116. 'model_description': fields.char('Model Description', size=128, required=True),
  117. 'date_start':fields.datetime('Start date', required=True),
  118. #'date_stop':fields.datetime('End date'), # to be removed?
  119. 'duration':fields.float('Duration'),
  120. 'user_id': fields.many2one('res.users', 'User'),
  121. 'configurator_id': fields.many2one('super.calendar.configurator', 'Configurator'),
  122. 'res_id': fields.reference('Resource', selection=_models_get, size=128),
  123. 'model_id': fields.many2one('ir.model', 'Model'),
  124. }
  125. super_calendar()