Skip to content

Commit

Permalink
[15.0][FIX] mail_activity_done: KeyError user_activites[model.model]
Browse files Browse the repository at this point in the history
A KeyError occurs for a missing model in the user_activities, when
updating the user_activites[model.model] dict.

This fix initializes the activity data.
  • Loading branch information
bobslee committed Aug 21, 2024
1 parent 1355ecf commit 486e57c
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions mail_activity_done/models/res_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
from collections import defaultdict

from odoo import api, fields, models
from odoo import api, fields, models, modules


class ResUsers(models.Model):
Expand All @@ -18,7 +18,7 @@ def systray_get_activities(self):
for item in res:
user_activities[item["model"]] = item
# Redo the method only with the archived records and subtract them.
query = """SELECT array_agg(res_id) as res_ids, m.id, count(*),
query = """SELECT array_agg(act.res_id) as res_ids, act.res_model, m.id, count(*),
CASE
WHEN %(today)s::date - act.date_deadline::date = 0 Then 'today'
WHEN %(today)s::date - act.date_deadline::date > 0 Then 'overdue'
Expand All @@ -28,7 +28,7 @@ def systray_get_activities(self):
JOIN ir_model AS m ON act.res_model_id = m.id
WHERE user_id = %(user_id)s
AND act.active = False
GROUP BY m.id, states;
GROUP BY m.id, act.res_model, states;
"""
self.env.cr.execute(
query,
Expand All @@ -38,10 +38,28 @@ def systray_get_activities(self):
},
)
activity_data = self.env.cr.dictfetchall()
model_ids = [a["id"] for a in activity_data]
model_names = {
n[0]: n[1] for n in self.env["ir.model"].sudo().browse(model_ids).name_get()
}
records_by_state_by_model = defaultdict(
lambda: {"today": set(), "overdue": set(), "planned": set(), "all": set()}
)
for data in activity_data:
if not user_activities.get(data["res_model"]):
module = self.env[data["res_model"]]._original_module
icon = module and modules.module.get_module_icon(module)
user_activities[data['res_model']] = {
"id": data["id"],
"name": model_names[data["id"]],
"model": data["res_model"],
"icon": icon,
"total_count": 0,
"today_count": 0,
"overdue_count": 0,
"planned_count": 0,
"type": "activity"
}
records_by_state_by_model[data["id"]][data["states"]] = set(data["res_ids"])
records_by_state_by_model[data["id"]]["all"] = records_by_state_by_model[
data["id"]
Expand Down

0 comments on commit 486e57c

Please sign in to comment.