@@ -19,7 +19,7 @@ def search_links(self, relation_name, refs=None):
1919 ._search_links_odoo (self , relation_name , refs )
2020 )
2121
22- def _create_or_update_by_xmlid (self , vals , code , namespace = "XXX" , module = "sync " ):
22+ def _create_or_update_by_xmlid (self , vals , code , namespace = "XXX" , module = "__sync " ):
2323 """
2424 Create or update a record by a dynamically generated XML ID.
2525 Warning! The field `noupdate` is ignored, i.e. existing records are always updated.
@@ -66,34 +66,35 @@ def _create_or_update_by_xmlid(self, vals, code, namespace="XXX", module="sync")
6666 "noupdate" : False ,
6767 }
6868 )
69-
7069 return record
7170
72- def _set_sync_property (self , property_name , property_type , property_value ):
73- """
74- Set or create a property for the current record. If the property field
75- does not exist, create it dynamically.
76-
77- Args:
78- property_name (str): Name of the property field to set.
79- property_value (Any): The value to assign to the property.
80- property_type (str): Type of the property field.
81- """
82- Property = self .env ["ir.property" ]
71+ def _sync_field_name (self , property_name , property_type ):
8372 sync_project_id = self .env .context .get ("sync_project_id" )
8473
8574 if not sync_project_id :
8675 raise exceptions .UserError (
8776 _ ("The 'sync_project_id' must be provided in the context." )
8877 )
8978
90- field_name = "x_sync_%s_%s_%s" % (sync_project_id , property_name , property_type )
79+ return "x_sync_%s_%s_%s" % (sync_project_id , property_name , property_type )
80+
81+ def _set_sync_value (self , property_name , property_type , property_value ):
82+ """
83+ Set or create a property for the current record. If the field
84+ does not exist, create it dynamically.
85+
86+ Args:
87+ property_name (str): Name of the property field to set.
88+ property_type (str): Type of the property field.
89+ property_value (Any): The value to assign to the property.
90+ """
91+ self .ensure_one ()
92+ field_name = self ._sync_field_name (property_name , property_type )
9193 field = self .env ["ir.model.fields" ].search (
9294 [
9395 ("name" , "=" , field_name ),
9496 ("model" , "=" , self ._name ),
9597 ("ttype" , "=" , property_type ),
96- ("sync_project_id" , "=" , sync_project_id ),
9798 ],
9899 limit = 1 ,
99100 )
@@ -104,73 +105,23 @@ def _set_sync_property(self, property_name, property_type, property_value):
104105 {
105106 "name" : field_name ,
106107 "ttype" : property_type ,
107- "model_id" : self .env ["ir.model" ]
108- .search ([("model" , "=" , self ._name )], limit = 1 )
109- .id ,
108+ "model_id" : self .env ["ir.model" ]._get_id (self ._name ),
110109 "field_description" : property_name .capitalize ().replace ("_" , " " ),
111- "sync_project_id" : sync_project_id , # Link to the sync project
112110 }
113111 )
112+ self [field_name ] = property_value
114113
115- res_id = f"{ self ._name } ,{ self .id } "
116- prop = Property .search (
117- [
118- ("name" , "=" , property_name ),
119- ("res_id" , "=" , res_id ),
120- ("fields_id" , "=" , field .id ),
121- ],
122- limit = 1 ,
123- )
124-
125- vals = {"type" : property_type , "value" : property_value }
126- if prop :
127- prop .write (vals )
128- else :
129- vals .update (
130- {
131- "name" : property_name ,
132- "fields_id" : field .id ,
133- "res_id" : res_id ,
134- }
135- )
136- Property .create (vals )
137-
138- def _get_sync_property (self , property_name , property_type ):
114+ def _get_sync_value (self , property_name , property_type ):
139115 """
140- Get the value of a property for the current record.
116+ Get the value of a dynamic field for the current record.
141117
142118 Args:
143119 property_name (str): Name of the property field to get.
120+ property_type (str): Type of the property field.
144121 """
145- Property = self .env ["ir.property" ]
146- sync_project_id = self .env .context .get ("sync_project_id" )
147-
148- if not sync_project_id :
149- raise exceptions .UserError (
150- _ ("The 'sync_project_id' must be provided in the context." )
151- )
152-
153- field_name = "x_sync_%s_%s_%s" % (sync_project_id , property_name , property_type )
154- field = self .env ["ir.model.fields" ].search (
155- [
156- ("name" , "=" , field_name ),
157- ("model" , "=" , self ._name ),
158- ("sync_project_id" , "=" , sync_project_id ),
159- ],
160- limit = 1 ,
161- )
162-
163- if not field :
122+ self .ensure_one ()
123+ field_name = self ._sync_field_name (property_name , property_type )
124+ try :
125+ return self [field_name ]
126+ except KeyError :
164127 return None
165-
166- res_id = f"{ self ._name } ,{ self .id } "
167- prop = Property .search (
168- [
169- ("name" , "=" , property_name ),
170- ("res_id" , "=" , res_id ),
171- ("fields_id" , "=" , field .id ),
172- ],
173- limit = 1 ,
174- )
175-
176- return prop .get_by_record () if prop else None
0 commit comments