Skip to content

Commit 4f9924c

Browse files
committed
[ADD]
odoo_convert_path_to_image.py : take a csv file, modify it convert path to binary encoded in base64, ideal to import product image odoo_convert_url_to_image.py: Take a csv file, convert the given column from url to the content encoded in base64 Add mapper for conversion from URL Change version to 2.2.0
1 parent 2f4c29b commit 4f9924c

11 files changed

+160
-4
lines changed

odoo_convert_path_to_image.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python
2+
#-*- coding: utf-8 -*-
3+
'''
4+
Copyright (C) Thibault Francois
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU Lesser General Public License as
8+
published by the Free Software Foundation, version 3.
9+
10+
This program is distributed in the hope that it will be useful, but
11+
WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Lesser Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public License
16+
along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
'''
18+
19+
import argparse
20+
import os
21+
from odoo_csv_tools.lib import mapper
22+
from odoo_csv_tools.lib.transform import Processor
23+
24+
if __name__ == '__main__':
25+
parser = argparse.ArgumentParser(description='Convert csv column Image Path into base64')
26+
parser.add_argument('file', metavar='F', help='file to convert')
27+
parser.add_argument('--path', dest='path', help='Image Path Prefix, default is the working directory')
28+
parser.add_argument('--out', dest='out', help='name of the result file, default out.csv', default="out.csv")
29+
parser.add_argument('-f', dest='fields', help='Fields to convert from path to base64, comma separated', required = True)
30+
args = parser.parse_args()
31+
32+
file_csv = args.file
33+
out_csv = args.out
34+
path = args.path
35+
fields = args.fields
36+
if not path:
37+
path = os.getcwd()
38+
if not path.endswith(os.sep):
39+
path += os.sep
40+
41+
42+
processor = Processor(file_csv)
43+
mapping = processor.get_o2o_mapping()
44+
for f in fields.split(','):
45+
f = f.strip()
46+
mapping[f] = mapper.binary_map(mapper.remove_sep_mapper(f), path)
47+
processor.process(mapping, out_csv, {}, 'list')
48+
processor.write_to_file("")
49+

odoo_convert_url_to_image.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env python
2+
#-*- coding: utf-8 -*-
3+
'''
4+
Copyright (C) Thibault Francois
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU Lesser General Public License as
8+
published by the Free Software Foundation, version 3.
9+
10+
This program is distributed in the hope that it will be useful, but
11+
WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Lesser Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public License
16+
along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
'''
18+
19+
import argparse
20+
import os
21+
from odoo_csv_tools.lib import mapper
22+
from odoo_csv_tools.lib.transform import Processor
23+
24+
if __name__ == '__main__':
25+
parser = argparse.ArgumentParser(description='Convert csv column Image URL into base64')
26+
parser.add_argument('file', metavar='F', help='file to convert')
27+
parser.add_argument('--out', dest='out', help='name of the result file, default out.csv', default="out.csv")
28+
parser.add_argument('-f', dest='fields', help='Fields to convert from path to base64, comma separated', required = True)
29+
args = parser.parse_args()
30+
31+
file_csv = args.file
32+
out_csv = args.out
33+
fields = args.fields
34+
35+
processor = Processor(file_csv)
36+
mapping = processor.get_o2o_mapping()
37+
for f in fields.split(','):
38+
f = f.strip()
39+
mapping[f] = mapper.binary_url(f, verbose=True)
40+
processor.process(mapping, out_csv, {}, 'list')
41+
processor.write_to_file("")
42+

odoo_csv_tools/lib/internal/io.py

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ def get_model():
5555
context = '--context="%s"' % str(context) if context else ''
5656
conf_file = conf_file or "%s%s%s" % ('conf', os.sep, 'connection.conf')
5757
write_csv(filename, header, data)
58+
if not launchfile:
59+
return
5860

5961
mode = init and 'w' or 'a'
6062
with open(launchfile, mode) as myfile:

odoo_csv_tools/lib/mapper.py

+33
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from . internal.exceptions import SkippingException
77
import base64
88
import os
9+
import requests
910

1011
def str_to_mapper(field):
1112
if is_string(field):
@@ -145,6 +146,27 @@ def binary_val(line):
145146
def binary(field, path_prefix, skip=False, encoding="utf-8"):
146147
return binary_map(val(field), path_prefix, skip=skip, encoding=encoding)
147148

149+
150+
151+
def binary_url_map(mapper, skip=False, verbose=False):
152+
def binary_url_fun(line):
153+
url = mapper(line)
154+
if verbose:
155+
print("Fetch %s" % url)
156+
res = requests.get(url)
157+
if not res.status_code == 200:
158+
if skip:
159+
raise SkippingException("Cannot fetch file at url %s" % url)
160+
return ''
161+
162+
return base64.b64encode(res.content)
163+
return binary_url_fun
164+
165+
def binary_url(field, skip=False, verbose=False):
166+
return binary_url_map(val(field), skip=skip, verbose=verbose)
167+
168+
169+
148170
"""
149171
Specific to attribute mapper for V9 product.attribute_import
150172
"""
@@ -256,6 +278,17 @@ def split_m2m_value_fun(line):
256278
return s
257279
return split_m2m_value_fun
258280

281+
def remove_sep_mapper(f):
282+
"""
283+
@param f: field that will have the starting folder separator removed
284+
"""
285+
def remove_sep_mapper_fun(line):
286+
if line[f].startswith(os.sep):
287+
return line[f][len(os.sep):]
288+
else:
289+
return line[f]
290+
return remove_sep_mapper_fun
291+
259292

260293
##############################
261294
# #

odoo_csv_tools/lib/transform.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def get_o2o_mapping(self):
6161
}
6262
"""
6363
mapping = {}
64-
for column in self.header:
64+
for column in [h for h in self.header if h]:
6565
map_val_rep = ReprWrapper("mapper.val('%s')" %column, mapper.val(column))
6666
mapping[str(column)] = map_val_rep
6767
return mapping

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
odoo-client-lib==1.2.0
22
unicodecsv==0.14.1
33
future==0.16.0
4+
requests==2.11.1

setup.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
from setuptools import setup, find_packages
1919

2020
setup(name='odoo-import-export-client',
21-
version='2.1.3',
22-
install_requires=['odoo-client-lib', 'future', 'unicodecsv'],
21+
version='2.2.0',
22+
install_requires=['odoo-client-lib', 'future', 'unicodecsv', 'requests'],
2323
description='Library and script that allow to export and import data to Odoo using rpc api.',
2424
author='Thibault Francois',
2525
author_email='[email protected]',
2626
url='https://github.com/tfrancoi/odoo_csv_import',
2727
packages=find_packages(exclude=['contrib', 'docs', 'tests*']),
28-
scripts=['odoo_export_thread.py', 'odoo_import_thread.py'],
28+
scripts=['odoo_export_thread.py', 'odoo_import_thread.py', 'odoo_convert_path_to_image.py', 'odoo_convert_url_to_image.py'],
2929
long_description="See the home page for any information: https://github.com/tfrancoi/odoo_csv_import",
3030
keywords="odoo library import export thread python client lib web service",
3131
license="LGPLv3",

tests/7_convert_binary.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
$1 ../odoo_convert_path_to_image.py --path=./origin/img/ -f Image origin/contact.csv
3+
$1 ../odoo_convert_url_to_image.py -f Image origin/contact_url.csv
4+

tests/clean.sh

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ rm 3_product_import.sh
1111
rm 4_product_import.sh
1212
rm .coverage
1313
rm error.log
14+
rm out.csv

tests/launch_test.sh

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env bash
22
#Need to launch odoo database accessible with the configuration given in conf/connection.conf
3+
#test works well on V11
34
#Modules contacts need to be installed
45
#EXEC="python2"
56
for EXEC in "python2" "python3" "python3.7" "coverage run -a"
@@ -29,5 +30,7 @@ do
2930
sh 5_partner_export.sh "$EXEC"
3031
echo "> Import One2Many"
3132
sh 6_o2m_import.sh "$EXEC"
33+
echo "> Convert Binary"
34+
sh 7_convert_binary.sh "$EXEC"
3235
coverage html
3336
done

tests/origin/contact_url.csv

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Company_ID;Company_Name;Phone;www;Account_Manager;address1;city;zip code;country;IsCustomer;IsSupplier;Language;Image;
2+
COM15602;Company BV;8988 2436428 ;www.company.com;Thibault;Rue de la poste;Brussel;14589;Belgique;1;0;English;https://github.com/tfrancoi/odoo_csv_import/raw/master/tests/origin/img/employee_al-image.jpg;
3+
COM12639;John SPRL;56528 221505;www.john.fr;Francois;Downing Street;Paris;14590;FR;1;;English;https://github.com/tfrancoi/odoo_csv_import/raw/master/tests/origin/img/employee_chs-image.jpg;
4+
COM20695;Odoo SA;98779 342 19 45 ;www.odoo.com;Francois;La prèle, 3;New york;14591;U.S;1;0;English;https://github.com/tfrancoi/odoo_csv_import/raw/master/tests/origin/img/employee_djj-image.png;
5+
COM7663;Kangoo LTD;1310 3240077;;Francois;Glass AV;Amsterdam;14592;U.S;1;;English;https://github.com/tfrancoi/odoo_csv_import/raw/master/tests/origin/img/employee_dzc-image.jpg;
6+
COM23594;Odoo INC;0465476 392761;;Thibault;Home sweet Home;Mechelen;14593;BE;1;;English;https://github.com/tfrancoi/odoo_csv_import/raw/master/tests/origin/img/employee_fme-image.jpg;
7+
COM25801;Mike Corsoft inc;6456444-7070;www.mike.crosfot;Thibault;PO Box 13821;Lyon;14594;FR;;1;Dutch;https://github.com/tfrancoi/odoo_csv_import/raw/master/tests/origin/img/employee_fpi-image.jpg;
8+
COM7778;Odoo Limited;679789 5034221;;Francois;Marnixstraat 21;SF;14595;U.S;;;English;https://github.com/tfrancoi/odoo_csv_import/raw/master/tests/origin/img/employee_fp-image.jpg;
9+
COM12268;Thomas sprl;65479845 5275812;;Thibault;Eggelaan 23;Delft;14596;NL;;;English;https://github.com/tfrancoi/odoo_csv_import/raw/master/tests/origin/img/employee_han-image.png;
10+
COM18683;Bois&co;654798 50 67 08 72;;Francois;J en M Sabbestraat 11;Marche-en-famenne;14597;Belgique;1;;English;https://github.com/tfrancoi/odoo_csv_import/raw/master/tests/origin/img/employee_hne-image.png;
11+
COM23750;Roger Namur;6579 2 729 51 03;www.sa.eu;Francois;Rue de la Fusée 96;Namur;14598;Belgique;;;English;https://github.com/tfrancoi/odoo_csv_import/raw/master/tests/origin/img/employee-image.png;
12+
COM15632;Company BV;8988 2436428 ;www.company.com;Thibault;Rue de la poste;Brussel;14589;Belgique;1;;French;https://github.com/tfrancoi/odoo_csv_import/raw/master/tests/origin/img/employee_jep-image.jpg;
13+
COM12539;John SPRL;56528 221505;www.john.fr;Francois;Downing Street;Paris;14590;FR;1;;English;https://github.com/tfrancoi/odoo_csv_import/raw/master/tests/origin/img/employee_jgo-image.jpg;
14+
COM20295;Odoo SA;98779 342 19 45 ;www.odoo.com;Francois;La prèle, 3;New york;14591;U.S;1;;English;https://github.com/tfrancoi/odoo_csv_import/raw/master/tests/origin/img/employee_jod-image.png;
15+
COM7693;Kangoo LTD;1310 3240077;;Francois;Glass AV;Amsterdam;14592;U.S;1;;English;https://github.com/tfrancoi/odoo_csv_import/raw/master/tests/origin/img/employee_jog-image.jpg;
16+
COM2344;Odoo INC;0465476 392761;;Thibault;Home sweet Home;Mechelen;14593;BE;1;;English;https://github.com/tfrancoi/odoo_csv_import/raw/master/tests/origin/img/employee_jth-image.png;
17+
COM22501;Mike Corsoft inc;6456444-7070;www.mike.crosfot;Thibault;PO Box 13821;Lyon;14594;FR;;1;English;https://github.com/tfrancoi/odoo_csv_import/raw/master/tests/origin/img/employee_jve-image.jpg;
18+
COM77238;Odoo Limited;679789 5034221;;Francois;Marnixstraat 21;SF;14595;U.S;;;Dutch;https://github.com/tfrancoi/odoo_csv_import/raw/master/tests/origin/img/employee_lur-image.png;
19+
COM122648;Thomas sprl;65479845 5275812;;Thibault;Eggelaan 23;Delft;14596;NL;;;English;https://github.com/tfrancoi/odoo_csv_import/raw/master/tests/origin/img/employee_mit-image.png;
20+
;Bois&co;654798 50 67 08 72;;Francois;J en M Sabbestraat 11;Marche-en-famenne;14597;Belgique;1;;English;https://github.com/tfrancoi/odoo_csv_import/raw/master/tests/origin/img/employee_ngh-image.jpg
21+
COM237540;;6579 2 729 51 03;www.verylongwebsitename-tocause-check-length-failed.eu;Francois;Rue de la Fusée 96;Namur;14598;Belgique;;;English;https://github.com/tfrancoi/odoo_csv_import/raw/master/tests/origin/img/employee_niv-image.jpg;

0 commit comments

Comments
 (0)