[FIX] website_sale_digital: unable to download digital resources.

After trying paying for a digital product and trying to download it
from the customer portal a server interal error occurred.
There was a confusion between the expected argument of
`_get_stream_from` wich is a record and the return value of
`search_read` wich is a dictionnary.
After this fix it will no longer be the case.

opw - 3233478
opw-3216900

closes odoo/odoo#116526

X-original-commit: 51f939267f4e101a78ffbc7eb2edc6fc279ed0c3
Signed-off-by: Victor Feyens (vfe) <vfe@odoo.com>
This commit is contained in:
Horacio Tellez
2023-03-24 19:21:32 +01:00
parent 89a76ab5aa
commit c667eab3a9
3 changed files with 25 additions and 9 deletions
@@ -46,14 +46,8 @@ class WebsiteSaleDigital(CustomerPortal):
@route(['/my/download'], type='http', auth='public')
def download_attachment(self, attachment_id):
# Check if this is a valid attachment id
attachment = request.env['ir.attachment'].sudo().search_read(
[('id', '=', int(attachment_id))],
["name", "datas", "mimetype", "res_model", "res_id", "type", "url"]
)
if attachment:
attachment = attachment[0]
else:
attachment = request.env['ir.attachment'].sudo().browse(int(attachment_id)).exists()
if not attachment:
return request.redirect(self.orders_page)
# Check if the user has bought the associated product
@@ -74,4 +68,4 @@ class WebsiteSaleDigital(CustomerPortal):
else:
return request.redirect(self.orders_page)
return self.env['ir.binary']._get_stream_from(attachment).get_response(as_attachment=True)
return request.env['ir.binary']._get_stream_from(attachment).get_response(as_attachment=True)
@@ -0,0 +1,3 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import test_website_sale_digital_resources_download
@@ -0,0 +1,19 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import http
from odoo.tests import HttpCase, tagged
@tagged('post_install', '-at_install')
class TestWebSaleDigitalResourcesDownload(HttpCase):
def setUp(self):
super().setUp()
self.ir_attachment = self.env['ir.attachment'].create({'name': "test attachment"})
def test_resources_can_be_downloaded(self):
self.authenticate('admin', 'admin')
res = self.url_open('/my/download', data={
'attachment_id': self.ir_attachment.id,
'csrf_token': http.Request.csrf_token(self),
})
res.raise_for_status()