8
8
from collections import defaultdict
9
9
10
10
from odoo import _ , api , exceptions , fields , models
11
- from odoo import SUPERUSER_ID
12
11
from odoo .exceptions import MissingError
13
12
14
13
_logger = logging .getLogger (__name__ )
@@ -132,8 +131,8 @@ def _compute_exchange_filename(self):
132
131
if not rec .exchange_filename :
133
132
rec .exchange_filename = rec .type_id ._make_exchange_filename (rec )
134
133
135
- @api .constrains ("edi_exchange_state" )
136
- def _compute_exchanged_on (self ):
134
+ @api .onchange ("edi_exchange_state" )
135
+ def _onchange_edi_exchange_state (self ):
137
136
for rec in self :
138
137
if rec .edi_exchange_state in ("input_received" , "output_sent" ):
139
138
rec .exchanged_on = fields .Datetime .now ()
@@ -193,9 +192,11 @@ def record(self):
193
192
# data from parent
194
193
if not self .model and not self .parent_id :
195
194
return None
196
- if not self .model and self .parent_id :
195
+ elif not self .model and self .parent_id :
197
196
return self .parent_id .record
198
- return self .env [self .model ].browse (self .res_id ).exists ()
197
+ elif self .model in self .env .registry .models :
198
+ return self .env [self .model ].browse (self .res_id ).exists ()
199
+ return None
199
200
200
201
def _set_file_content (
201
202
self , output_string , encoding = "utf-8" , field_name = "exchange_file"
@@ -222,7 +223,7 @@ def name_get(self):
222
223
result = []
223
224
for rec in self :
224
225
rec_name = rec .identifier
225
- if rec .res_id and rec .model :
226
+ if rec .res_id and rec .model and rec . record :
226
227
rec_name = rec .record .display_name
227
228
name = "[{}] {}" .format (rec .type_id .name , rec_name )
228
229
result .append ((rec .id , name ))
@@ -356,7 +357,8 @@ def action_open_related_exchanges(self):
356
357
self .ensure_one ()
357
358
if not self .related_exchange_ids :
358
359
return {}
359
- action = self .env .ref ("edi_oca.act_open_edi_exchange_record_view" )
360
+ action = self .env .ref (
361
+ "edi_oca.act_open_edi_exchange_record_view" ).sudo ().read ()[0 ]
360
362
action ["domain" ] = [("id" , "in" , self .related_exchange_ids .ids )]
361
363
return action
362
364
@@ -453,8 +455,8 @@ def _search(
453
455
count = False ,
454
456
access_rights_uid = access_rights_uid ,
455
457
)
456
- if self ._uid == SUPERUSER_ID :
457
- # rules do not apply for the superuser
458
+ if self .env . user . _is_system () :
459
+ # rules do not apply to group "Settings"
458
460
return len (ids ) if count else ids
459
461
460
462
# TODO highlight orphaned EDI records in UI:
@@ -531,7 +533,7 @@ def check_access_rule(self, operation):
531
533
"""In order to check if we can access a record, we are checking if we can access
532
534
the related document"""
533
535
super (EDIExchangeRecord , self ).check_access_rule (operation )
534
- if self ._uid == SUPERUSER_ID :
536
+ if self .env . user . _is_superuser () :
535
537
return
536
538
default_checker = self .env ["edi.exchange.consumer.mixin" ].get_edi_access
537
539
by_model_rec_ids = defaultdict (set )
@@ -584,8 +586,9 @@ def _inverse_res_id(self):
584
586
if not rec .model or not rec .res_id :
585
587
continue
586
588
try :
587
- rec .env [rec .model ].browse (rec .res_id ).write ({
588
- 'exchange_record_ids' : [(4 , rec .id )]
589
- })
589
+ if "exchange_record_ids" in rec .env [rec .model ]._fields :
590
+ rec .env [rec .model ].browse (rec .res_id ).write ({
591
+ 'exchange_record_ids' : [(4 , rec .id )]
592
+ })
590
593
except (KeyError , ValueError , MissingError ):
591
594
continue
0 commit comments