1
1
# Copyright 2013-2019 Camptocamp SA
2
2
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
3
- from odoo import _ , api , fields , models
3
+ from contextlib import contextmanager
4
+
5
+ from odoo import _ , api , fields , models , registry , tools
4
6
5
7
6
8
class StockBatchPicking (models .Model ):
@@ -36,6 +38,19 @@ def _get_options_to_add(self, carrier=None):
36
38
options = carrier .available_option_ids
37
39
return options .filtered (lambda rec : rec .mandatory or rec .by_default )
38
40
41
+ @contextmanager
42
+ @api .model
43
+ def _do_in_new_env (self ):
44
+ # Be careful with the test_enable flag, as this behavior won't be the same on tests.
45
+ # If in test mode, there won't be any concurrent threading.
46
+ if tools .config ["test_enable" ]:
47
+ yield self .env
48
+ return
49
+
50
+ with api .Environment .manage ():
51
+ with registry (self .env .cr .dbname ).cursor () as new_cr :
52
+ yield api .Environment (new_cr , self .env .uid , self .env .context )
53
+
39
54
@api .onchange ("carrier_id" )
40
55
def carrier_id_change (self ):
41
56
"""Inherit this method in your module"""
@@ -106,10 +121,11 @@ def create(self, values):
106
121
return super ().create (values )
107
122
108
123
def purge_tracking_references (self ):
109
- """Purge tracking for each picking and destination package"""
110
124
for batch in self :
111
125
move_lines = batch .move_line_ids
112
- packs = move_lines .result_package_id .filtered (lambda p : p .parcel_tracking )
126
+ packs = move_lines .result_package_id .filtered (
127
+ lambda p : p .parcel_tracking
128
+ )
113
129
if packs :
114
130
packs .write ({"parcel_tracking" : False })
115
131
pickings = self .env ["stock.picking" ].search (
@@ -120,3 +136,17 @@ def purge_tracking_references(self):
120
136
)
121
137
if pickings :
122
138
pickings .write ({"carrier_tracking_ref" : False })
139
+
140
+ #WARNING ! Do not call this function unless you know what you do ;-)
141
+ def purge_tracking_references_in_new_env (self ):
142
+ """Purge tracking for each picking and destination package"""
143
+ with self ._do_in_new_env () as new_env :
144
+
145
+ # labels = new_env['shipping.label']
146
+ new_self = self .with_env (new_env )
147
+ new_self .purge_tracking_references ()
148
+ # may not be necessary but we leave it here for now
149
+ self .env .cr .commit ()
150
+
151
+
152
+
0 commit comments