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.

63 lines
2.9 KiB

  1. # Copyright 2018 Eficent Business and IT Consulting Services, S.L.
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. from odoo import api, models, fields, modules
  4. class ResUsers(models.Model):
  5. _inherit = "res.users"
  6. activity_team_ids = fields.Many2many(
  7. comodel_name='mail.activity.team',
  8. relation='mail_activity_team_users_rel',
  9. string="Activity Teams",
  10. )
  11. @api.model
  12. def activity_user_count(self, user_id=False):
  13. if not self._context.get('team_activities', False):
  14. return super().activity_user_count()
  15. query = """SELECT m.id, count(*), act.res_model as model,
  16. CASE
  17. WHEN %(today)s::date -
  18. act.date_deadline::date = 0 Then 'today'
  19. WHEN %(today)s::date -
  20. act.date_deadline::date > 0 Then 'overdue'
  21. WHEN %(today)s::date -
  22. act.date_deadline::date < 0 Then 'planned'
  23. END AS states
  24. FROM mail_activity AS act
  25. JOIN ir_model AS m ON act.res_model_id = m.id
  26. WHERE team_id in (
  27. SELECT mail_activity_team_id
  28. FROM mail_activity_team_users_rel
  29. WHERE res_users_id = %(user_id)s
  30. )
  31. GROUP BY m.id, states, act.res_model;
  32. """
  33. user = user_id if user_id else self.env.uid,
  34. self.env.cr.execute(query, {
  35. 'today': fields.Date.context_today(self),
  36. 'user_id': user,
  37. })
  38. activity_data = self.env.cr.dictfetchall()
  39. model_ids = [a['id'] for a in activity_data]
  40. model_names = {n[0]: n[1] for n in
  41. self.env['ir.model'].browse(model_ids).name_get()}
  42. user_activities = {}
  43. for activity in activity_data:
  44. if not user_activities.get(activity['model']):
  45. user_activities[activity['model']] = {
  46. 'name': model_names[activity['id']],
  47. 'model': activity['model'],
  48. 'icon': modules.module.get_module_icon(
  49. self.env[activity['model']]._original_module),
  50. 'total_count': 0, 'today_count': 0, 'overdue_count': 0,
  51. 'planned_count': 0,
  52. }
  53. user_activities[activity['model']][
  54. '%s_count' % activity['states']] += activity['count']
  55. if activity['states'] in ('today', 'overdue'):
  56. user_activities[activity['model']]['total_count'] += activity[
  57. 'count']
  58. return list(user_activities.values())