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.

119 lines
5.0 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 Odoo SA <https://www.odoo.com>
  3. # Copyright 2018 Eficent <http://www.eficent.com>
  4. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
  5. from openerp import api, models, fields
  6. class CalendarEvent(models.Model):
  7. _inherit = "calendar.event"
  8. @api.model
  9. def default_get(self, fields):
  10. if self.env.context.get('default_res_model') and not \
  11. self.env.context.get('default_res_model_id'):
  12. self = self.with_context(
  13. default_res_model_id=self.env['ir.model'].sudo().search([
  14. ('model', '=', self.env.context['default_res_model'])
  15. ], limit=1).id
  16. )
  17. defaults = super(CalendarEvent, self).default_get(fields)
  18. # support active_model / active_id as replacement of
  19. # default_* if not already given
  20. if 'res_model_id' not in defaults and 'res_model_id' in fields and \
  21. self.env.context.get('active_model') and \
  22. self.env.context['active_model'] != 'calendar.event':
  23. defaults['res_model_id'] = self.env['ir.model'].sudo().search(
  24. [('model', '=', self.env.context['active_model'])],
  25. limit=1).id
  26. if 'res_id' not in defaults and 'res_id' in fields and \
  27. defaults.get(
  28. 'res_model_id') and self.env.context.get('active_id'):
  29. defaults['res_id'] = self.env.context['active_id']
  30. return defaults
  31. # linked document
  32. res_id = fields.Integer('Document ID')
  33. res_model_id = fields.Many2one('ir.model',
  34. 'Document Model', ondelete='cascade')
  35. res_model = fields.Char('Document Model Name',
  36. related='res_model_id.model', readonly=True,
  37. store=True)
  38. activity_ids = fields.One2many('mail.activity', 'calendar_event_id',
  39. string='Activities')
  40. @api.model
  41. def create(self, values):
  42. # created from calendar: try to create an activity
  43. # on the related record
  44. if not values.get('activity_ids'):
  45. defaults = self.default_get(['activity_ids',
  46. 'res_model_id', 'res_id', 'user_id'])
  47. res_model_id = values.get('res_model_id', defaults.get(
  48. 'res_model_id'))
  49. res_id = values.get('res_id', defaults.get('res_id'))
  50. user_id = values.get('user_id', defaults.get('user_id'))
  51. if not defaults.get('activity_ids') and res_model_id and res_id:
  52. if hasattr(self.env[self.env['ir.model'].sudo().browse(
  53. res_model_id).model], 'activity_ids'):
  54. meeting_activity_type = \
  55. self.env['mail.activity.type'].search(
  56. [('category', '=', 'meeting')], limit=1)
  57. if meeting_activity_type:
  58. activity_vals = {
  59. 'res_model_id': res_model_id,
  60. 'res_id': res_id,
  61. 'activity_type_id': meeting_activity_type.id,
  62. }
  63. if user_id:
  64. activity_vals['user_id'] = user_id
  65. values['activity_ids'] = [(0, 0, activity_vals)]
  66. meeting = super(CalendarEvent, self).create(values)
  67. meeting._sync_activities(values)
  68. return meeting
  69. @api.multi
  70. def write(self, values):
  71. # compute duration, only if start and stop are modified
  72. if 'duration' not in values and 'start' in values and 'stop' in values:
  73. values['duration'] = self._get_duration(values['start'],
  74. values['stop'])
  75. self._sync_activities(values)
  76. return super(CalendarEvent, self).write(values)
  77. @api.multi
  78. def action_close_dialog(self):
  79. return {'type': 'ir.actions.act_window_close'}
  80. @api.multi
  81. def action_done(self):
  82. for rec in self:
  83. rec.activity_ids.action_feedback()
  84. return {'type': 'ir.actions.act_window_close'}
  85. @api.multi
  86. def action_open_calendar_event(self):
  87. if self.res_model and self.res_id:
  88. return self.env[self.res_model].browse(
  89. self.res_id).get_formview_action()[0]
  90. return False
  91. def _sync_activities(self, values):
  92. # update activities
  93. if self.mapped('activity_ids'):
  94. activity_values = {}
  95. if values.get('name'):
  96. activity_values['summary'] = values['name']
  97. if values.get('description'):
  98. activity_values['note'] = values['description']
  99. if values.get('start'):
  100. activity_values['date_deadline'] = \
  101. fields.Datetime.from_string(values['start']).date()
  102. if values.get('user_id'):
  103. activity_values['user_id'] = values['user_id']
  104. if activity_values.keys():
  105. self.mapped('activity_ids').write(activity_values)