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.

134 lines
7.4 KiB

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. from openerp import netsvc
  24. from mako.template import Template
  25. from datetime import datetime
  26. from openerp import tools
  27. def _models_get(self, cr, uid, context=None):
  28. obj = self.pool.get('ir.model')
  29. ids = obj.search(cr, uid, [])
  30. res = obj.read(cr, uid, ids, ['model', 'name'], context)
  31. return [(r['model'], r['name']) for r in res]
  32. class super_calendar_configurator(orm.Model):
  33. _logger = netsvc.Logger()
  34. _name = 'super.calendar.configurator'
  35. _columns = {
  36. 'name': fields.char('Name', size=64, required=True),
  37. 'line_ids': fields.one2many('super.calendar.configurator.line', 'configurator_id', 'Lines'),
  38. }
  39. def generate_calendar_records(self, cr, uid, ids, context=None):
  40. configurator_ids = self.search(cr, uid, [])
  41. super_calendar_pool = self.pool.get('super.calendar')
  42. # removing old records
  43. super_calendar_ids = super_calendar_pool.search(cr, uid, [])
  44. super_calendar_pool.unlink(cr, uid, super_calendar_ids)
  45. for configurator_id in configurator_ids:
  46. configurator = self.browse(cr, uid, configurator_id)
  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 [])
  50. for current_record_id in current_record_ids:
  51. current_record = current_pool.browse(cr, uid, current_record_id)
  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 and current_record[line.description_field_id.name]) or line.description_code) \
  58. and current_record[line.date_start_field_id.name]:
  59. duration = False
  60. 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]:
  61. date_start= datetime.strptime(current_record[line.date_start_field_id.name], tools.DEFAULT_SERVER_DATETIME_FORMAT)
  62. date_stop= datetime.strptime(current_record[line.date_stop_field_id.name], tools.DEFAULT_SERVER_DATETIME_FORMAT)
  63. duration = (date_stop - date_start).total_seconds() / 3600
  64. elif line.duration_field_id:
  65. duration = current_record[line.duration_field_id.name]
  66. if line.description_type != 'code':
  67. name = current_record[line.description_field_id.name]
  68. else:
  69. parse_dict = {'o': current_record}
  70. mytemplate = Template(line.description_code)
  71. name= mytemplate.render(**parse_dict)
  72. super_calendar_values = {
  73. 'name': name,
  74. 'model_description': line.description,
  75. 'date_start': current_record[line.date_start_field_id.name],
  76. 'duration': duration,
  77. '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,
  78. 'configurator_id': configurator.id,
  79. 'res_id': line.name.model+','+str(current_record['id']),
  80. 'model_id': line.name.id,
  81. }
  82. super_calendar_pool.create(cr, uid, super_calendar_values)
  83. self._logger.notifyChannel('super_calendar', netsvc.LOG_INFO, "Calendar generated")
  84. return True
  85. super_calendar_configurator()
  86. class super_calendar_configurator_line(orm.Model):
  87. _name = 'super.calendar.configurator.line'
  88. _columns = {
  89. 'name': fields.many2one('ir.model', 'Model', required=True),
  90. 'description': fields.char('Description', size=128, required=True),
  91. 'domain': fields.char('Domain', size=512),
  92. 'configurator_id': fields.many2one('super.calendar.configurator', 'Configurator'),
  93. 'description_type': fields.selection([
  94. ('field', 'Field'),
  95. ('code', 'Code'),
  96. ], string="Description Type"),
  97. 'description_field_id': fields.many2one('ir.model.fields', 'Description field',
  98. domain="[('model_id', '=', name),('ttype', '=', 'char')]"),
  99. 'description_code': fields.text('Description field', help="Use '${o}' to refer to the involved object. E.g.: '${o.project_id.name}'"),
  100. 'date_start_field_id': fields.many2one('ir.model.fields', 'Start date field',
  101. domain="['&','|',('ttype', '=', 'datetime'),('ttype', '=', 'date'),('model_id', '=', name)]",
  102. required=True),
  103. 'date_stop_field_id': fields.many2one('ir.model.fields', 'End date field',
  104. domain="['&',('ttype', '=', 'datetime'),('model_id', '=', name)]"),
  105. 'duration_field_id': fields.many2one('ir.model.fields', 'Duration field',
  106. domain="['&',('ttype', '=', 'float'),('model_id', '=', name)]"),
  107. 'user_field_id': fields.many2one('ir.model.fields', 'User field',
  108. domain="['&',('ttype', '=', 'many2one'),('model_id', '=', name)]"),
  109. }
  110. super_calendar_configurator_line()
  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. #'date_stop':fields.datetime('End date'), # to be removed?
  118. 'duration':fields.float('Duration'),
  119. 'user_id': fields.many2one('res.users', 'User'),
  120. 'configurator_id': fields.many2one('super.calendar.configurator', 'Configurator'),
  121. 'res_id': fields.reference('Resource', selection=_models_get, size=128),
  122. 'model_id': fields.many2one('ir.model', 'Model'),
  123. }
  124. super_calendar()