Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][IMP]helpdesk_mgmt: better attachment support #642

Merged
merged 3 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion helpdesk_mgmt/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,18 @@ def submit_ticket(self, **kw):
new_ticket = request.env["helpdesk.ticket"].sudo().create(vals)
new_ticket.message_subscribe(partner_ids=request.env.user.partner_id.ids)
if kw.get("attachment"):
IrAttachment = request.env["ir.attachment"]
attachment_ids = IrAttachment
for c_file in request.httprequest.files.getlist("attachment"):
data = c_file.read()
if c_file.filename:
request.env["ir.attachment"].sudo().create(
attachment_ids += IrAttachment.sudo().create(
{
"name": c_file.filename,
"datas": base64.b64encode(data),
"res_model": "helpdesk.ticket",
"res_id": new_ticket.id,
}
)
attachment_ids.sudo().generate_access_token()
return werkzeug.utils.redirect("/my/ticket/%s" % new_ticket.id)
11 changes: 11 additions & 0 deletions helpdesk_mgmt/controllers/myaccount.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,22 @@ def _ticket_get_page_view_values(self, ticket, access_token, **kwargs):
closed_stages = ticket.team_id._get_applicable_stages().filtered(
lambda s: s.close_from_portal
)
files = (
request.env["ir.attachment"]
.sudo()
.search(
[
("res_model", "=", "helpdesk.ticket"),
("res_id", "=", ticket.id),
]
)
)
values = {
"closed_stages": closed_stages, # used to display close buttons
"page_name": "ticket",
"ticket": ticket,
"user": request.env.user,
"files": files,
}
return self._get_page_view_values(
ticket, access_token, values, "my_tickets_history", False, **kwargs
Expand Down
35 changes: 33 additions & 2 deletions helpdesk_mgmt/tests/test_helpdesk_portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ def _create_ticket(self, partner, ticket_title, **values):
data.update(**values)
return self.env["helpdesk.ticket"].create(data)

def _submit_ticket(self, **values):
def _submit_ticket(self, files=None, **values):
data = {
"category": self.env.ref("helpdesk_mgmt.helpdesk_category_1").id,
"csrf_token": http.Request.csrf_token(self),
"subject": self.new_ticket_title,
"description": "\n".join(self.new_ticket_desc_lines),
}
data.update(**values)
resp = self.url_open("/submitted/ticket", data=data)
resp = self.url_open("/submitted/ticket", data=data, files=files)
self.assertEqual(resp.status_code, 200)


Expand Down Expand Up @@ -204,3 +204,34 @@ def _call_close_ticket(self, ticket, stage):
self.assertTrue(resp.is_redirect) # http://127.0.0.1:8069/my/ticket/<ticket-id>
self.assertTrue(resp.headers["Location"].endswith(f"/my/ticket/{ticket.id}"))
return resp

def test_submit_ticket_with_attachments(self):
self.authenticate("test-basic-user", "test-basic-user")
self._submit_ticket(
files=[
(
"attachment",
("test.txt", b"test", "plain/text"),
),
(
"attachment",
("test.svg", b"<svg></svg>", "image/svg+xml"),
),
]
)
ticket_id = self.get_new_tickets(self.basic_user)
self.assertEqual(len(ticket_id), 1)
# check that both files have been linked to the newly created ticket
attachment_ids = self.env["ir.attachment"].search(
[
("res_model", "=", "helpdesk.ticket"),
("res_id", "=", ticket_id.id),
]
)
self.assertEqual(len(attachment_ids), 2)
# check that both files have kept their names
self.assertIn("test.txt", attachment_ids.mapped("name"))
self.assertIn("test.svg", attachment_ids.mapped("name"))
# check that both files are public (access_token is set)
self.assertTrue(attachment_ids[0].access_token)
self.assertTrue(attachment_ids[1].access_token)
14 changes: 14 additions & 0 deletions helpdesk_mgmt/views/helpdesk_ticket_templates.xml
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,20 @@
</form>
</t>
</div>
<strong>Attachments:</strong>
<br />
<t t-foreach="files" t-as="f">
<t t-if="f.access_token">
<a
t-attf-href="/web/content/#{f.id}?download=true&amp;access_token=#{f.access_token}"
target="_blank"
>
<span class="fa fa-download" />
<span t-esc="f.name" />
</a>
<br />
</t>
</t>
</div>
</div>
<div class="row mt-3" t-if="ticket.user_id or ticket.partner_id">
Expand Down
Loading