8000 16.0-[MIG] base_order_by_related to 16.0 by ntsirintanis · Pull Request #3288 · OCA/server-tools · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

16.0-[MIG] base_order_by_related to 16.0 #3288

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

Draft
wants to merge 9 commits into
base: 16.0
Choose a base branch
from
Draft
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
97 changes: 97 additions & 0 deletions base_order_by_related/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
=====================
Base order by related
=====================

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

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |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%2Fserver--tools-lightgray.png?logo=github
:target: https://github.com/OCA/server-tools/tree/16.0/base_order_by_related
:alt: OCA/server-tools
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/server-tools-16-0/server-tools-16-0-base_order_by_related
: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/server-tools&target_branch=16.0
:alt: Try me on Runboat

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

This module allows a search to order by a non-stored related field.

**Table of contents**

.. contents::
:local:

Usage
=====

In any model where you have a non-stored related field, you can make it sortable::

class ResPartner(models.Model):
_inherit = "res.partner"

country_name = fields.Char(related="country_id.name")

# make some non-stored related fields sortable
_order_by_related = ["country_name"]

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

Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-tools/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/server-tools/issues/new?body=module:%20base_order_by_related%0Aversion:%2016.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
~~~~~~~

* Therp BV

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

* Tom Blauwendraat <tblauwendraat@therp.nl>

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.

.. |maintainer-thomaspaulb| image:: https://github.com/thomaspaulb.png?size=40px
:target: https://github.com/thomaspaulb
:alt: thomaspaulb

Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-thomaspaulb|

This module is part of the `OCA/server-tools <https://github.com/OCA/server-tools/tree/16.0/base_order_by_related>`_ 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 base_order_by_related/__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_order_by_related/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (C) 2025 Therp (<http://www.therp.nl>).
# @author Tom Blauwendraat <tblauwendraat@therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"name": "Base order by related",
"summary": "Order by non-stored related fields",
"version": "16.0.1.0.0",
"category": "Tools", EDBE
"website": "https://github.com/OCA/server-tools",
"author": "Therp BV, Odoo Community Association (OCA)",
"maintainers": ["thomaspaulb"],
"license": "AGPL-3",
"application": False,
"installable": True,
"depends": ["base"],
"data": [],
}
19 changes: 19 additions & 0 deletions base_order_by_related/i18n/base_order_by_related.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * base_order_by_related
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 14.0\n"
"Report-Msgid-Bugs-To: \n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: base_order_by_related
#: model:ir.model,name:base_order_by_related.model_base
msgid "Base"
msgstr ""
22 changes: 22 additions & 0 deletions base_order_by_related/i18n/it.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * base_order_by_related
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 14.0\n"
"Report-Msgid-Bugs-To: \n"
"PO-Revision-Date: 2025-01-16 16:06+0000\n"
"Last-Translator: mymage <stefano.consolaro@mymage.it>\n"
"Language-Team: none\n"
"Language: it\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
"X-Generator: Weblate 5.6.2\n"

#. module: base_order_by_related
#: model:ir.model,name:base_order_by_related.model_base
msgid "Base"
msgstr "Base"
1 change: 1 addition & 0 deletions base_order_by_related/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
89 changes: 89 additions & 0 deletions base_order_by_related/models/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Copyright (C) 2023 Therp (<http://www.therp.nl>).
# @author Tom Blauwendraat <tblauwendraat@therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import logging

from odoo import api, models

_logger = logging.getLogger(__name__)


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

_order_by_related = []

@api.model
def _related_join_calc(self, alias, fname, query):
model, field = self, self._fields[fname]
while field.related:
# retrieve the parent model where field is inherited from
if field.related_field.model_name not in self.env:
continue
related_model = self.env[field.related_field.model_name]
if len(field.related) == 1:
related_fname = "id"
related_fname2 = field.related[0]
else:
related_fname = field.related[0]
related_fname2 = field.related[1]

# JOIN related_model._table AS related_alias ON
# alias.related_fname = related_alias.id
related_alias = query.left_join(
alias,
related_fname,
related_model._table,
"id",
related_fname2,
)
model, alias, field = related_model, related_alias, field.related_field
return '"%s"."%s"' % (alias, related_fname2)

@api.model
def _generate_order_by_inner(
self, alias, order_spec, query, reverse_direction=False, seen=None
):
order_parts = order_spec.split(",")
order_fields = [p.strip().split(" ")[0].strip() for p in order_parts]
if not any(f in self._order_by_related for f in order_fields):
return super()._generate_order_by_inner(
alias, order_spec, query, reverse_direction=reverse_direction, seen=seen
)

if seen is None:
seen = set()
self._check_qorder(order_spec)

order_by_elements = []
for order_part in order_parts:
order_split = order_part.strip().split(" ")
order_field = order_split[0].strip()
if order_field not in self._order_by_related:
order_by_elements += super()._generate_order_by_inner(
alias,
order_part,
query,
reverse_direction=reverse_direction,
seen=seen,
)
continue

self._fields.get(order_field)
order_direction = (
order_split[1].strip().upper() if len(order_split) == 2 else ""
)
if reverse_direction:
order_direction = "ASC" if order_direction == "DESC" else "DESC"
qualified_name = self._related_join_calc(alias, order_field, query)
order_by_elements.append("%s %s" % (qualified_name, order_direction))
return order_by_elements

@api.model
def fields_get(self, allfields=None, attributes=None):
ret = super().fields_get(allfields=allfields, attributes=attributes)
for k in self._order_by_related:
if k in ret:
ret[k]["sortable"] = True
return ret
1 change: 1 addition & 0 deletions base_order_by_related/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Tom Blauwendraat <tblauwendraat@therp.nl>
1 change: 1 addition & 0 deletions base_order_by_related/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module allows a search to order by a non-stored related field.
9 changes: 9 additions & 0 deletions base_order_by_related/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
In any model where you have a non-stored related field, you can make it sortable::

class ResPartner(models.Model):
_inherit = "res.partner"

country_name = fields.Char(related="country_id.name")

# make some non-stored related fields sortable
_order_by_related = ["country_name"]
Binary file added base_order_by_related/static/description/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading
0