11# Copyright 2025 Dixmit
22# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
33
4+ import requests
5+
46from odoo import api , fields , models
57
68from odoo .addons .mail .tools .discuss import Store
@@ -16,6 +18,8 @@ class IrAttachment(models.Model):
1618 string = "Content Grid Data" ,
1719 ondelete = "cascade" ,
1820 )
21+ contentgrid_connection_id = fields .Many2one ("contentgrid.connection" )
22+ contentgrid_url = fields .Char ()
1923
2024 def _push_to_contentgrid (self , manual_send = False ):
2125 self .ensure_one ()
@@ -62,3 +66,29 @@ def get_contentgrid_data(self):
6266 return [
6367 record ._get_contentgrid_data () for record in self .contentgrid_ids .sudo ()
6468 ]
69+
70+ @api .depends ("contentgrid_connection_id" , "contentgrid_url" )
71+ def _compute_raw (self ):
72+ for attachment in self .filtered (lambda r : r .contentgrid_connection_id ):
73+ access_token = attachment .contentgrid_connection_id ._get_token ()
74+ headers = {
75+ "Authorization" : f"Bearer { access_token } " ,
76+ }
77+ data_request = requests .get (
78+ f"{ attachment .contentgrid_connection_id .base_url } /{ attachment .contentgrid_url } " ,
79+ headers = headers ,
80+ timeout = attachment .contentgrid_connection_id ._timeout ,
81+ )
82+ try :
83+ data_request .raise_for_status ()
84+ attachment .raw = data_request .content
85+ except requests .HTTPError :
86+ attachment .raw = b""
87+ return super (
88+ IrAttachment , self .filtered (lambda r : not r .contentgrid_connection_id )
89+ )._compute_raw ()
90+
91+ def _set_attachment_data (self , asbytes ):
92+ if self .contentgrid_connection_id :
93+ return
94+ return super ()._set_attachment_data (asbytes )
0 commit comments