From 27536af8853872df27ea7d92eaa11dc4560e3363 Mon Sep 17 00:00:00 2001 From: Carolina Fernandez Date: Mon, 6 Nov 2023 14:22:50 -0300 Subject: [PATCH] [15.0][ADD] heldespk_mgmt: Helpdesk stages per teams TT45710 --- helpdesk_mgmt/controllers/main.py | 5 -- helpdesk_mgmt/controllers/myaccount.py | 17 +++++- helpdesk_mgmt/models/helpdesk_ticket.py | 52 +++++++++++++++---- helpdesk_mgmt/models/helpdesk_ticket_stage.py | 6 +++ helpdesk_mgmt/models/helpdesk_ticket_team.py | 10 ++++ helpdesk_mgmt/readme/CONTRIBUTORS.rst | 1 + helpdesk_mgmt/static/description/index.html | 1 + helpdesk_mgmt/tests/common.py | 1 + helpdesk_mgmt/tests/test_helpdesk_ticket.py | 42 +++++++++++++++ .../views/helpdesk_ticket_stage_views.xml | 3 ++ 10 files changed, 121 insertions(+), 17 deletions(-) diff --git a/helpdesk_mgmt/controllers/main.py b/helpdesk_mgmt/controllers/main.py index 53056efef6..ac4ea74a5d 100644 --- a/helpdesk_mgmt/controllers/main.py +++ b/helpdesk_mgmt/controllers/main.py @@ -84,11 +84,6 @@ def _prepare_submit_ticket_vals(self, **kw): "partner_id": request.env.user.partner_id.id, "partner_name": request.env.user.partner_id.name, "partner_email": request.env.user.partner_id.email, - # Need to set stage_id so that the _track_template() method is called - # and the mail is sent automatically if applicable - "stage_id": request.env["helpdesk.ticket"] - .with_company(company.id) - .default_get(["stage_id"])["stage_id"], } if company.helpdesk_mgmt_portal_select_team and kw.get("team"): team = ( diff --git a/helpdesk_mgmt/controllers/myaccount.py b/helpdesk_mgmt/controllers/myaccount.py index 1eca8c0109..3d067fb44c 100644 --- a/helpdesk_mgmt/controllers/myaccount.py +++ b/helpdesk_mgmt/controllers/myaccount.py @@ -65,7 +65,13 @@ def portal_my_tickets( searchbar_filters = { "all": {"label": _("All"), "domain": []}, } - for stage in request.env["helpdesk.ticket.stage"].search([]): + for stage in request.env["helpdesk.ticket.stage"].search( + [ + "|", + ("team_ids", "=", HelpdeskTicket.team_id.id), + ("team_ids", "=", False), + ] + ): searchbar_filters[str(stage.id)] = { "label": stage.name, "domain": [("stage_id", "=", stage.id)], @@ -183,8 +189,15 @@ def portal_my_ticket(self, ticket_id, access_token=None, **kw): def _ticket_get_page_view_values(self, ticket, access_token, **kwargs): closed_stages = request.env["helpdesk.ticket.stage"].search( - [("close_from_portal", "=", True)] + [ + "&", + "|", + ("team_ids", "=", ticket.team_id.id), + ("team_ids", "=", False), + ("close_from_portal", "=", True), + ] ) + values = { "closed_stages": closed_stages, # used to display close buttons "page_name": "ticket", diff --git a/helpdesk_mgmt/models/helpdesk_ticket.py b/helpdesk_mgmt/models/helpdesk_ticket.py index b8c8a3669d..517c87b6ce 100644 --- a/helpdesk_mgmt/models/helpdesk_ticket.py +++ b/helpdesk_mgmt/models/helpdesk_ticket.py @@ -11,12 +11,38 @@ class HelpdeskTicket(models.Model): _inherit = ["mail.thread.cc", "mail.activity.mixin", "portal.mixin"] def _get_default_stage_id(self): - return self.env["helpdesk.ticket.stage"].search([], limit=1).id + if self.team_id: + return self.team_id._determine_stage()[self.team_id.id].id + else: + return ( + self.env["helpdesk.ticket.stage"] + .search([("team_ids", "=", False)], limit=1) + .id + ) + + @api.depends("team_id") + def _compute_stage_ids(self): + for ticket in self: + if ticket.team_id: + ticket.stage_id = ticket.team_id._determine_stage()[ticket.team_id.id] + else: + ticket.stage_id = ( + self.env["helpdesk.ticket.stage"] + .search([("team_ids", "=", False)], limit=1) + .id + ) @api.model def _read_group_stage_ids(self, stages, domain, order): - stage_ids = self.env["helpdesk.ticket.stage"].search([]) - return stage_ids + search_domain = [("id", "in", stages.ids)] + if self.env.context.get("default_team_id"): + search_domain = [ + "|", + "|", + ("team_ids", "=", False), + ("team_ids", "in", self.env.context["default_team_id"]), + ] + search_domain + return stages.search(search_domain, order=order) number = fields.Char(string="Ticket number", default="/", readonly=True) name = fields.Char(string="Title", required=True) @@ -31,16 +57,22 @@ def _read_group_stage_ids(self, stages, domain, order): user_ids = fields.Many2many( comodel_name="res.users", related="team_id.user_ids", string="Users" ) + stage_id = fields.Many2one( comodel_name="helpdesk.ticket.stage", string="Stage", - group_expand="_read_group_stage_ids", + compute="_compute_stage_ids", default=_get_default_stage_id, - tracking=True, + store=True, + readonly=False, ondelete="restrict", - index=True, + tracking=True, + group_expand="_read_group_stage_ids", copy=False, + index=True, + domain="['|',('team_ids', '=', team_id),('team_ids','=',False)]", ) + partner_id = fields.Many2one(comodel_name="res.partner", string="Contact") partner_name = fields.Char() partner_email = fields.Char(string="Email") @@ -67,10 +99,7 @@ def _read_group_stage_ids(self, stages, domain, order): comodel_name="helpdesk.ticket.category", string="Category", ) - team_id = fields.Many2one( - comodel_name="helpdesk.ticket.team", - string="Team", - ) + team_id = fields.Many2one(comodel_name="helpdesk.ticket.team", string="Team") priority = fields.Selection( selection=[ ("0", "Low"), @@ -130,6 +159,9 @@ def create(self, vals_list): vals["number"] = self._prepare_ticket_number(vals) if vals.get("user_id") and not vals.get("assigned_date"): vals["assigned_date"] = fields.Datetime.now() + if vals.get("team_id"): + team = self.env["helpdesk.ticket.team"].browse(vals["team_id"]) + vals["stage_id"] = team._determine_stage()[team.id].id return super().create(vals_list) def copy(self, default=None): diff --git a/helpdesk_mgmt/models/helpdesk_ticket_stage.py b/helpdesk_mgmt/models/helpdesk_ticket_stage.py index 7d1fc7e469..e220b5b5e6 100644 --- a/helpdesk_mgmt/models/helpdesk_ticket_stage.py +++ b/helpdesk_mgmt/models/helpdesk_ticket_stage.py @@ -35,6 +35,12 @@ class HelpdeskTicketStage(models.Model): string="Company", default=lambda self: self.env.company, ) + team_ids = fields.Many2many( + comodel_name="helpdesk.ticket.team", + relation="team_stage_rel", + string="Helpdesk Teams", + help="Specific team that uses this stage. If it is empty all teams could uses", + ) @api.onchange("closed") def _onchange_closed(self): diff --git a/helpdesk_mgmt/models/helpdesk_ticket_team.py b/helpdesk_mgmt/models/helpdesk_ticket_team.py index 7f141d33eb..892a0db29a 100644 --- a/helpdesk_mgmt/models/helpdesk_ticket_team.py +++ b/helpdesk_mgmt/models/helpdesk_ticket_team.py @@ -67,6 +67,16 @@ class HelpdeskTeam(models.Model): help="Allow to select this team when creating a new ticket in the portal.", ) + def _determine_stage(self): + result = dict.fromkeys(self.ids, self.env["helpdesk.ticket.stage"]) + for team in self: + result[team.id] = self.env["helpdesk.ticket.stage"].search( + ["|", ("team_ids", "=", False), ("team_ids", "in", team.id)], + order="sequence", + limit=1, + ) + return result + @api.depends("ticket_ids", "ticket_ids.stage_id") def _compute_todo_tickets(self): ticket_model = self.env["helpdesk.ticket"] diff --git a/helpdesk_mgmt/readme/CONTRIBUTORS.rst b/helpdesk_mgmt/readme/CONTRIBUTORS.rst index 15e2effd37..5b4696871e 100644 --- a/helpdesk_mgmt/readme/CONTRIBUTORS.rst +++ b/helpdesk_mgmt/readme/CONTRIBUTORS.rst @@ -27,6 +27,7 @@ * Pedro M. Baeza * Víctor Martínez + * Carolina Fernandez * `ID42 Sistemas `_: diff --git a/helpdesk_mgmt/static/description/index.html b/helpdesk_mgmt/static/description/index.html index be455c12f6..319f97afa6 100644 --- a/helpdesk_mgmt/static/description/index.html +++ b/helpdesk_mgmt/static/description/index.html @@ -568,6 +568,7 @@

Contributors

  • Tecnativa:
    • Pedro M. Baeza
    • Víctor Martínez
    • +
    • Carolina Fernandez
  • ID42 Sistemas:
      diff --git a/helpdesk_mgmt/tests/common.py b/helpdesk_mgmt/tests/common.py index f2f58ce4f1..565698179f 100644 --- a/helpdesk_mgmt/tests/common.py +++ b/helpdesk_mgmt/tests/common.py @@ -39,6 +39,7 @@ def setUpClass(cls): cls.team_b = helpdesk_ticket_team.create( {"name": "Team B", "user_ids": [(6, 0, [cls.user_team.id])]} ) + cls.new_stage = cls.env.ref("helpdesk_mgmt.helpdesk_ticket_stage_new") cls.ticket_a_unassigned = cls._create_ticket(cls, cls.team_a) cls.ticket_a_unassigned.priority = "3" cls.ticket_a_user_own = cls._create_ticket(cls, cls.team_a, cls.user_own) diff --git a/helpdesk_mgmt/tests/test_helpdesk_ticket.py b/helpdesk_mgmt/tests/test_helpdesk_ticket.py index 2f1dc8f0c6..d7ed7a74dd 100644 --- a/helpdesk_mgmt/tests/test_helpdesk_ticket.py +++ b/helpdesk_mgmt/tests/test_helpdesk_ticket.py @@ -97,3 +97,45 @@ def test_helpdesk_ticket_message_new(self): self.assertEqual( t.name, title, "The ticket should have the correct (new) title." ) + + def test_ticket_with_team_stage(self): + self.new_stage.team_ids = [(6, 0, [self.team_a.id, self.team_b.id])] + in_progress_stage = self.env.ref( + "helpdesk_mgmt.helpdesk_ticket_stage_in_progress" + ) + in_progress_stage.team_ids = [(6, 0, [self.team_b.id])] + new_ticket = self.env["helpdesk.ticket"].create( + { + "name": "New Ticket", + "description": "Description", + "team_id": self.team_a.id, + "user_id": self.user.id, + "priority": "1", + } + ) + self.assertEqual(new_ticket.stage_id, self.new_stage) + self.new_stage.team_ids = [(6, 0, [self.team_a.id])] + new_ticket.team_id = self.team_b + self.assertEqual(new_ticket.stage_id, in_progress_stage) + self.new_stage.team_ids = False + new_ticket.team_id = False + self.assertEqual(new_ticket.stage_id, self.new_stage) + + def test_ticket_without_team_stage(self): + self.new_stage.team_ids = False + self.assertEqual( + self.ticket.stage_id, self.team_a._determine_stage()[self.team_a.id] + ) + self.assertEqual(self.ticket.stage_id, self.new_stage) + + def test_ticket_without_team(self): + new_ticket = self.env["helpdesk.ticket"].create( + { + "name": "New Ticket", + "description": "Description", + "team_id": False, + "user_id": self.user.id, + "priority": "1", + } + ) + self.assertEqual(self.new_stage, new_ticket.stage_id) diff --git a/helpdesk_mgmt/views/helpdesk_ticket_stage_views.xml b/helpdesk_mgmt/views/helpdesk_ticket_stage_views.xml index 2077034bf7..b1117c0181 100644 --- a/helpdesk_mgmt/views/helpdesk_ticket_stage_views.xml +++ b/helpdesk_mgmt/views/helpdesk_ticket_stage_views.xml @@ -12,6 +12,7 @@ /> + + +