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.

54 lines
2.4 KiB

  1. # Copyright 2018 Eficent <http://www.eficent.com>
  2. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
  3. from odoo import api, fields, models, modules
  4. class ResUsers(models.Model):
  5. _inherit = 'res.users'
  6. @api.model
  7. def activity_user_count(self):
  8. # Here we totally override the method. Not very nice, but
  9. # we should perhaps ask Odoo to add a hook here.
  10. query = """SELECT m.id, count(*), act.res_model as model,
  11. CASE
  12. WHEN %(today)s::date -
  13. act.date_deadline::date = 0 Then 'today'
  14. WHEN %(today)s::date -
  15. act.date_deadline::date > 0 Then 'overdue'
  16. WHEN %(today)s::date -
  17. act.date_deadline::date < 0 Then 'planned'
  18. END AS states
  19. FROM mail_activity AS act
  20. JOIN ir_model AS m ON act.res_model_id = m.id
  21. WHERE user_id = %(user_id)s
  22. AND act.done = False
  23. GROUP BY m.id, states, act.res_model;
  24. """
  25. self.env.cr.execute(query, {
  26. 'today': fields.Date.context_today(self),
  27. 'user_id': self.env.uid,
  28. })
  29. activity_data = self.env.cr.dictfetchall()
  30. model_ids = [a['id'] for a in activity_data]
  31. model_names = {n[0]: n[1] for n in self.env['ir.model'].browse(
  32. model_ids).name_get()}
  33. user_activities = {}
  34. for activity in activity_data:
  35. if not user_activities.get(activity['model']):
  36. user_activities[activity['model']] = {
  37. 'name': model_names[activity['id']],
  38. 'model': activity['model'],
  39. 'icon': modules.module.get_module_icon(
  40. self.env[activity['model']]._original_module),
  41. 'total_count': 0, 'today_count': 0,
  42. 'overdue_count': 0, 'planned_count': 0,
  43. }
  44. user_activities[activity['model']][
  45. '%s_count' % activity['states']] += activity['count']
  46. if activity['states'] in ('today', 'overdue'):
  47. user_activities[activity['model']][
  48. 'total_count'] += activity['count']
  49. return list(user_activities.values())