calendar.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models
  4. class CalendarEvent(models.Model):
  5. _inherit = 'calendar.event'
  6. @api.model
  7. def default_get(self, fields):
  8. if self.env.context.get('default_opportunity_id'):
  9. self = self.with_context(
  10. default_res_model_id=self.env.ref('crm.model_crm_lead').id,
  11. default_res_id=self.env.context['default_opportunity_id']
  12. )
  13. defaults = super(CalendarEvent, self).default_get(fields)
  14. # sync res_model / res_id to opportunity id (aka creating meeting from lead chatter)
  15. if 'opportunity_id' not in defaults:
  16. if self._is_crm_lead(defaults, self.env.context):
  17. defaults['opportunity_id'] = defaults.get('res_id', False) or self.env.context.get('default_res_id', False)
  18. return defaults
  19. opportunity_id = fields.Many2one(
  20. 'crm.lead', 'Opportunity', domain="[('type', '=', 'opportunity')]",
  21. index=True, ondelete='set null')
  22. def _compute_is_highlighted(self):
  23. super(CalendarEvent, self)._compute_is_highlighted()
  24. if self.env.context.get('active_model') == 'crm.lead':
  25. opportunity_id = self.env.context.get('active_id')
  26. for event in self:
  27. if event.opportunity_id.id == opportunity_id:
  28. event.is_highlighted = True
  29. @api.model_create_multi
  30. def create(self, vals):
  31. events = super(CalendarEvent, self).create(vals)
  32. for event in events:
  33. if event.opportunity_id and not event.activity_ids:
  34. event.opportunity_id.log_meeting(event.name, event.start, event.duration)
  35. return events
  36. def _is_crm_lead(self, defaults, ctx=None):
  37. """
  38. This method checks if the concerned model is a CRM lead.
  39. The information is not always in the defaults values,
  40. this is why it is necessary to check the context too.
  41. """
  42. res_model = defaults.get('res_model', False) or ctx and ctx.get('default_res_model')
  43. res_model_id = defaults.get('res_model_id', False) or ctx and ctx.get('default_res_model_id')
  44. return res_model and res_model == 'crm.lead' or res_model_id and self.env['ir.model'].sudo().browse(res_model_id).model == 'crm.lead'