-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate_variant_imgs.py
49 lines (40 loc) · 1.27 KB
/
update_variant_imgs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import signal
import base64
from importer import Import
class Variant(Import):
updated_ids = []
not_found_ids = []
model = 'product.product'
def get_variants(self):
model = 'ir.model.data'
domain = [
['model', '=', 'product.product'],
]
fields = ['res_id', 'name']
self.variants = self.api.search_read(model, domain, fields)
def process_row(self, row):
variant = list(filter(
lambda item: item['name'] == row[0],
self.variants))
if not len(variant):
# print("Product not found: {}".format(row[0]))
return
variant_id = variant[0]['res_id']
try:
with open("uploads/"+row[1], "rb") as image_file:
vals = {
'image_1920': base64.b64encode(
image_file.read()).decode('ascii')
}
self.api.update(self.model, [variant_id], vals)
self.updated_ids.append(variant_id)
except Exception as e:
print(e)
def main():
variant = Variant('variant_imgs.csv')
signal.signal(signal.SIGINT, variant.signal_handler)
variant.run()
if __name__ == '__main__':
main()