This commit is contained in:
jinling.yang
2024-06-28 10:10:46 +08:00
3 changed files with 110 additions and 140 deletions

View File

@@ -200,7 +200,7 @@ def _create(self, data_list):
@api.model
def unlink(self, ids):
def unlink(self):
""" unlink()
Deletes the records in ``self``.
@@ -210,47 +210,43 @@ def unlink(self, ids):
"""
if not self:
return True
if not ids:
return True
self.check_access_rights('unlink')
self.check_access_rule('unlink')
from odoo.addons.base.models.ir_model import MODULE_UNINSTALL_FLAG
for record_id in ids:
record = self.browse(record_id)
for func in record._ondelete_methods:
for func in self._ondelete_methods:
# func._ondelete is True if it should be called during uninstallation
if func._ondelete or not record._context.get(MODULE_UNINSTALL_FLAG):
func(record)
if func._ondelete or not self._context.get(MODULE_UNINSTALL_FLAG):
func(self)
# TOFIX: this avoids an infinite loop when trying to recompute a
# field, which triggers the recomputation of another field using the
# same compute function, which then triggers again the computation
# of those two fields
for field in record._fields.values():
record.env.remove_to_compute(field, record)
for field in self._fields.values():
self.env.remove_to_compute(field, self)
record.env.flush_all()
self.env.flush_all()
cr = record._cr
Data = record.env['ir.model.data'].sudo().with_context({})
Defaults = record.env['ir.default'].sudo()
Property = record.env['ir.property'].sudo()
Attachment = record.env['ir.attachment'].sudo()
cr = self._cr
Data = self.env['ir.model.data'].sudo().with_context({})
Defaults = self.env['ir.default'].sudo()
Property = self.env['ir.property'].sudo()
Attachment = self.env['ir.attachment'].sudo()
ir_property_unlink = Property
ir_model_data_unlink = Data
ir_attachment_unlink = Attachment
# mark fields that depend on 'record' to recompute them after 'record' has
# mark fields that depend on 'self' to recompute them after 'self' has
# been deleted (like updating a sum of lines after deleting one line)
with record.env.protecting(record._fields.values(), record):
record.modified(record._fields, before=True)
for sub_ids in cr.split_for_in_conditions(record.ids):
records = record.browse(sub_ids)
with self.env.protecting(self._fields.values(), self):
self.modified(self._fields, before=True)
for sub_ids in cr.split_for_in_conditions(self.ids):
records = self.browse(sub_ids)
# Check if the records are used as default properties.
refs = [f'{record._name},{id_}' for id_ in sub_ids]
refs = [f'{self._name},{id_}' for id_ in sub_ids]
if Property.search(
[('res_id', '=', False), ('value_reference', 'in', refs)],
limit=1):
@@ -260,7 +256,7 @@ def unlink(self, ids):
# Delete the records' properties.
ir_property_unlink |= Property.search([('res_id', 'in', refs)])
query = f'DELETE FROM "{record._table}" WHERE id IN %s'
query = f'DELETE FROM "{self._table}" WHERE id IN %s'
cr.execute(query, (sub_ids,))
# Removing the ir_model_data reference if the record being deleted
@@ -271,7 +267,7 @@ def unlink(self, ids):
# access rights restrictions, and with no context to avoid possible
# side-effects during admin calls.
data = Data.search(
[('model', '=', record._name), ('res_id', 'in', sub_ids)])
[('model', '=', self._name), ('res_id', 'in', sub_ids)])
ir_model_data_unlink |= data
# For the same reason, remove the defaults having some of the
@@ -283,13 +279,13 @@ def unlink(self, ids):
# ir_attachment is overridden to hide attachments of deleted
# records)
query = 'SELECT id FROM ir_attachment WHERE res_model=%s AND res_id IN %s'
cr.execute(query, (record._name, sub_ids))
cr.execute(query, (self._name, sub_ids))
ir_attachment_unlink |= Attachment.browse(
row[0] for row in cr.fetchall())
# invalidate the *whole* cache, since the orm does not handle all
# changes made in the database, like cascading delete!
record.env.invalidate_all(flush=False)
self.env.invalidate_all(flush=False)
if ir_property_unlink:
ir_property_unlink.unlink()
if ir_model_data_unlink:
@@ -298,58 +294,32 @@ def unlink(self, ids):
ir_attachment_unlink.unlink()
# DLE P93: flush after the unlink, for recompute fields depending on
# the modified of the unlink
record.env.flush_all()
self.env.flush_all()
# auditing: deletions are infrequent and leave no trace in the database
_unlink.info('User #%s deleted %s records with IDs: %r', record._uid,
record._name, record.ids)
_unlink.info('User #%s deleted %s records with IDs: %r', self._uid,
self._name, self.ids)
# This is used to restrict the access right to unlink a record
current_model_id = record.env['ir.model'].sudo().search(
[('model', '=', record._name)]).id
# access_right_rec = record.env['access.right'].sudo().search_read(
# [('model_id', '=', current_model_id)], ['model_id', 'is_delete',
# 'groups_id'])
# if access_right_rec and not record.env.is_admin():
# for rec in access_right_rec:
# group_name = record.env['ir.model.data'].sudo().search([
# ('model', '=', 'res.groups'),
# ('res_id', '=', rec['groups_id'][0])
# ]).name
# module_name = record.env['ir.model.data'].sudo().search([
# ('model', '=', 'res.groups'),
# ('res_id', '=', rec['groups_id'][0])
# ]).module
# group = module_name + "." + group_name
# if record.env.user.has_group(group):
# if rec['is_delete']:
# raise UserError(_('You are restricted from performing this'
# ' operation. Please contact the'
# ' administrator.'))
# 检查 'access.right' 模型是否存在于环境中
if 'access.right' in record.env:
# current_model_id = record.env['ir.model'].sudo().search([('model', '=', record._name)]).id
access_right_rec = record.env['access.right'].sudo().search_read(
[('model_id', '=', current_model_id)], ['model_id', 'is_delete', 'groups_id']
)
if access_right_rec and not record.env.is_admin():
current_model_id = self.env['ir.model'].sudo().search(
[('model', '=', self._name)]).id
access_right_rec = self.env['access.right'].sudo().search_read(
[('model_id', '=', current_model_id)], ['model_id', 'is_delete',
'groups_id'])
if access_right_rec and not self.env.is_admin():
for rec in access_right_rec:
group_data = record.env['ir.model.data'].sudo().search_read(
[('model', '=', 'res.groups'), ('res_id', '=', rec['groups_id'][0])],
['name', 'module']
)
if group_data:
group_name = group_data[0]['name']
module_name = group_data[0]['module']
group_xml_id = f"{module_name}.{group_name}"
if record.env.user.has_group(group_xml_id) and rec['is_delete']:
raise UserError(
_('You are restricted from performing this operation. Please contact the administrator.'))
else:
# 如果 'access.right' 模型不存在,可以在这里定义备选逻辑
pass
group_name = self.env['ir.model.data'].sudo().search([
('model', '=', 'res.groups'),
('res_id', '=', rec['groups_id'][0])
]).name
module_name = self.env['ir.model.data'].sudo().search([
('model', '=', 'res.groups'),
('res_id', '=', rec['groups_id'][0])
]).module
group = module_name + "." + group_name
if self.env.user.has_group(group):
if rec['is_delete']:
raise UserError(_('You are restricted from performing this'
' operation. Please contact the'
' administrator.'))
return True

View File

@@ -37,7 +37,7 @@ class sf_production_plan(models.Model):
_order = 'state_order asc, write_date desc'
name = fields.Char(string='制造订单')
active = fields.Boolean(string='已归档', default=True)
# active = fields.Boolean(string='已归档', default=True)
# selected = fields.Boolean(default=False)
# order_number = fields.Char(string='订单号')
order_deadline = fields.Datetime(string='订单交期')
@@ -101,17 +101,17 @@ class sf_production_plan(models.Model):
# return super(sf_production_plan, self.with_context(active_test=False))._search(
# args, offset, limit, order, count, access_rights_uid)
def archive(self):
"""
归档
"""
self.write({'active': False})
def unarchive(self):
"""
取消归档
"""
self.write({'active': True})
# def archive(self):
# """
# 归档
# """
# self.write({'active': False})
#
# def unarchive(self):
# """
# 取消归档
# """
# self.write({'active': True})
@api.model
def get_import_templates(self):

View File

@@ -63,7 +63,7 @@
</div>
<group>
<group string="基本信息">
<field name="active" invisible="1"/>
<!-- <field name="active" invisible="1"/> -->
<field name="production_id" widget="many2one_button"/>
<field name="product_id"/>
<field name="origin"/>