11# Copyright 2021 Therp B.V.
22# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
33
4- from odoo import models , fields , api , modules , _
4+ from odoo import exceptions , models , fields , api , modules , _
55from odoo .addons .auditlog .models .rule import FIELDS_BLACKLIST
66
77
@@ -19,8 +19,22 @@ class AuditlogRule(models.Model):
1919 )
2020 all_fields = fields .Boolean (
2121 string = "Publicly visible log lines for all fields" ,
22+ readonly = True ,
23+ compute = "compute_all_fields" ,
24+ store = False ,
2225 help = "Check this field if you want this model to be visible by everyone" )
2326
27+ @api .depends ('field_ids' )
28+ def compute_all_fields (self ):
29+ for this in self .sudo ():
30+ this .all_fields = not bool (len (this .field_ids ))
31+
32+ @api .onchange ('model_id' )
33+ def onchange_model_id (self ):
34+ # if model changes we must wipe out all field ids
35+ self .field_ids = False
36+
37+
2438
2539 def get_field_ids_domain (self ):
2640 """ note this solution will work only with a hardcoded design of models,
@@ -34,27 +48,26 @@ def get_field_ids_domain(self):
3448 def unlink (self ):
3549 # if we delete auditlog rule, corresponding ir.rules are removed
3650 # TODO PROPOSAL: a warning here with detailed information?
51+ to_delete = self .env ["ir.rule" ].with_context (auditlog_write = True ).search (
52+ [("auditlog_id" , "=" , self .ids )])
3753 res = super (AuditlogRule , self ).unlink ()
38- return (
39- res
40- and self .env ["ir.rule" ]
41- .with_context (auditlog_write = True )
42- .search ([("auditlog_id" , "in" , self .ids )])
43- .unlink ()
44- )
45-
54+ if res :
55+ res = res and to_delete .unlink ()
56+ return res
4657
4758 @api .model
4859 def create (self , vals ):
4960 if 'group_ids' not in vals :
5061 # if group has been removed and no group specified ad base user default
5162 vals ['group_ids' ] = [(6 , 0 , [self .env .ref ('base.group_user' ).id ])]
52- if field_ids not in vals and not vals .get ('all_fields' ):
63+ if ' field_ids' not in vals and not vals .get ('all_fields' ):
5364 # this was done because of a
5465 # bug in attrs={'required': [('all_fields', '!=', True}
55- raise exceptions .validationerror (
66+ raise exceptions .ValidationError (
5667 _ ("please specify fields to log or make all_fields true" ))
57- return super (AuditlogRule , self ).create (vals )
68+ res = super (AuditlogRule , self ).create (vals )
69+ res .generate_rules ()
70+ return res
5871
5972 @api .multi
6073 def write (self , vals ):
0 commit comments