· 7 years ago · Sep 12, 2018, 03:48 PM
1@api.multi
2 def store_attributes(self, row, header, prod_tmpl_id):
3 create_date = datetime.datetime.now()
4 write_date = create_date
5 create_uid = self._uid
6 write_uid = create_uid
7
8 for head in header:
9 value_of_attribute = row[header.index(head)].strip()
10
11 # if cell in csv is empty, do not store attribute for product
12 if value_of_attribute == '':
13 continue
14
15 # Raw SQL way of query
16 sql_product_attribute = 'SELECT id FROM product_attribute WHERE name=%s'
17 self.env.cr.execute(sql_product_attribute, (head,))
18 self.env.cr.commit()
19 product_attribute_id = self.env.cr.fetchone()
20
21 # OO way of query
22 # product_attribute_id = self.env['product.attribute'].search(
23 # [
24 # (
25 # 'name',
26 # '=',
27 # head
28 # )
29 # ]
30 # )
31
32 # if it does not exists, move to the other head
33 # if not product_attribute_id:
34 if product_attribute_id is None:
35 continue
36 else:
37 # check if the attribute-value pair already exist
38 #########################################################################################
39 # SQL way of query
40 sql_match = 'SELECT id FROM product_attribute_value WHERE name=%s AND attribute_id=%s'
41 self.env.cr.execute(sql_match, (value_of_attribute, product_attribute_id[0]))
42 self.env.cr.commit()
43 match_found_id = self.env.cr.fetchone()
44
45 # OO way of query
46 # match_found_id = self.env['product.attribute.value'].search(
47 # [
48 # (
49 # 'name',
50 # '=',
51 # value_of_attribute
52 # ),
53 # (
54 # 'attribute_id',
55 # '=',
56 # product_attribute_id.id
57 # )
58 # ]
59 # )
60
61 # if not match_found_id:
62 if match_found_id is None:
63 # storing in product_attribute_value table if match attribute-value is not found
64 # SQL way
65 sql_result = 'INSERT INTO product_attribute_value(name, attribute_id, sequence, create_uid, create_date, write_uid, write_date)' \
66 ' VALUES (%s, %s, %s, %s, %s, %s, %s) RETURNING id'
67 self.env.cr.execute(sql_result, (
68 value_of_attribute, product_attribute_id[0], 0, create_uid, create_date, write_uid, write_date))
69 self.env.cr.commit()
70 result = self.env.cr.fetchone()
71 # OO way of storing data
72 # result = self.env['product.attribute.value'].create(
73 # {
74 # 'name': value_of_attribute,
75 # 'attribute_id': product_attribute_id.id,
76 # 'sequence': 0
77 # }
78 # )
79 if result is not None:
80 produ_attr_value_id = result[0]
81 else:
82 raise UserError('Record was not stored in product_attribute_value')
83 else:
84 produ_attr_value_id = match_found_id[0]
85 #########################################################################################
86
87 # search for match between product_tmpl_id and attribute_id in product_attribute_line
88 # if it does not exists, create one
89 #####################################################################################
90 match_prod_attribute = self.env['product.attribute.line'].search(
91 [
92 (
93 'product_tmpl_id',
94 '=',
95 prod_tmpl_id
96 ),
97 (
98 'attribute_id',
99 '=',
100 product_attribute_id[0]
101 )
102 ]
103 )
104
105 if not match_prod_attribute:
106 # then store product_tmpl_id and attribute_id in table product_attribute_line
107 try:
108 # prod_attr_line_id = self.store_values_in_table('product.attribute.line', {'product_tmpl_id':prod_tmpl_id, 'attribute_id':product_attribute_id})
109 prod_attr_line_id = self.env['product.attribute.line'].create(
110 {
111 'product_tmpl_id': prod_tmpl_id,
112 'attribute_id': product_attribute_id[0]
113 }
114 )
115 except:
116 raise UserWarning('Error with storing in product.attribute.line')
117 else:
118 prod_attr_line_id = match_prod_attribute
119
120 # search for match between product_tmpl_id and attribute_id in product_attribute_line
121 # if it does not exists, create one
122 #####################################################################################
123 self.env.cr.execute(
124 "SELECT * FROM product_attribute_line_product_attribute_value_rel WHERE "
125 "product_attribute_line_id = %s AND product_attribute_value_id = %s ",
126 (prod_attr_line_id.id, produ_attr_value_id))
127
128 match_final = self.env.cr.fetchone()
129
130 if match_final is None:
131 try:
132 self.env.cr.execute(
133 "INSERT INTO product_attribute_line_product_attribute_value_rel (product_attribute_line_id, product_attribute_value_id) "
134 "VALUES (%s, %s)",
135 (prod_attr_line_id.id, produ_attr_value_id))
136
137 except:
138 raise UserWarning('Problem with %s %s' % (prod_attr_line_id.id, produ_attr_value_id))
139 else:
140 continue