res_users.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import json
  4. from odoo import api, fields, models, modules, _
  5. class Users(models.Model):
  6. _name = 'res.users'
  7. _inherit = ['res.users']
  8. @api.model
  9. def systray_get_activities(self):
  10. """ Split mass_mailing and mass_mailing_sms activities in systray by
  11. removing the single mailing.mailing activity represented and
  12. doing a new query to split them by mailing_type.
  13. """
  14. activities = super(Users, self).systray_get_activities()
  15. for activity in activities:
  16. if activity.get('model') == 'mailing.mailing':
  17. activities.remove(activity)
  18. query = """SELECT m.mailing_type, count(*), act.res_model as model, act.res_id,
  19. CASE
  20. WHEN %(today)s::date - act.date_deadline::date = 0 Then 'today'
  21. WHEN %(today)s::date - act.date_deadline::date > 0 Then 'overdue'
  22. WHEN %(today)s::date - act.date_deadline::date < 0 Then 'planned'
  23. END AS states
  24. FROM mail_activity AS act
  25. JOIN mailing_mailing AS m ON act.res_id = m.id
  26. WHERE act.res_model = 'mailing.mailing' AND act.user_id = %(user_id)s
  27. GROUP BY m.mailing_type, states, act.res_model, act.res_id;
  28. """
  29. self.env.cr.execute(query, {
  30. 'today': fields.Date.context_today(self),
  31. 'user_id': self.env.uid,
  32. })
  33. activity_data = self.env.cr.dictfetchall()
  34. user_activities = {}
  35. for act in activity_data:
  36. if not user_activities.get(act['mailing_type']):
  37. if act['mailing_type'] == 'sms':
  38. module = 'mass_mailing_sms'
  39. name = _('SMS Marketing')
  40. else:
  41. module = 'mass_mailing'
  42. name = _('Email Marketing')
  43. icon = module and modules.module.get_module_icon(module)
  44. res_ids = set()
  45. user_activities[act['mailing_type']] = {
  46. 'id': self.env['ir.model']._get('mailing.mailing').id,
  47. 'name': name,
  48. 'model': 'mailing.mailing',
  49. 'type': 'activity',
  50. 'icon': icon,
  51. 'total_count': 0, 'today_count': 0, 'overdue_count': 0, 'planned_count': 0,
  52. 'res_ids': res_ids,
  53. }
  54. user_activities[act['mailing_type']]['res_ids'].add(act['res_id'])
  55. user_activities[act['mailing_type']]['%s_count' % act['states']] += act['count']
  56. if act['states'] in ('today', 'overdue'):
  57. user_activities[act['mailing_type']]['total_count'] += act['count']
  58. for mailing_type in user_activities.keys():
  59. user_activities[mailing_type].update({
  60. 'actions': [{'icon': 'fa-clock-o', 'name': 'Summary',}],
  61. 'domain': json.dumps([['activity_ids.res_id', 'in', list(user_activities[mailing_type]['res_ids'])]])
  62. })
  63. activities.extend(list(user_activities.values()))
  64. break
  65. return activities