# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import models


class View(models.Model):
    _inherit = 'ir.ui.view'

    def _postprocess_access_rights(self, model, node):
        """ Compute and set on node access rights based on view type. Specific
        views can add additional specific rights like creating columns for
        many2one-based grouping views. """
        node = super(View, self)._postprocess_access_rights(model, node)

        Model = self.env[model]
        is_base_model = self.env.context.get('base_model_name', model) == model

        if node and node.tag in ('gantt'):
            for action, operation in (('create', 'create'), ('edit', 'write')):
                if (not node.get(action) and
                        not Model.check_access_rights(operation, raise_exception=False) or
                        not self._context.get(action, True) and is_base_model):
                    node.set(action, 'false')

        return node

    """def _postprocess_access_rights(self, model, node):
        Model = self.env[model].sudo(False)
        is_base_model = self.env.context.get('base_model_name', model) == model
        if node.tag in ('gantt'):
            for action, operation in (('create', 'create'), ('edit', 'write')):
                if (not node.get(action) and
                        not Model.check_access_rights(operation, raise_exception=False) or
                            not self._context.get(action, True) and is_base_model):
                    node.set(action, 'false')

        if node.tag in ('kanban', 'tree', 'form', 'activity'):
            for action, operation in (('create', 'create'), ('delete', 'unlink'), ('edit', 'write')):
                if (not node.get(action) and
                        not Model.check_access_rights(operation, raise_exception=False) or
                            not self._context.get(action, True) and is_base_model):
                    node.set(action, 'false')

        if node.tag == 'kanban':
            group_by_name = node.get('default_group_by')
            group_by_field = Model._fields.get(group_by_name)
            if group_by_field and group_by_field.type == 'many2one':
                group_by_model = Model.env[group_by_field.comodel_name]
                for action, operation in (
                ('group_create', 'create'), ('group_delete', 'unlink'), ('group_edit', 'write')):
                    if (not node.get(action) and
                            not group_by_model.check_access_rights(operation, raise_exception=False) or
                                not self._context.get(action, True) and is_base_model):
                        node.set(action, 'false')"""


