Skip to content
Snippets Groups Projects
Commit 901393ca6efe authored by Brendan Masson's avatar Brendan Masson
Browse files

Add delete restrictions on analytic objects to maintain coherence

parent 5ab4da6db72d
No related branches found
No related tags found
No related merge requests found
......@@ -110,6 +110,7 @@
('view_type', '=', False),
('disabled_per_company', '=', False),
],
ondelete='restrict',
track_visibility='onchange',
index=True,
)
......@@ -180,6 +181,48 @@
return res
@AddMethod(superclass)
@api.cr_uid_context
def unlink(self, cr, uid, ids, context=None):
"""When removing this object, remove all associated analytic
codes referenced by this object.
Note: the method will fail if the code is referenced by any other
object due to the RESTRICT constraint. That is the intended
behavior.
"""
analytic_obj = self.pool['analytic.structure']
code_obj = self.pool['analytic.code']
# Gather analytic values
analytic_values = []
for record in self.browse(cr, uid, ids, context=context):
for prefix, struct in analytic.iteritems():
values = analytic_obj.extract_values(
cr, uid, record, struct,
prefix=prefix, context=context
)
analytic_values.append(values)
# Find all related codes
code_ids = []
for values in analytic_values:
filtered_code_ids = filter(None, values.values())
code_ids.extend(filtered_code_ids)
# Set analytic fields to null
if analytic_values:
# The keys are all the same
fields = analytic_values[0].keys()
vals = {field: False for field in fields}
self.write(cr, uid, ids, vals)
code_obj.unlink(cr, uid, code_ids, context=context)
res = super(superclass, self).unlink(
cr, uid, ids, context=context
)
return res
return (superclass,)
@classmethod
......@@ -300,6 +343,7 @@
related=".".join([column, code_col]),
relation="analytic.code",
required=req,
ondelete='restrict',
store=True
)
if model_col not in defaults:
......@@ -417,8 +461,6 @@
self._generate_code_ref_id(cr, uid, res, context=context)
if new_code_id:
from pprint import pprint
pprint('{},{}'.format(self._name, res))
code_osv.write(cr, uid, new_code_id, {
'origin_id': '{},{}'.format(self._name, res),
})
......@@ -488,6 +530,32 @@
return res
@AddMethod(superclass)
@api.cr_uid_ids_context
def unlink(self, cr, uid, ids, context=None):
"""When removing this object, remove all associated analytic
codes referenced by this object.
Note: the method will fail if the code is referenced by any other
object due to the RESTRICT constraint. That is the intended
behavior.
"""
code_obj = self.pool['analytic.code']
# Find all related codes
code_ids = [
record[column].id for record
in self.browse(cr, uid, ids, context=context)
]
res = super(superclass, self).unlink(
cr, uid, ids, context=context
)
# self.write(cr, uid, ids, {column: False}, context=context)
code_obj.unlink(cr, uid, code_ids, context=context)
return res
@AddMethod(superclass)
def _force_code(self, cr, uid, force_code_id, code_vals, context=None):
code_osv = self.pool['analytic.code']
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment