Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[15.0][ADD] web_field_text_limit: New module to add limits to texts #3079

Open
wants to merge 1 commit into
base: 15.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions setup/web_field_text_limit/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,
)
107 changes: 107 additions & 0 deletions web_field_text_limit/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
====================
Web Field Text Limit
====================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:132960eebb3b4b035d2fadac727eaca7758c0f8b88903d12f99a2d269e69bb18
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Alpha-red.png
:target: https://odoo-community.org/page/development-status
:alt: Alpha
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fweb-lightgray.png?logo=github
:target: https://github.com/OCA/web/tree/15.0/web_field_text_limit
:alt: OCA/web
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/web-15-0/web-15-0-web_field_text_limit
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/web&target_branch=15.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This module adds the possibility to limit the displayed length of char and text fields.

.. IMPORTANT::
This is an alpha version, the data model and design can change at any time without warning.
Only for development or testing purpose, do not use in production.
`More details on development status <https://odoo-community.org/page/development-status>`_

**Table of contents**

.. contents::
:local:

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

You can change the limit applied to the text from Settings > Technical > User Interface > Text Limits

If the limit is set to 0, no limit will be applied

Usage
=====

To use it in QWeb views, you need to assign the text widget in t-options if the field does not use it by
default, along with text_limit and the name of the limit to be applied.

.. code:: xml

<span t-field="name" t-options="{'text_limit': 'Journal Limit'}" />

Known issues / Roadmap
======================

For now, the limit is only applied to QWeb views, but the intention is to apply it to all views once the corresponding
option is added.

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

Bugs are tracked on `GitHub Issues <https://github.com/OCA/web/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/web/issues/new?body=module:%20web_field_text_limit%0Aversion:%2015.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* Tecnativa

Contributors
~~~~~~~~~~~~

* `Tecnativa <https://www.tecnativa.com>`_:

* Carlos Dauden
* Carlos Roca

Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

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

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.

This module is part of the `OCA/web <https://github.com/OCA/web/tree/15.0/web_field_text_limit>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions web_field_text_limit/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
18 changes: 18 additions & 0 deletions web_field_text_limit/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2025 Tecnativa - Carlos Roca
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
"name": "Web Field Text Limit",
"version": "15.0.1.0.0",
"author": "Tecnativa, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/web",
"development_status": "Alpha",
"license": "AGPL-3",
"category": "Web",
"summary": "Adds an option to be able to limit displayed size of a text field",
"depends": ["base"],
"data": [
"security/ir.model.access.csv",
"data/data.xml",
"views/text_limit_views.xml",
],
}
7 changes: 7 additions & 0 deletions web_field_text_limit/data/data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" ?>
<odoo noupdate="1">
<record model="text.limit" id="text_limit_test">
<field name="name">Journal Limit</field>
<field name="value">40</field>
</record>
</odoo>
2 changes: 2 additions & 0 deletions web_field_text_limit/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import ir_qweb_field_text
from . import text_limit
20 changes: 20 additions & 0 deletions web_field_text_limit/models/ir_qweb_field_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2025 Tecnativa - Carlos Roca
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from markupsafe import escape

from odoo import api, models

from odoo.addons.base.models.ir_qweb_fields import nl2br


class IrQwebFieldText(models.AbstractModel):
_inherit = "ir.qweb.field.text"

@api.model
def value_to_html(self, value, options):
res = super().value_to_html(value, options)

Check warning on line 15 in web_field_text_limit/models/ir_qweb_field_text.py

View check run for this annotation

Codecov / codecov/patch

web_field_text_limit/models/ir_qweb_field_text.py#L15

Added line #L15 was not covered by tests
if value and options.get("text_limit"):
limit = self.env["text.limit"].get_limit(options["text_limit"])

Check warning on line 17 in web_field_text_limit/models/ir_qweb_field_text.py

View check run for this annotation

Codecov / codecov/patch

web_field_text_limit/models/ir_qweb_field_text.py#L17

Added line #L17 was not covered by tests
if len(value) > limit:
res = nl2br(escape(value[:limit] + "..."))
return res

Check warning on line 20 in web_field_text_limit/models/ir_qweb_field_text.py

View check run for this annotation

Codecov / codecov/patch

web_field_text_limit/models/ir_qweb_field_text.py#L19-L20

Added lines #L19 - L20 were not covered by tests
45 changes: 45 additions & 0 deletions web_field_text_limit/models/text_limit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2025 Tecnativa - Carlos Roca
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, fields, models, tools


class TextLimit(models.Model):
_name = "text.limit"
_description = "Text Limit"

name = fields.Char("Usage", index=True, required=True)
value = fields.Integer(required=True, default=40)

_sql_constraints = [
(
"name_uniq",
"unique (name)",
"""Only one value can be defined for each given usage!""",
),
]

@api.model
@tools.ormcache("application")
def get_limit(self, application):
self.flush(["name", "value"])
self.env.cr.execute(

Check warning on line 25 in web_field_text_limit/models/text_limit.py

View check run for this annotation

Codecov / codecov/patch

web_field_text_limit/models/text_limit.py#L24-L25

Added lines #L24 - L25 were not covered by tests
"select value from text_limit where name=%s", (application,)
)
res = self.env.cr.fetchone()
return res[0] if res else 2

Check warning on line 29 in web_field_text_limit/models/text_limit.py

View check run for this annotation

Codecov / codecov/patch

web_field_text_limit/models/text_limit.py#L28-L29

Added lines #L28 - L29 were not covered by tests

@api.model_create_multi
def create(self, vals_list):
res = super().create(vals_list)
self.clear_caches()
return res

def write(self, data):
res = super().write(data)
self.clear_caches()
return res

Check warning on line 40 in web_field_text_limit/models/text_limit.py

View check run for this annotation

Codecov / codecov/patch

web_field_text_limit/models/text_limit.py#L38-L40

Added lines #L38 - L40 were not covered by tests

def unlink(self):
res = super().unlink()
self.clear_caches()
return res

Check warning on line 45 in web_field_text_limit/models/text_limit.py

View check run for this annotation

Codecov / codecov/patch

web_field_text_limit/models/text_limit.py#L43-L45

Added lines #L43 - L45 were not covered by tests
3 changes: 3 additions & 0 deletions web_field_text_limit/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
You can change the limit applied to the text from Settings > Technical > User Interface > Text Limits

If the limit is set to 0, no limit will be applied
4 changes: 4 additions & 0 deletions web_field_text_limit/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
* `Tecnativa <https://www.tecnativa.com>`_:

* Carlos Dauden
* Carlos Roca
1 change: 1 addition & 0 deletions web_field_text_limit/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module adds the possibility to limit the displayed length of char and text fields.
2 changes: 2 additions & 0 deletions web_field_text_limit/readme/ROADMAP.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
For now, the limit is only applied to QWeb views, but the intention is to apply it to all views once the corresponding
option is added.
6 changes: 6 additions & 0 deletions web_field_text_limit/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
To use it in QWeb views, you need to assign the text widget in t-options if the field does not use it by
default, along with text_limit and the name of the limit to be applied.

.. code:: xml

<span t-field="name" t-options="{'text_limit': 'Journal Limit'}" />
2 changes: 2 additions & 0 deletions web_field_text_limit/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink"
"access_text_limit","access_text_limit","model_text_limit","base.group_system",1,1,0,0
Loading
Loading