1
1
import logging
2
2
from os .path import basename
3
3
4
- import odoo
5
- from odoo import api
6
-
7
- from ..utils .config import config
4
+ from ..utils .registry import registry
8
5
from ..utils .data_files_utils import data_files
6
+ from ..utils .config import config
7
+ from odoo import modules as modules
9
8
10
9
_logger = logging .getLogger (__name__ )
11
10
@@ -23,7 +22,12 @@ def __init__(self):
23
22
filters = {}
24
23
field_rules = {}
25
24
26
- def apply_rules (self , record = {}, rules = {}):
25
+ def apply_rules (self , record = None , rules = None ):
26
+
27
+ if rules is None :
28
+ rules = {}
29
+ if record is None :
30
+ record = {}
27
31
28
32
# all rules should be registered here
29
33
_rules_register = {
@@ -43,42 +47,24 @@ def load_files(self, relevant_folder, model_name):
43
47
def load_file (self , file_ ):
44
48
if not file_ :
45
49
return []
46
- with api .Environment .manage ():
47
- registry = odoo .registry (config .db_name )
48
- with registry .cursor () as cr :
49
- uid = odoo .SUPERUSER_ID
50
- cr .execute ('SELECT model FROM ir_model ' )
51
- models = cr .dictfetchall ()
52
-
53
- # Checking if the model is available in database
54
- found = False
55
- for model in models :
56
- if model ['model' ] == self .model_name :
57
- found = True
58
-
59
- if (not config .init ) and found and (self .model_name not in odoo .api .Environment (cr , uid , {})):
60
- _logger .info ("Updating registry once" )
61
- registry .delete (config .db_name )
62
- config .init = True
63
-
64
- if self .model_name not in odoo .api .Environment (cr , uid , {}):
65
- _logger .warn ("Model '" + self .model_name + "' not found." )
66
- return False
67
-
68
- ctx = odoo .api .Environment (cr , uid , {})['res.users' ].context_get ()
69
- env = odoo .api .Environment (cr , uid , ctx )
70
-
71
- model = env ["base_import.import" ]
72
- import_wizard = model .create (
73
- {
74
- "res_model" : self .model_name ,
75
- "file" : file_ ,
76
- "file_type" : "text/csv" ,
77
- }
78
- )
79
- result = import_wizard .do (
80
- self .fields , {"quoting" : '"' , "separator" : "," , "headers" : True }
81
- )
50
+
51
+ env = registry .env
52
+ if self .model_name not in env :
53
+ _logger .warning ("Model '" + self .model_name + "' not found." )
54
+ return False
55
+
56
+ model = env ["base_import.import" ]
57
+ import_wizard = model .create (
58
+ {
59
+ "res_model" : self .model_name ,
60
+ "file" : file_ ,
61
+ "file_type" : "text/csv" ,
62
+ }
63
+ )
64
+ result = import_wizard .do (
65
+ self .fields , [], {"quoting" : '"' , "separator" : "," , "headers" : True }
66
+ )
67
+
82
68
return True
83
69
84
70
def _validate_mapping (self , mapping , file_header ):
@@ -92,7 +78,7 @@ def _validate_mapping(self, mapping, file_header):
92
78
validated_mapping [field [0 ]] = field [1 ]
93
79
else :
94
80
validated_mapping = {}
95
- _logger .warn ("Skipping file import, Field '" + field [1 ] + "' is missing" )
81
+ _logger .warning ("Skipping file import, Field '" + field [1 ] + "' is missing" )
96
82
break
97
83
return validated_mapping
98
84
@@ -144,11 +130,9 @@ def _pre_process(self, file_, mapping, filters_):
144
130
return data_files .build_csv (mapped_dict ) if mapped_dict else []
145
131
146
132
def _record_exist (self , record_id ):
147
- with api .Environment .manage ():
148
- registry = odoo .registry (config .db_name )
149
- with registry .cursor () as cr :
150
- cr .execute ("SELECT res_id FROM ir_model_data WHERE name='" + record_id + "';" )
151
- return cr .dictfetchall () or False
133
+ cr = registry .cursor
134
+ cr .execute ("SELECT res_id FROM ir_model_data WHERE name='" + record_id + "';" )
135
+ return cr .dictfetchall () or False
152
136
153
137
# Rule to delete a field that shouldn't be updated if found, from the record
154
138
def _no_update_rule (self , record = None , field = None ):
@@ -188,4 +172,4 @@ def load_(self):
188
172
data_files .get_checksum_path (file_ , self .model_name ), data_files .md5 (file_ )
189
173
)
190
174
else :
191
- _logger .warn ("File cannot be loaded: " + basename (file_ ))
175
+ _logger .warning ("File cannot be loaded: " + basename (file_ ))
0 commit comments