Skip to content

Commit

Permalink
[FIX] helpdesk_mgmt_activity: Activity wizard is removed
Browse files Browse the repository at this point in the history
At the click 'perform action' button not open activity wizard, and create activity automatically to source record.
  • Loading branch information
geomer198 committed Oct 21, 2024
1 parent a605955 commit fb2bb1d
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 80 deletions.
36 changes: 21 additions & 15 deletions helpdesk_mgmt_activity/models/helpdesk_ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,28 @@ def _check_activity_values(self):
raise models.UserError(_("Date Deadline is not set!"))

def perform_action(self):
self._check_activity_values()
"""Perform action for ticket"""
self.ensure_one()
# Check values for create activity
self._check_activity_values()
try:
# Create activity for source record
self.record_ref.activity_schedule(
summary=self.name,
note=self.description,
date_deadline=self.date_deadline,
activity_type_id=self.source_activity_type_id.id,
ticket_id=self.id,
user_id=self.user_id.id,
)
self.set_next_stage()
except Exception as e:
raise models.UserError from e
return {
"type": "ir.actions.act_window",
"name": "Helpdesk Ticket Action",
"view_mode": "form",
"res_model": "mail.activity",
"view_type": "form",
"context": {
"default_res_model_id": self.env["ir.model"]._get_id(self.res_model),
"default_res_id": self.res_id,
"default_activity_type_id": self.source_activity_type_id.id,
"default_date_deadline": self.date_deadline,
"default_ticket_id": self.id,
"default_summary": self.name,
"default_user_id": self.user_id.id,
"type": "ir.actions.client",
"tag": "display_notification",
"params": {
"type": "success",
"message": _("Activity has been created!"),
},
"target": "new",
}
19 changes: 1 addition & 18 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 api, fields, models
from odoo import fields, models


class MailActivity(models.Model):
Expand All @@ -17,20 +17,3 @@ def _action_done(self, feedback=False, attachment_ids=None):
# 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
123 changes: 76 additions & 47 deletions helpdesk_mgmt_activity/tests/test_helpdesk_ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,8 @@ def create_ticket_and_activity(self):
"date_deadline": Date.today(),
}
)
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,
)
ticket.perform_action()
activity = ticket.record_ref.activity_ids
return ticket, activity

def test_ticket_next_stage(self):
Expand All @@ -57,8 +51,10 @@ def test_ticket_next_stage(self):
# 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)
self.assertEqual(ticket.stage_id, self.new_stage, "Stage must be new")
self.assertEqual(
ticket.next_stage_id, self.progress_stage, "Next stage must be progress"
)

# Set activity configuration for ticket
ticket.write(
Expand All @@ -70,21 +66,19 @@ def test_ticket_next_stage(self):
)

# 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,
)
ticket.perform_action()
activity = ticket.record_ref.activity_ids

self.assertEqual(ticket.stage_id, self.progress_stage)
self.assertEqual(
ticket.stage_id, self.progress_stage, "Ticket stage must be progress"
)

# Activity set done
activity.action_done()

self.assertEqual(ticket.stage_id, self.stage_closed)
self.assertEqual(
ticket.stage_id, self.stage_closed, "Ticket stage must be closed"
)

def test_ticket_available_model_ids(self):
"""Test flow when available model for ticket is updated"""
Expand All @@ -99,50 +93,78 @@ def test_ticket_available_model_ids(self):
"helpdesk_mgmt_activity.helpdesk_available_model_ids", False
)
values = settings.get_values()
self.assertFalse(values.get("helpdesk_available_model_ids"))
self.assertFalse(
values.get("helpdesk_available_model_ids"), "Available models must be False"
)

def test_ticket_record_ref(self):
"""Test flow when change source record"""
ticket = self._create_ticket(self.team_a, self.user)
self.assertFalse(ticket.record_ref)
self.assertFalse(ticket.res_model)
self.assertFalse(ticket.res_id)
self.assertFalse(ticket.record_ref, "Reference record must be False")
self.assertFalse(ticket.res_model, "Res Model must be False")
self.assertFalse(ticket.res_id, "Res ID must be False")

ticket.record_ref = f"res.partner,{self.test_partner.id}"
self.assertEqual(ticket.record_ref, self.test_partner)
self.assertEqual(ticket.res_id, self.test_partner.id)
self.assertEqual(ticket.res_model, "res.partner")
self.assertEqual(
ticket.record_ref,
self.test_partner,
f"Reference record must be equal to {self.test_partner}",
)
self.assertEqual(
ticket.res_id,
self.test_partner.id,
f"Res ID must be equal to {self.test_partner.id}",
)
self.assertEqual(
ticket.res_model, "res.partner", "Res Model must be equal to 'res.partner'"
)

ticket.record_ref = False
self.assertFalse(ticket.res_id)
self.assertFalse(ticket.res_model)
self.assertFalse(ticket.res_id, "Res ID must be False")
self.assertFalse(ticket.res_model, "Res Model must be False")

def test_perform_action(self):
"""Test flow when create action in record reference"""
ticket = self._create_ticket(self.team_a, self.user)

with self.assertRaises(UserError) as error:
ticket.perform_action()
self.assertEqual(error.exception.args[0], "You cannot create activity!")
self.assertEqual(
error.exception.args[0],
"You cannot create activity!",
"Errors must be the same",
)

ticket.team_id.allow_set_activity = True

with self.assertRaises(UserError) as error:
ticket.perform_action()
self.assertEqual(error.exception.args[0], "Source Record is not set!")
self.assertEqual(
error.exception.args[0],
"Source Record is not set!",
"Errors must be the same",
)

ticket.record_ref = f"res.partner,{self.test_partner.id}"

with self.assertRaises(UserError) as error:
ticket.perform_action()
self.assertEqual(error.exception.args[0], "Activity Type is not set!")
self.assertEqual(
error.exception.args[0],
"Activity Type is not set!",
"Errors must be the same",
)

ticket.source_activity_type_id = self.activity_type_meeting
ticket.date_deadline = False

with self.assertRaises(UserError) as error:
ticket.perform_action()
self.assertEqual(error.exception.args[0], "Date Deadline is not set!")
self.assertEqual(
error.exception.args[0],
"Date Deadline is not set!",
"Errors must be the same",
)

ticket.date_deadline = Date.today()

Expand All @@ -151,24 +173,31 @@ def test_perform_action(self):
self.assertDictEqual(
action,
{
"type": "ir.actions.act_window",
"name": "Helpdesk Ticket Action",
"view_mode": "form",
"res_model": "mail.activity",
"view_type": "form",
"context": {
"default_res_model_id": self.partner_model.id,
"default_res_id": ticket.res_id,
"default_activity_type_id": ticket.source_activity_type_id.id,
"default_date_deadline": ticket.date_deadline,
"default_ticket_id": ticket.id,
"default_summary": ticket.name,
"default_user_id": ticket.user_id.id,
"type": "ir.actions.client",
"tag": "display_notification",
"params": {
"type": "success",
"message": "Activity has been created!",
},
"target": "new",
},
)

activity = self.test_partner.activity_ids
self.assertEqual(len(activity), 1, "Activity count must be equal to 1")
self.assertRecordValues(
activity,
[
{
"summary": ticket.name,
"note": ticket.description,
"date_deadline": ticket.date_deadline,
"activity_type_id": ticket.source_activity_type_id.id,
"ticket_id": ticket.id,
"user_id": ticket.user_id.id,
}
],
)

def test_helpdesk_activity_with_team_stage(self):
"""
Test flow when create activity from helpdesk ticket
Expand Down

0 comments on commit fb2bb1d

Please sign in to comment.