Skip to content

Commit

Permalink
[FIX] helpdesk_mgmt_activity: Code review fixing and functional is ad…
Browse files Browse the repository at this point in the history
…ded.

* is_set_activity field renamed to allow_set_activity
* _referenceable_models method is renamed to _selection_record_ref
* compute and inverce methods are updated
* move to next stage at the activity created from helpdesk ticket for source record functional is added
* tests for new functional is added
* views are updated
  • Loading branch information
geomer198 committed Oct 16, 2024
1 parent e7503f6 commit 822f89e
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 29 deletions.
1 change: 1 addition & 0 deletions helpdesk_mgmt_activity/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"data": [
"views/res_config_settings_views.xml",
"views/helpdesk_ticket_view.xml",
"views/mail_activity_views.xml",
"views/helpdesk_ticket_team_views.xml",
],
"application": False,
Expand Down
74 changes: 54 additions & 20 deletions helpdesk_mgmt_activity/models/helpdesk_ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,27 @@
class HelpdeskTicket(models.Model):
_inherit = "helpdesk.ticket"

can_create_activity = fields.Boolean(related="team_id.is_set_activity")
can_create_activity = fields.Boolean(related="team_id.allow_set_activity")
res_model = fields.Char(string="Source Document Model", index=True)
res_id = fields.Integer(string="Source Document", index=True)

record_ref = fields.Reference(
selection="_referenceable_models",
selection="_selection_record_ref",
compute="_compute_record_ref",
inverse="_inverse_record_ref",
string="Source Record",
)
source_activity_type_id = fields.Many2one(comodel_name="mail.activity.type")
date_deadline = fields.Date(string="Due Date", default=fields.Date.context_today)
next_stage_id = fields.Many2one(
comodel_name="helpdesk.ticket.stage",
compute="_compute_next_stage_id",
store=True,
index=True,
)

@api.model
def _referenceable_models(self):
def _selection_record_ref(self):
"""Select target model for source document"""
model_ids_str = (
self.env["ir.config_parameter"]
Expand All @@ -43,31 +49,60 @@ def _referenceable_models(self):
if IrModelAccess.check(model.get("model"), "read", False)
]

@api.model
def _get_team_stages(self, teams):
"""
Get grouping stages by team id
:param teams: helpdesk.ticket.team record set
:return: dict {team_id: team stages recordset}
"""
return {team.id: team._get_applicable_stages() for team in teams}

@api.depends("stage_id")
def _compute_next_stage_id(self):
"""Compute next stage for ticket"""
team_stages = self._get_team_stages(self.team_id)
for record in self:
current_stage = record.stage_id
stages = team_stages.get(record.team_id.id)
next_stage = (
stages.filtered(
lambda stage, _cur_stage=current_stage: stage.sequence
> current_stage.sequence
)[:1]
or current_stage
)
record.next_stage_id = next_stage

@api.depends("res_model", "res_id")
def _compute_record_ref(self):
"""Compute Source Document Reference"""
for rec in self:
if rec.res_model and rec.res_id:
try:
self.env[rec.res_model].browse(rec.res_id).check_access_rule("read")
rec.record_ref = "%s,%s" % (
rec.res_model,
rec.res_id,
)
except Exception:
rec.record_ref = None
else:
if not rec.res_model or not rec.res_id:
rec.record_ref = None
continue
try:
self.env[rec.res_model].browse(rec.res_id).check_access_rule("read")
rec.record_ref = "%s,%s" % (rec.res_model, rec.res_id)
except Exception:
rec.record_ref = None

def _inverse_record_ref(self):
"""Set Source Document Reference"""
for record in self:
if record.record_ref:
res_id = record.record_ref.id
res_model = record.record_ref._name
else:
res_id, res_model = False, False
record.write({"res_id": res_id, "res_model": res_model})
record_ref = record.record_ref
record.write(
{
"res_id": record_ref and record_ref.id or False,
"res_model": record_ref and record_ref._name or False,
}
)

def set_next_stage(self):
"""Set next ticket stage"""
for record in self:
record.stage_id = record.next_stage_id

def _check_activity_values(self):
"""Check activity values for helpdesk ticket"""
Expand All @@ -94,7 +129,6 @@ def perform_action(self):
"default_res_id": self.res_id,
"default_activity_type_id": self.source_activity_type_id.id,
"default_date_deadline": self.date_deadline,
"default_note": self.description,
"default_ticket_id": self.id,
"default_summary": self.name,
"default_user_id": self.user_id.id,
Expand Down
2 changes: 1 addition & 1 deletion helpdesk_mgmt_activity/models/helpdesk_ticket_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class HelpdeskTicketTeam(models.Model):
_inherit = "helpdesk.ticket.team"

is_set_activity = fields.Boolean(
allow_set_activity = fields.Boolean(
string="Set Activities",
help="Available to set activity on source record from ticket",
)
Expand Down
21 changes: 19 additions & 2 deletions helpdesk_mgmt_activity/models/mail_activity.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from odoo import fields, models
from odoo import api, fields, models


class MailActivity(models.Model):
Expand All @@ -12,8 +12,25 @@ class MailActivity(models.Model):

def _action_done(self, feedback=False, attachment_ids=None):
# Get closed stage for ticket
for ticket in self.mapped("ticket_id"):
for ticket in self.ticket_id:
if ticket.team_id and ticket.team_id.activity_stage_id:
# Change ticket stage
ticket.stage_id = ticket.team_id.activity_stage_id.id
return super()._action_done(feedback, attachment_ids)

@api.onchange("activity_type_id")
def _onchange_activity_type_id(self):
result = super()._onchange_activity_type_id()
if self.ticket_id:
# Add ticket description to action note
self.note = (self.note or "") + "<br />{}".format(
self.ticket_id.description
)
return result

@api.model_create_multi
def create(self, vals_list):
records = super().create(vals_list)
# Set next stage for helpdesk ticket's
records.ticket_id.set_next_stage()
return records
54 changes: 50 additions & 4 deletions helpdesk_mgmt_activity/tests/test_helpdesk_ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ def setUpClass(cls):
"helpdesk_mgmt_activity.helpdesk_available_model_ids", cls.partner_model.ids
)

# Stages
cls.progress_stage = cls.env.ref(
"helpdesk_mgmt.helpdesk_ticket_stage_in_progress"
)
cls.awaiting_stage = cls.env.ref("helpdesk_mgmt.helpdesk_ticket_stage_awaiting")

def create_ticket_and_activity(self):
"""Create ticket and activity for record"""
ticket = self._create_ticket(self.team_a, self.user)
Expand All @@ -39,6 +45,47 @@ def create_ticket_and_activity(self):
)
return ticket, activity

def test_ticket_next_stage(self):
"""Test flow check stage for ticket"""
# Set team config
self.team_a.write(
{
"allow_set_activity": True,
"activity_stage_id": self.stage_closed.id,
}
)
# Create ticket
ticket = self._create_ticket(self.team_a, self.user)

self.assertEqual(ticket.stage_id, self.new_stage)
self.assertEqual(ticket.next_stage_id, self.progress_stage)

# Set activity configuration for ticket
ticket.write(
{
"record_ref": f"res.partner,{self.test_partner.id}",
"source_activity_type_id": self.activity_type_meeting.id,
"date_deadline": Date.today(),
}
)

# Create activity for source record
action = ticket.perform_action()
activity = ticket.record_ref.with_context(
**action.get("context", {})
).activity_schedule(
summary=ticket.name,
note=ticket.description,
date_deadline=ticket.date_deadline,
)

self.assertEqual(ticket.stage_id, self.progress_stage)

# Activity set done
activity.action_done()

self.assertEqual(ticket.stage_id, self.stage_closed)

def test_ticket_available_model_ids(self):
"""Test flow when available model for ticket is updated"""
settings = self.env["res.config.settings"].create({})
Expand Down Expand Up @@ -78,7 +125,7 @@ def test_perform_action(self):
ticket.perform_action()
self.assertEqual(error.exception.args[0], "You cannot create activity!")

ticket.team_id.is_set_activity = True
ticket.team_id.allow_set_activity = True

with self.assertRaises(UserError) as error:
ticket.perform_action()
Expand Down Expand Up @@ -114,7 +161,6 @@ def test_perform_action(self):
"default_res_id": ticket.res_id,
"default_activity_type_id": ticket.source_activity_type_id.id,
"default_date_deadline": ticket.date_deadline,
"default_note": ticket.description,
"default_ticket_id": ticket.id,
"default_summary": ticket.name,
"default_user_id": ticket.user_id.id,
Expand All @@ -130,7 +176,7 @@ def test_helpdesk_activity_with_team_stage(self):
"""
self.team_a.write(
{
"is_set_activity": True,
"allow_set_activity": True,
"activity_stage_id": self.stage_closed.id,
}
)
Expand All @@ -154,7 +200,7 @@ def test_helpdesk_activity_without_team_stage(self):
Test flow when create activity from helpdesk ticket
and done it without activity_stage_id field value from ticket team
"""
self.team_a.is_set_activity = True
self.team_a.allow_set_activity = True
ticket, activity = self.create_ticket_and_activity()
ticket_stage_id = ticket.stage_id.id
self.assertEqual(
Expand Down
3 changes: 2 additions & 1 deletion helpdesk_mgmt_activity/views/helpdesk_ticket_team_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
<field name="priority">99</field>
<field name="arch" type="xml">
<group name="right" position="inside">
<field name="is_set_activity" />
<field name="allow_set_activity" />
<field
name="activity_stage_id"
domain="['|', ('team_ids', '=', False), ('team_ids', 'in', [id])]"
attrs="{'invisible': [('allow_set_activity', '=', False)]}"
/>
</group>
</field>
Expand Down
3 changes: 2 additions & 1 deletion helpdesk_mgmt_activity/views/helpdesk_ticket_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
<field name="can_create_activity" invisible="1" />
<button
string="Perform Action"
class="btn btn-primary"
name="perform_action"
type="object"
attrs="{'invisible': [('can_create_activity', '=', False)]}"
/>
</header>
<group name="main" position="inside">
<group string="Assign Activity">
<group string="Activity Config">
<field name="res_id" invisible="1" />
<field name="res_model" invisible="1" />
<field name="record_ref" />
Expand Down
15 changes: 15 additions & 0 deletions helpdesk_mgmt_activity/views/mail_activity_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>

<record id="mail_activity_view_form_popup" model="ir.ui.view">
<field name="name">mail.activity.view.form.popup.helpdesk.inherit</field>
<field name="model">mail.activity</field>
<field name="inherit_id" ref="mail.mail_activity_view_form_popup" />
<field name="arch" type="xml">
<field name="res_model" position="before">
<field name="ticket_id" invisible="1" />
</field>
</field>
</record>

</odoo>

0 comments on commit 822f89e

Please sign in to comment.