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.

86 lines
3.5 KiB

  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 MailActivityMixin(models.AbstractModel):
  5. """ Mail Activity Mixin is a mixin class to use if you want to add
  6. activities management on a model. It works like the mail.thread mixin. It
  7. defines an activity_ids one2many field toward activities using res_id and
  8. res_model_id.
  9. Various related / computed fields are also added to have a global status of
  10. activities on documents.
  11. Activities come with a new JS widget for the form view. It is integrated in
  12. the Chatter widget although it is a separate widget. It displays activities
  13. linked to the current record and allow to schedule, edit and mark done
  14. activities.
  15. Use widget="mail_activity" on activity_ids field in form view to use it.
  16. There is also a kanban widget defined. It defines a small widget to
  17. integrate in kanban vignettes. It allow to manage activities directly from
  18. the kanban view. Use widget="kanban_activity" on activitiy_ids field in
  19. kanban view to use it."""
  20. _name = 'mail.activity.mixin'
  21. _description = 'Activity Mixin'
  22. activity_ids = fields.One2many(
  23. 'mail.activity', 'res_id', 'Activities',
  24. auto_join=True,
  25. domain=lambda self: [('res_model', '=', self._name)])
  26. activity_state = fields.Selection([
  27. ('overdue', 'Overdue'),
  28. ('today', 'Today'),
  29. ('planned', 'Planned')], string='State',
  30. compute='_compute_activity_state',
  31. help='Status based on activities\n'
  32. 'Overdue: Due date is already passed\n'
  33. 'Today: Activity date is today\nPlanned: Future activities.')
  34. activity_user_id = fields.Many2one(
  35. 'res.users', 'Responsible',
  36. related='activity_ids.user_id',
  37. search='_search_activity_user_id')
  38. activity_type_id = fields.Many2one(
  39. 'mail.activity.type', 'Next Activity Type',
  40. related='activity_ids.activity_type_id',
  41. search='_search_activity_type_id')
  42. activity_date_deadline = fields.Date(
  43. 'Next Activity Deadline', related='activity_ids.date_deadline',
  44. readonly=True, store=True) # store to enable ordering + search
  45. activity_summary = fields.Char(
  46. 'Next Activity Summary', related='activity_ids.summary',
  47. search='_search_activity_summary')
  48. @api.depends('activity_ids.state')
  49. def _compute_activity_state(self):
  50. for record in self:
  51. states = record.activity_ids.mapped('state')
  52. if 'overdue' in states:
  53. record.activity_state = 'overdue'
  54. elif 'today' in states:
  55. record.activity_state = 'today'
  56. elif 'planned' in states:
  57. record.activity_state = 'planned'
  58. @api.model
  59. def _search_activity_user_id(self, operator, operand):
  60. return [('activity_ids.user_id', operator, operand)]
  61. @api.model
  62. def _search_activity_type_id(self, operator, operand):
  63. return [('activity_ids.activity_type_id', operator, operand)]
  64. @api.model
  65. def _search_activity_summary(self, operator, operand):
  66. return [('activity_ids.summary', operator, operand)]
  67. @api.multi
  68. def unlink(self):
  69. """ Override unlink to delete records activities through
  70. (res_model, res_id). """
  71. record_ids = self.ids
  72. result = super(MailActivityMixin, self).unlink()
  73. self.env['mail.activity'].sudo().search(
  74. [('res_model', '=', self._name), ('res_id', 'in', record_ids)]
  75. ).unlink()
  76. return result