From 376735fa1707ae715bf88e50f73711bfc2662a68 Mon Sep 17 00:00:00 2001 From: Catalin Date: Fri, 15 May 2020 16:08:17 +0200 Subject: [PATCH 1/4] [ADD] helpdesk_mgmt: revamped endpoints, the access is now public in order for non-registered users to create tickets. The forms for those endpoints are also revamped as now can be edited via the website editor, which needed the addition of the website module as dependency. Furthermore, filling and submitting a form won't redirect to the helpdesk own list but to a thankyou page --- helpdesk_mgmt/__manifest__.py | 2 +- helpdesk_mgmt/controllers/main.py | 185 +++-- helpdesk_mgmt/data/helpdesk_data.xml | 12 +- helpdesk_mgmt/models/helpdesk_ticket.py | 1 - helpdesk_mgmt/models/helpdesk_ticket_team.py | 2 +- .../tests/test_helpdesk_ticket_team.py | 4 +- .../views/helpdesk_ticket_templates.xml | 657 ++++++++++++------ 7 files changed, 568 insertions(+), 295 deletions(-) diff --git a/helpdesk_mgmt/__manifest__.py b/helpdesk_mgmt/__manifest__.py index 8dcaea5819..a5bb12ffff 100644 --- a/helpdesk_mgmt/__manifest__.py +++ b/helpdesk_mgmt/__manifest__.py @@ -14,7 +14,7 @@ "SDi Soluciones, " "Odoo Community Association (OCA)", "website": "https://github.com/OCA/helpdesk", - "depends": ["mail", "portal"], + "depends": ["mail", "portal", "website"], "data": [ "data/helpdesk_data.xml", "data/ir_cron.xml", diff --git a/helpdesk_mgmt/controllers/main.py b/helpdesk_mgmt/controllers/main.py index bb51b1dfc5..c8108e25ad 100644 --- a/helpdesk_mgmt/controllers/main.py +++ b/helpdesk_mgmt/controllers/main.py @@ -11,57 +11,65 @@ class HelpdeskTicketController(http.Controller): - @http.route("/ticket/close", type="http", auth="user") - def support_ticket_close(self, **kw): - """Close the support ticket""" - values = {} - for field_name, field_value in kw.items(): - if field_name.endswith("_id"): - values[field_name] = int(field_value) - else: - values[field_name] = field_value - ticket = ( - http.request.env["helpdesk.ticket"] + @http.route("/help", type="http", auth="public", website=True) + def render_public_ticket(self): + """ + :return: rendered template + """ + + user_email = None + user_name = None + user_id = None + if not request.env.ref("base.public_user").id == request.env.user.id: + user_email = http.request.env.user.email + user_name = http.request.env.user.name + user_id = http.request.env.user.id + category_ids = ( + http.request.env["helpdesk.ticket.category"] .with_user(SUPERUSER_ID) - .search([("id", "=", values["ticket_id"])]) + .search([("active", "=", True)]) ) - ticket.stage_id = values.get("stage_id") - - return werkzeug.utils.redirect("/my/ticket/" + str(ticket.id)) - @http.route("/new/ticket", type="http", auth="user", website=True) - def create_new_ticket(self, **kw): - categories = http.request.env["helpdesk.ticket.category"].search( - [("active", "=", True)] - ) - email = http.request.env.user.email - name = http.request.env.user.name return http.request.render( "helpdesk_mgmt.portal_create_ticket", - {"categories": categories, "email": email, "name": name}, + { + "email": user_email, + "name": user_name, + "id_user": user_id, + "categories": category_ids, + }, ) - @http.route("/submitted/ticket", type="http", auth="user", website=True, csrf=True) + @http.route("/help/ticket/submit", type="http", auth="public", website=True) def submit_ticket(self, **kw): - vals = { + """ + Main submit endpoint which receives calls from public and private rendering + endpoints + :param kw: values + :return: a redirection to the thankyou endpoint + """ + + values = { "partner_name": kw.get("name"), - "company_id": http.request.env.user.company_id.id, "category_id": kw.get("category"), "partner_email": kw.get("email"), "description": kw.get("description"), "name": kw.get("subject"), "attachment_ids": False, - "channel_id": request.env["helpdesk.ticket.channel"] - .with_user(SUPERUSER_ID) - .search([("name", "=", "Web")]) - .id, - "partner_id": request.env["res.partner"] - .with_user(SUPERUSER_ID) - .search([("name", "=", kw.get("name")), ("email", "=", kw.get("email"))]) - .id, + "company_id": http.request.env.user.company_id.id, + "channel_id": self._search_id_channel(), + "partner_id": self._search_id_partner(kw), } - new_ticket = request.env["helpdesk.ticket"].with_user(SUPERUSER_ID).create(vals) + + id_team = kw.get("id_team") + if id_team and str.isdigit(id_team): + values.update({"team_id": int(id_team)}) + + new_ticket = ( + request.env["helpdesk.ticket"].with_user(SUPERUSER_ID).create(values) + ) new_ticket.message_subscribe(partner_ids=request.env.user.partner_id.ids) + if kw.get("attachment"): for c_file in request.httprequest.files.getlist("attachment"): data = c_file.read() @@ -75,30 +83,63 @@ def submit_ticket(self, **kw): "res_id": new_ticket.id, } ) - return werkzeug.utils.redirect("/my/tickets") + return werkzeug.utils.redirect("/help/thankyou") + @http.route("/help/ticket/close", type="http", auth="user") + def support_ticket_close(self, **kw): + """Close the support ticket""" + values = {} + for field_name, field_value in kw.items(): + if field_name.endswith("_id"): + values[field_name] = int(field_value) + else: + values[field_name] = field_value + ticket = ( + http.request.env["helpdesk.ticket"] + .with_user(SUPERUSER_ID) + .search([("id", "=", values["ticket_id"])]) + ) + ticket.stage_id = values.get("stage_id") -class HelpdeskTeamForm(http.Controller): - @http.route("/helpdesk/", type="http", auth="user", website=True) + return werkzeug.utils.redirect("/my/ticket/%s" % str(ticket.id)) + + @http.route("/help/thankyou", type="http", auth="public", website=True, csrf=True) + def thankyou_ticket(self): + return http.request.render("helpdesk_mgmt.portal_thankyou_ticket") + + @http.route( + "/help/team/", type="http", auth="public", website=True + ) def create_new_team_ticket(self, endpoint): - team_id = self._team_exists(endpoint) - r = False # don't brake the flow ~~ - if team_id: - email = http.request.env.user.email - name = http.request.env.user.name + user_email = None + user_name = None + user_id = None + if not request.env.ref("base.public_user").id == request.env.user.id: + user_email = http.request.env.user.email + user_name = http.request.env.user.name + user_id = http.request.env.user.id + category_ids = ( + http.request.env["helpdesk.ticket.category"] + .with_user(SUPERUSER_ID) + .search([("active", "=", True)]) + ) + id_team = self._search_id_team(endpoint) + r = False # maybe some day someone'll make a cool error template + if id_team: r = http.request.render( - "helpdesk_mgmt.portal_create_team_ticket", - {"email": email, "name": name, "id_team": team_id.id}, + "helpdesk_mgmt.portal_create_ticket", + { + "email": user_email, + "name": user_name, + "id_team": id_team, + "id_user": user_id, + "categories": category_ids, + }, ) - return r @http.route( - "/helpdesk/ticket/team/submit", - type="http", - auth="user", - website=True, - csrf=True, + "/help/ticket/team/submit", type="http", auth="user", website=True, csrf=True, ) def submit_new_team_ticket(self, **kw): values = { @@ -108,12 +149,10 @@ def submit_new_team_ticket(self, **kw): "partner_email": kw.get("email"), "description": kw.get("description"), "name": kw.get("subject"), - "team_id": kw.get("id_team"), + "team_id": int(kw.get("id_team")), "attachment_ids": False, - "partner_id": request.env["res.partner"] - .with_user(SUPERUSER_ID) - .search([("name", "=", kw.get("name")), ("email", "=", kw.get("email"))]) - .id, + "partner_id": self._search_id_partner(kw), + "channel_id": self._search_id_channel(), } new_ticket = ( request.env["helpdesk.ticket"].with_user(SUPERUSER_ID).create(values) @@ -135,7 +174,35 @@ def submit_new_team_ticket(self, **kw): return werkzeug.utils.redirect("/my/tickets") @staticmethod - def _team_exists(endpoint: str): - return request.env["helpdesk.ticket.team"].search( - [("endpoint_webform", "=", endpoint), ("enable_webform", "=", True)] + def _search_id_partner(kw: dict) -> int: + id_partner = kw.get("id_partner") + if id_partner and str.isdigit(id_partner): + id_partner = int(id_partner) + else: + partner_id = ( + request.env["res.partner"] + .with_user(SUPERUSER_ID) + .search([("email", "=", kw.get("email"))]) + ) + id_partner = partner_id[0].id if partner_id else None + return id_partner + + @staticmethod + def _search_id_channel() -> int: + channel_id = ( + request.env["helpdesk.ticket.channel"] + .with_user(SUPERUSER_ID) + .search([("name", "=", "Web")]) + ) + return channel_id[0].id if channel_id else None + + @staticmethod + def _search_id_team(endpoint: str) -> int: + team_id = ( + request.env["helpdesk.ticket.team"] + .with_user(SUPERUSER_ID) + .search( + [("endpoint_webform", "=", endpoint), ("enable_webform", "=", True)] + ) ) + return team_id[0].id if team_id else None diff --git a/helpdesk_mgmt/data/helpdesk_data.xml b/helpdesk_mgmt/data/helpdesk_data.xml index 1c7fcdb2f3..f7c0bb003f 100644 --- a/helpdesk_mgmt/data/helpdesk_data.xml +++ b/helpdesk_mgmt/data/helpdesk_data.xml @@ -15,10 +15,12 @@ ${object.company_id.partner_id.email} ${not object.partner_id and object.partner_email or ''|safe}, + >${not object.partner_id and object.partner_email or ''|safe}, + ${object.company_id.name} Ticket Assignment (Ref ${object.number or 'n/a' }) + >${object.company_id.name} Ticket Assignment (Ref ${object.number or 'n/a' }) + ${object.partner_id.id} ${object.partner_id.lang} @@ -33,7 +35,8 @@ ${object.company_id.partner_id.email} ${not object.partner_id and object.partner_email or ''|safe}, + >${not object.partner_id and object.partner_email or ''|safe}, + The ticket ${object.number} has been closed. ${object.partner_id.id} @@ -100,7 +103,8 @@ ${object.company_id.partner_id.email} ${not object.partner_id and object.partner_email or ''|safe}, + >${not object.partner_id and object.partner_email or ''|safe}, + The ticket ${object.number} stage has changed. ${object.partner_id.id} diff --git a/helpdesk_mgmt/models/helpdesk_ticket.py b/helpdesk_mgmt/models/helpdesk_ticket.py index c05038283e..50124626fb 100644 --- a/helpdesk_mgmt/models/helpdesk_ticket.py +++ b/helpdesk_mgmt/models/helpdesk_ticket.py @@ -30,7 +30,6 @@ def _compute_automatic_user_assignment(self): :return: user_id: for debugging purposes, as computation methods have to assign the value directly to the field """ - if self.team_id.auto_assign_type != "manual" and not self.user_id: if ( self.team_id.auto_assign_type == "fixed" diff --git a/helpdesk_mgmt/models/helpdesk_ticket_team.py b/helpdesk_mgmt/models/helpdesk_ticket_team.py index 7c0012afca..7a07fff5c9 100644 --- a/helpdesk_mgmt/models/helpdesk_ticket_team.py +++ b/helpdesk_mgmt/models/helpdesk_ticket_team.py @@ -25,7 +25,7 @@ def _compute_endpoint_webform(self): if record.env[record._name].search(domain): _endpoint += "-{}".format(record.id) record.endpoint_webform = _endpoint - record.endpoint_full_webform = "helpdesk/{}".format( + record.endpoint_full_webform = "help/team/{}".format( record.endpoint_webform ) diff --git a/helpdesk_mgmt/tests/test_helpdesk_ticket_team.py b/helpdesk_mgmt/tests/test_helpdesk_ticket_team.py index bca0a36b47..815b2ad223 100644 --- a/helpdesk_mgmt/tests/test_helpdesk_ticket_team.py +++ b/helpdesk_mgmt/tests/test_helpdesk_ticket_team.py @@ -77,7 +77,7 @@ def test_getters(self): self.team_id._compute_endpoint_webform() self.assertEqual(self.team_id.endpoint_webform, team_endpoint) self.assertEqual( - self.team_id.endpoint_full_webform, "helpdesk/{}".format(team_endpoint) + self.team_id.endpoint_full_webform, "help/team/{}".format(team_endpoint) ) _team_id = self.env["helpdesk.ticket.team"].create({"name": "Team 1"}) @@ -89,5 +89,5 @@ def test_getters(self): ) self.assertEqual( _team_id.endpoint_full_webform, - "helpdesk/{}-{}".format(team_endpoint, _team_id.id), + "help/team/{}-{}".format(team_endpoint, _team_id.id), ) diff --git a/helpdesk_mgmt/views/helpdesk_ticket_templates.xml b/helpdesk_mgmt/views/helpdesk_ticket_templates.xml index 6f404e6aaa..fab5afff2e 100644 --- a/helpdesk_mgmt/views/helpdesk_ticket_templates.xml +++ b/helpdesk_mgmt/views/helpdesk_ticket_templates.xml @@ -11,10 +11,8 @@ t-if="page_name == 'ticket' or ticket" t-attf-class="breadcrumb-item #{'active ' if not ticket else ''}" > - Tickets + Tickets + Tickets