@@ -50,68 +50,77 @@ def _sync_tower_values(self):
5050 def _sync_tower_variable_values (self ):
5151 """Sync from Tower variable values - get all values"""
5252 created_values = self .env ["product.attribute.value" ]
53+ all_attribute_values = self .env ["product.attribute.value" ].search (
54+ [("attribute_id" , "=" , self .id )]
55+ )
56+ existing_names = set (all_attribute_values .mapped ("name" ))
57+ existing_tower_value_ids = set (
58+ all_attribute_values .mapped ("tower_variable_value_id" ).ids
59+ )
5360
5461 # Build domain for variable values - get all values for this variable
5562 domain = [("variable_id" , "=" , self .tower_variable_id .id )]
5663
5764 variable_values = self .env ["cx.tower.variable.value" ].search (domain )
58-
65+ vals_list = []
5966 for var_value in variable_values :
6067 value_name = var_value .value_char or var_value .variable_reference
6168
62- # Check if already exists by tower_variable_value_id
63- existing_by_tower_id = self .env ["product.attribute.value" ].search (
64- [
65- ("attribute_id" , "=" , self .id ),
66- ("tower_variable_value_id" , "=" , var_value .id ),
67- ]
68- )
69-
70- # Check if already exists by name (to prevent duplicate names)
71- existing_by_name = self .env ["product.attribute.value" ].search (
72- [("attribute_id" , "=" , self .id ), ("name" , "=" , value_name )]
73- )
74-
7569 # Only create if doesn't exist by tower ID and by name
76- if not existing_by_tower_id and not existing_by_name :
77- created_values |= self .env ["product.attribute.value" ].create (
70+ if (
71+ var_value .id not in existing_tower_value_ids
72+ and value_name not in existing_names
73+ ):
74+ vals_list .append (
7875 {
7976 "name" : value_name ,
8077 "attribute_id" : self .id ,
8178 "tower_variable_value_id" : var_value .id ,
8279 "tower_variable_reference" : self .tower_variable_id .reference ,
8380 }
8481 )
82+ # Add to sets to prevent creating duplicates from this sync batch
83+ existing_names .add (value_name )
84+ existing_tower_value_ids .add (var_value .id )
85+
86+ if vals_list :
87+ created_values |= self .env ["product.attribute.value" ].create (vals_list )
8588
8689 return created_values
8790
8891 def _sync_tower_options (self ):
8992 """Sync from Tower variable options - for option-type variables"""
9093 created_values = self .env ["product.attribute.value" ]
94+ all_attribute_values = self .env ["product.attribute.value" ].search (
95+ [("attribute_id" , "=" , self .id )]
96+ )
97+ existing_names = set (all_attribute_values .mapped ("name" ))
98+ existing_tower_option_ids = set (
99+ all_attribute_values .mapped ("tower_option_id" ).ids
100+ )
91101
92102 # Get all options for this Tower variable
93103 tower_options = self .tower_variable_id .option_ids
94-
104+ vals_list = []
95105 for option in tower_options :
96- # Check if already exists by tower_option_id
97- existing_by_tower_id = self .env ["product.attribute.value" ].search (
98- [("attribute_id" , "=" , self .id ), ("tower_option_id" , "=" , option .id )]
99- )
100-
101- # Check if already exists by name (to prevent duplicate names)
102- existing_by_name = self .env ["product.attribute.value" ].search (
103- [("attribute_id" , "=" , self .id ), ("name" , "=" , option .value_char )]
104- )
105-
106106 # Only create if doesn't exist by tower ID and by name
107- if not existing_by_tower_id and not existing_by_name :
108- created_values |= self .env ["product.attribute.value" ].create (
107+ if (
108+ option .id not in existing_tower_option_ids
109+ and option .value_char not in existing_names
110+ ):
111+ vals_list .append (
109112 {
110113 "name" : option .value_char ,
111114 "attribute_id" : self .id ,
112115 "tower_option_id" : option .id ,
113116 "tower_variable_reference" : self .tower_variable_id .reference ,
114117 }
115118 )
119+ # Add to sets to prevent creating duplicates from this sync batch
120+ existing_names .add (option .value_char )
121+ existing_tower_option_ids .add (option .id )
122+
123+ if vals_list :
124+ created_values |= self .env ["product.attribute.value" ].create (vals_list )
116125
117126 return created_values
0 commit comments