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.

163 lines
8.0 KiB

11 years ago
12 years ago
11 years ago
12 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
11 years ago
12 years ago
12 years ago
11 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, 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.safe_eval import safe_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(
  50. cr,
  51. uid,
  52. line.domain and safe_eval(line.domain) or [],
  53. context=context)
  54. for current_record_id in current_record_ids:
  55. current_record = current_pool.browse(cr, uid, current_record_id, context=context)
  56. if (line.user_field_id and
  57. current_record[line.user_field_id.name] and
  58. current_record[line.user_field_id.name]._table_name != 'res.users'):
  59. raise orm.except_orm(
  60. _('Error'),
  61. _("The 'User' field of record %s (%s) does not refer to res.users")
  62. % (current_record[line.description_field_id.name], line.name.model))
  63. if (((line.description_field_id and current_record[line.description_field_id.name]) or
  64. line.description_code) and
  65. current_record[line.date_start_field_id.name]):
  66. duration = False
  67. if (not line.duration_field_id and
  68. line.date_stop_field_id and
  69. current_record[line.date_start_field_id.name] and
  70. current_record[line.date_stop_field_id.name]):
  71. date_start = datetime.strptime(
  72. current_record[line.date_start_field_id.name],
  73. tools.DEFAULT_SERVER_DATETIME_FORMAT
  74. )
  75. date_stop = datetime.strptime(
  76. current_record[line.date_stop_field_id.name],
  77. tools.DEFAULT_SERVER_DATETIME_FORMAT
  78. )
  79. duration = (date_stop - date_start).total_seconds() / 3600
  80. elif line.duration_field_id:
  81. duration = current_record[line.duration_field_id.name]
  82. if line.description_type != 'code':
  83. name = current_record[line.description_field_id.name]
  84. else:
  85. parse_dict = {'o': current_record}
  86. mytemplate = Template(line.description_code)
  87. name = mytemplate.render(**parse_dict)
  88. super_calendar_values = {
  89. 'name': name,
  90. 'model_description': line.description,
  91. 'date_start': current_record[line.date_start_field_id.name],
  92. 'duration': duration,
  93. 'user_id': (
  94. line.user_field_id and
  95. current_record[line.user_field_id.name] and
  96. current_record[line.user_field_id.name].id or
  97. False
  98. ),
  99. 'configurator_id': configurator.id,
  100. 'res_id': line.name.model+','+str(current_record['id']),
  101. 'model_id': line.name.id,
  102. }
  103. super_calendar_pool.create(cr, uid, super_calendar_values, context=context)
  104. self._logger.info('Calendar generated')
  105. return True
  106. class super_calendar_configurator_line(orm.Model):
  107. _name = 'super.calendar.configurator.line'
  108. _columns = {
  109. 'name': fields.many2one('ir.model', 'Model', required=True),
  110. 'description': fields.char('Description', size=128, required=True),
  111. 'domain': fields.char('Domain', size=512),
  112. 'configurator_id': fields.many2one('super.calendar.configurator', 'Configurator'),
  113. 'description_type': fields.selection([
  114. ('field', 'Field'),
  115. ('code', 'Code'),
  116. ], string="Description Type"),
  117. 'description_field_id': fields.many2one(
  118. 'ir.model.fields', 'Description field',
  119. domain="[('model_id', '=', name),('ttype', '=', 'char')]"),
  120. 'description_code': fields.text(
  121. 'Description field',
  122. help="Use '${o}' to refer to the involved object. E.g.: '${o.project_id.name}'"
  123. ),
  124. 'date_start_field_id': fields.many2one(
  125. 'ir.model.fields', 'Start date field',
  126. domain="['&','|',('ttype', '=', 'datetime'),('ttype', '=', 'date'),('model_id', '=', name)]",
  127. required=True),
  128. 'date_stop_field_id': fields.many2one(
  129. 'ir.model.fields', 'End date field',
  130. domain="['&',('ttype', '=', 'datetime'),('model_id', '=', name)]"
  131. ),
  132. 'duration_field_id': fields.many2one(
  133. 'ir.model.fields', 'Duration field',
  134. domain="['&',('ttype', '=', 'float'),('model_id', '=', name)]"),
  135. 'user_field_id': fields.many2one(
  136. 'ir.model.fields', 'User field',
  137. domain="['&',('ttype', '=', 'many2one'),('model_id', '=', name)]"),
  138. }
  139. class super_calendar(orm.Model):
  140. _name = 'super.calendar'
  141. _columns = {
  142. 'name': fields.char('Description', size=512, required=True),
  143. 'model_description': fields.char('Model Description', size=128, required=True),
  144. 'date_start': fields.datetime('Start date', required=True),
  145. 'duration': fields.float('Duration'),
  146. 'user_id': fields.many2one('res.users', 'User'),
  147. 'configurator_id': fields.many2one('super.calendar.configurator', 'Configurator'),
  148. 'res_id': fields.reference('Resource', selection=_models_get, size=128),
  149. 'model_id': fields.many2one('ir.model', 'Model'),
  150. }