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.

132 lines
7.4 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  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. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as published
  9. # by the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp.osv import fields, osv, orm
  22. from openerp.tools.translate import _
  23. import logging
  24. from mako.template import Template
  25. from datetime import datetime
  26. from openerp import tools
  27. from openerp.tools import safe_eval as eval
  28. def _models_get(self, cr, uid, context=None):
  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(orm.Model):
  34. _logger = logging.getLogger('super.calendar')
  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, [], context=context)
  45. super_calendar_pool.unlink(cr, uid, super_calendar_ids, context=context)
  46. for configurator in self.browse(cr, uid, configurator_ids, context):
  47. for line in configurator.line_ids:
  48. current_pool = self.pool.get(line.name.model)
  49. current_record_ids = current_pool.search(cr, uid, line.domain and eval(line.domain) or [], context=context)
  50. for current_record_id in current_record_ids:
  51. current_record = current_pool.browse(cr, uid, current_record_id, context=context)
  52. if line.user_field_id and \
  53. current_record[line.user_field_id.name] and current_record[line.user_field_id.name]._table_name != 'res.users':
  54. raise osv.except_osv(_('Error'),
  55. _("The 'User' field of record %s (%s) does not refer to res.users")
  56. % (current_record[line.description_field_id.name], line.name.model))
  57. if (((line.description_field_id
  58. and current_record[line.description_field_id.name])
  59. or line.description_code)
  60. and current_record[line.date_start_field_id.name]):
  61. duration = False
  62. 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]:
  63. date_start= datetime.strptime(current_record[line.date_start_field_id.name], tools.DEFAULT_SERVER_DATETIME_FORMAT)
  64. date_stop= datetime.strptime(current_record[line.date_stop_field_id.name], tools.DEFAULT_SERVER_DATETIME_FORMAT)
  65. duration = (date_stop - date_start).total_seconds() / 3600
  66. elif line.duration_field_id:
  67. duration = current_record[line.duration_field_id.name]
  68. if line.description_type != 'code':
  69. name = current_record[line.description_field_id.name]
  70. else:
  71. parse_dict = {'o': current_record}
  72. mytemplate = Template(line.description_code)
  73. name= mytemplate.render(**parse_dict)
  74. super_calendar_values = {
  75. 'name': name,
  76. 'model_description': line.description,
  77. 'date_start': current_record[line.date_start_field_id.name],
  78. 'duration': duration,
  79. '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,
  80. 'configurator_id': configurator.id,
  81. 'res_id': line.name.model+','+str(current_record['id']),
  82. 'model_id': line.name.id,
  83. }
  84. super_calendar_pool.create(cr, uid, super_calendar_values, context=context)
  85. self._logger.info('Calendar generated')
  86. return True
  87. class super_calendar_configurator_line(orm.Model):
  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. class super_calendar(orm.Model):
  112. _name = 'super.calendar'
  113. _columns = {
  114. 'name': fields.char('Description', size=512, required=True),
  115. 'model_description': fields.char('Model Description', size=128, required=True),
  116. 'date_start':fields.datetime('Start date', required=True),
  117. 'duration':fields.float('Duration'),
  118. 'user_id': fields.many2one('res.users', 'User'),
  119. 'configurator_id': fields.many2one('super.calendar.configurator', 'Configurator'),
  120. 'res_id': fields.reference('Resource', selection=_models_get, size=128),
  121. 'model_id': fields.many2one('ir.model', 'Model'),
  122. }