Skip to content

Commit

Permalink
[16.0][IMP] Add the possibility to disable create/edit from relationa…
Browse files Browse the repository at this point in the history
…l fields
  • Loading branch information
lmarion-source committed Sep 3, 2024
1 parent adcd164 commit f11a369
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 0 deletions.
91 changes: 91 additions & 0 deletions base_optional_create_edit/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3

=========================
Base Optional Create Edit
=========================

Add the possibility to remove create and create/edit from many2one fields, for a specific model

Installation
============

To install this module, you need to:

#. Do this ...

Configuration
=============

To configure this module, you need to:

#. Go to ...

.. figure:: path/to/local/image.png
:alt: alternative description
:width: 600 px

Usage
=====

To use this module, you need to:

#. Go to ...

.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/{repo_id}/{branch}

.. repo_id is available in https://github.com/OCA/maintainer-tools/blob/master/tools/repos_with_ids.txt
.. branch is "8.0" for example
Known issues / Roadmap
======================

* ...

Bug Tracker
===========

Bugs are tracked on `GitHub Issues
<https://github.com/OCA/{project_repo}/issues>`_. In case of trouble, please
check there if your issue has already been reported. If you spotted it first,
help us smash it by providing detailed and welcomed feedback.

Credits
=======

Images
------

* Odoo Community Association: `Icon <https://github.com/OCA/maintainer-tools/blob/master/template/module/static/description/icon.svg>`_.

Contributors
------------

* Firstname Lastname <[email protected]>
* Second Person <[email protected]>

Funders
-------

The development of this module has been financially supported by:

* Company 1 name
* Company 2 name

Maintainer
----------

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

This module is maintained by the OCA.

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

To contribute to this module, please visit https://odoo-community.org.
1 change: 1 addition & 0 deletions base_optional_create_edit/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
18 changes: 18 additions & 0 deletions base_optional_create_edit/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2024 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"name": "Base Optional Create Edit",
"summary": """
Add the possibility to remove create and create/edit
from many2one fields, for a specific model""",
"version": "16.0.1.0.0",
"license": "AGPL-3",
"author": "ACSONE SA/NV,Odoo Community Association (OCA)",
"website": "https://github.com/OCA/server-ux",
"depends": ["base"],
"data": [
"views/ir_model.xml",
],
"demo": [],
}
2 changes: 2 additions & 0 deletions base_optional_create_edit/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import ir_model
from . import ir_ui_view
11 changes: 11 additions & 0 deletions base_optional_create_edit/models/ir_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright 2024 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class IrModel(models.Model):

_inherit = "ir.model"

avoid_create_edit = fields.Boolean()
38 changes: 38 additions & 0 deletions base_optional_create_edit/models/ir_ui_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2024 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from lxml import etree

from odoo import api, models


class Model(models.AbstractModel):
_inherit = "base"

@api.model
def get_views(self, views, options=None):
res = super().get_views(views, options)
views = res.get("views")
view_form = views["form"]["arch"]
tree = etree.fromstring(view_form)
view_fields = set(tree.xpath(".//field[not(ancestor::field)]"))

field_names = []
for field_name, model in self._fields.items():
if model.relational:
ir_model = self.env["ir.model"].search(
[("model", "=", model.comodel_name)]
)
if ir_model.avoid_create_edit:
field_names.append(field_name)

Check warning on line 27 in base_optional_create_edit/models/ir_ui_view.py

View check run for this annotation

Codecov / codecov/patch

base_optional_create_edit/models/ir_ui_view.py#L27

Added line #L27 was not covered by tests

for view_field in view_fields:
if view_field.attrib["name"] in field_names:
view_field.set("can_create", "false")
view_field.set("can_write", "false")
view_field.set("no_create", "true")
view_field.set("no_edit", "true")

Check warning on line 34 in base_optional_create_edit/models/ir_ui_view.py

View check run for this annotation

Codecov / codecov/patch

base_optional_create_edit/models/ir_ui_view.py#L31-L34

Added lines #L31 - L34 were not covered by tests

view_form = etree.tostring(tree)
res["views"]["form"]["arch"] = view_form
return res
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions base_optional_create_edit/views/ir_model.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2024 ACSONE SA/NV
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>
<record id="view_model_form" model="ir.ui.view">
<field name="model">ir.model</field>
<field name="inherit_id" ref="base.view_model_form" />
<field name="arch" type="xml">
<field name="transient" position="after">
<field name="avoid_create_edit" />
</field>
</field>
</record>
</odoo>
6 changes: 6 additions & 0 deletions setup/base_optional_create_edit/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit f11a369

Please sign in to comment.