server-ux/base_tier_validation/models/res_users.py

50 lines
2.1 KiB
Python
Raw Normal View History

2024-05-03 09:21:24 +00:00
# Copyright 2019 ForgeFlow S.L. (https://www.forgeflow.com)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from flectra import api, fields, models, modules
class Users(models.Model):
_inherit = "res.users"
review_ids = fields.Many2many(string="Reviews", comodel_name="tier.review")
@api.model
def review_user_count(self):
user_reviews = {}
domain = [
("status", "=", "pending"),
("can_review", "=", True),
("id", "in", self.env.user.review_ids.ids),
]
review_groups = self.env["tier.review"].read_group(domain, ["model"], ["model"])
for review_group in review_groups:
model = review_group["model"]
2024-12-03 13:20:46 +00:00
Model = self.env[model]
2024-05-03 09:21:24 +00:00
reviews = self.env["tier.review"].search(review_group.get("__domain"))
2024-12-03 13:20:46 +00:00
# Skip Models not having Tier Validation enabled (example: was unistalled)
if reviews and hasattr(Model, "can_review"):
2024-05-03 09:21:24 +00:00
records = (
2024-12-03 13:20:46 +00:00
Model.with_user(self.env.user)
.with_context(active_test=False)
2024-05-03 09:21:24 +00:00
.search([("id", "in", reviews.mapped("res_id"))])
.filtered(lambda x: not x.rejected and x.can_review)
)
# Excludes any cancelled records depending on the structure of the model
if self.env[model]._state_field in self.env[model]._fields:
records = records.filtered(
lambda x: x[x._state_field] != x._cancel_state
)
if records:
record = self.env[model]
user_reviews[model] = {
"id": records[0].id,
"name": record._description,
"model": model,
"active_field": "active" in record._fields,
"icon": modules.module.get_module_icon(record._original_module),
"type": "tier_review",
"pending_count": len(records),
}
return list(user_reviews.values())