``create`` accessed ``partner.user_ids[0]`` which raised ``IndexError`` when the ticket's partner had no linked user (e.g. a plain contact). Use ``partner.user_ids[:1]`` so a partner without a user yields an empty recordset, which the following ``if not user`` guard handles gracefully, leaving the ticket's team untouched.
56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
from odoo import api, models
|
|
|
|
|
|
class HelpdeskTicket(models.Model):
|
|
_inherit = "helpdesk.ticket"
|
|
|
|
@api.depends("team_id")
|
|
def _compute_user_id(self):
|
|
for ticket in self:
|
|
ticket.user_id = self._define_user_id(
|
|
ticket.team_id,
|
|
ticket.user_id,
|
|
)
|
|
|
|
@api.model_create_multi
|
|
def create(self, vals_list):
|
|
for vals in vals_list:
|
|
if not vals.get("team_id") and vals.get("partner_id"):
|
|
# Find the user who creates the ticket
|
|
partner = self.env["res.partner"].browse(vals.get("partner_id"))
|
|
if not partner:
|
|
continue
|
|
# A partner may have no linked user (e.g. a plain contact)
|
|
user = partner.user_ids[:1]
|
|
if not user:
|
|
continue
|
|
|
|
# Get its default team_id
|
|
team = user.default_helpdesk_ticket_team_id
|
|
if not team:
|
|
continue
|
|
|
|
vals["team_id"] = team.id
|
|
|
|
# Set the linked project
|
|
if team.default_project_id:
|
|
vals["project_id"] = team.default_project_id.id
|
|
|
|
# Set the user_id to which the ticket is assigned
|
|
user_id = self._define_user_id(team, None)
|
|
if user_id:
|
|
vals["user_id"] = user_id.id
|
|
return super().create(vals_list)
|
|
|
|
def _define_user_id(self, team_id=None, ticket_user_id=None):
|
|
if not team_id:
|
|
return ticket_user_id
|
|
|
|
if not ticket_user_id:
|
|
return team_id.user_id
|
|
|
|
if ticket_user_id not in team_id.user_ids:
|
|
return team_id.user_id
|
|
|
|
return ticket_user_id
|