Skip to content
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
2 changes: 1 addition & 1 deletion .copier-answers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ odoo_test_flavor: Both
odoo_version: 20.0
org_name: Odoo Community Association (OCA)
org_slug: OCA
postgres_image: ''
postgres_image: postgis/postgis:16-3.4
rebel_module_groups: []
repo_description: geospatial
repo_name: geospatial
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
makepot: "true"
services:
postgres:
image: postgres:16
image: postgis/postgis:16-3.4
env:
POSTGRES_USER: odoo
POSTGRES_PASSWORD: odoo
Expand Down
4 changes: 2 additions & 2 deletions base_geoengine/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
"name": "Geospatial support for Odoo",
"version": "19.0.1.0.2",
"version": "20.0.1.0.0",
"category": "GeoBI",
"author": "Camptocamp,ACSONE SA/NV,Odoo Community Association (OCA)",
"license": "AGPL-3",
Expand All @@ -17,7 +17,7 @@
"views/ir_view_view.xml",
"views/geo_raster_layer_view.xml",
"views/geo_vector_layer_view.xml",
"security/ir.model.access.csv",
"security/ir.access.csv",
],
"assets": {
"web.assets_backend": [
Expand Down
89 changes: 7 additions & 82 deletions base_geoengine/domains.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import contextlib
import logging
import warnings

from odoo.fields import Domain
from odoo.models import BaseModel
from odoo.orm import domains
from odoo.orm.domains import (
CONDITION_OPERATORS,
NEGATIVE_CONDITION_OPERATORS,
SQL,
DomainCondition,
OptimizationLevel,
Query,
TableSQL,
)
from odoo.orm.identifiers import NewId

_logger = logging.getLogger(__name__)

GEO_OPERATORS = frozenset(
[
Expand All @@ -30,76 +23,7 @@
)


def checked(self) -> DomainCondition:
"""Validate `self` and return it if correct, otherwise raise an exception."""
if not isinstance(self.field_expr, str) or not self.field_expr:
self._raise("Empty field name", error=TypeError)
operator = self.operator.lower()
if operator != self.operator:
warnings.warn(
(
f"Deprecated since 19.0, the domain condition "
f"{(self.field_expr, self.operator, self.value)!r} "
f"should have a lower-case operator"
),
DeprecationWarning,
# <MOD>
stacklevel=2,
# </MOD>
)
return DomainCondition(self.field_expr, operator, self.value).checked()
if operator not in CONDITION_OPERATORS:
# <MOD>
if operator not in GEO_OPERATORS:
# </MOD>
self._raise("Invalid operator")

# check already the consistency for domain manipulation
# these are common mistakes and optimizations,
# do them here to avoid recreating the domain
# - NewId is not a value
# - records are not accepted, use values
# - Query and Domain values should be using a relational operator
# <MOD>
# from .models import BaseModel
# </MOD>

value = self.value
if value is None:
value = False
elif isinstance(value, NewId):
_logger.warning(
"Domains don't support NewId, use .ids instead, for %r",
(self.field_expr, self.operator, self.value),
)
operator = "not in" if operator in NEGATIVE_CONDITION_OPERATORS else "in"
value = []
elif isinstance(value, BaseModel):
_logger.warning(
"The domain condition %r should not have a value which is a model",
(self.field_expr, self.operator, self.value),
)
value = value.ids
elif isinstance(value, (Domain, Query, SQL)) and operator not in (
"any",
"not any",
"any!",
"not any!",
"in",
"not in",
):
# accept SQL object in the right part for simple operators
# use case: compare 2 fields
_logger.warning(
"The domain condition %r should use the 'any' or 'not any' operator.",
(self.field_expr, self.operator, self.value),
)
if value is not self.value:
return DomainCondition(self.field_expr, operator, value)
return self


def _to_sql(self, model: BaseModel, alias: str, query: Query) -> SQL:
def _to_sql(self, table: TableSQL) -> SQL:
"""Enhanced _to_sql that handles geospatial operators."""
field_expr, operator, value = self.field_expr, self.operator, self.value

Expand All @@ -111,12 +35,13 @@ def _to_sql(self, model: BaseModel, alias: str, query: Query) -> SQL:
f"{(field_expr, operator, value)}"
)

model: BaseModel = table._model
field = self._field(model)
model._check_field_access(field, "read")
return field.condition_to_sql(field_expr, operator, value, model, alias, query)
model.check_field_access(field, "read")
return field.condition_to_sql(table, field_expr, operator, value)

# For all other operators, use the original method
return original__to_sql(self, model, alias, query)
return original__to_sql(self, table)


def _optimize_step(self, model: BaseModel, level: OptimizationLevel) -> Domain:
Expand All @@ -142,8 +67,8 @@ def _optimize_step(self, model: BaseModel, level: OptimizationLevel) -> Domain:
original__optimize_step = DomainCondition._optimize_step
original__to_sql = DomainCondition._to_sql

DomainCondition.checked = checked
DomainCondition._to_sql = _to_sql
DomainCondition._optimize_step = _optimize_step

# The core DomainCondition.checked() reads this global at call time
domains.CONDITION_OPERATORS = domains.CONDITION_OPERATORS.union(GEO_OPERATORS)
160 changes: 87 additions & 73 deletions base_geoengine/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from odoo import fields
from odoo.fields import Domain
from odoo.models import BaseModel
from odoo.tools import SQL, Query
from odoo.orm.query import Query, TableSQL
from odoo.tools import SQL

from .fields import GeoField
from .geo_operators import GeoOperator
Expand Down Expand Up @@ -40,17 +41,17 @@

def _condition_to_sql(
self,
table: TableSQL,
field_expr: str,
operator: str,
value,
model: BaseModel,
alias: str,
query: Query,
) -> SQL:
"""
This method has been monkey patched in order to be able to include
geo_operators into the Odoo search method.
"""
model: BaseModel = table._model
alias = table._alias
if operator in GEO_OPERATORS:
current_field = model._fields.get(field_expr)
current_operator = GeoOperator(current_field)
Expand All @@ -60,66 +61,72 @@ def _condition_to_sql(
# We are having indirect geo_operator like (‘geom’, ‘geo_...’,
# {‘res.zip.poly’: [‘id’, ‘in’, [1,2,3]] })
ref_search = value
if not ref_search:
raise ValueError(
f"Missing geo reference for {field_expr!r}: expected "
f"{{'<model>.<geo field>': <domain>}}"
)
sub_queries = []
for key in ref_search:
i = key.rfind(".")
rel_model_name = key[0:i]
rel_col = key[i + 1 :]
if rel_model_name not in model.env or not isinstance(
model.env[rel_model_name]._fields.get(rel_col), GeoField
):
raise ValueError(
f"Invalid geo reference {key!r}: expected "
f"'<model>.<geo field>'"
)
rel_model = model.env[rel_model_name]
# we compute the attributes search on spatial rel
if ref_search[key]:
rel_alias = (
rel_model._table
+ "_"
+ "".join(random.choices(string.ascii_lowercase, k=5))
)
rel_query = where_calc(
rel_model,
ref_search[key],
active_test=True,
alias=rel_alias,
rel_alias = (
rel_model._table
+ "_"
+ "".join(random.choices(string.ascii_lowercase, k=5))
)
rel_query = where_calc(
rel_model,
ref_search[key],
active_test=True,
alias=rel_alias,
)
# Access rights and record rules (ir.access) on the related
# model are applied inside where_calc(), mirroring
# BaseModel._search().
left_sql = SQL.identifier(alias, field_expr)
right_sql = SQL.identifier(rel_alias, rel_col)
sql_operator = GEO_SQL_OPERATORS[operator]
if operator == "geo_equal":
rel_query.add_where(
SQL("%s %s %s", left_sql, sql_operator, right_sql)
)
# Record rules (ir.rule) on the related model are applied
# inside where_calc(), mirroring BaseModel._search().
if operator == "geo_equal":
rel_query.add_where(
f'"{alias}"."{field_expr}" {GEO_OPERATORS[operator]} '
f"{rel_alias}.{rel_col}"
)
elif operator in ("geo_greater", "geo_lesser"):
rel_query.add_where(
f"ST_Area({alias}.{field_expr}) "
f"{GEO_OPERATORS[operator]} "
f"ST_Area({rel_alias}.{rel_col})"
elif operator in ("geo_greater", "geo_lesser"):
rel_query.add_where(
SQL(
"ST_Area(%s) %s ST_Area(%s)",
left_sql,
sql_operator,
right_sql,
)
else:
rel_query.add_where(
f'{GEO_OPERATORS[operator]}("{alias}"."{field_expr}", '
f"{rel_alias}.{rel_col})"
)

subquery_sql = rel_query.subselect("1")
sub_query_mogrified = (
model.env.cr.mogrify(subquery_sql.code, subquery_sql.params)
.decode("utf-8")
.replace(f"'{rel_model._table}'", f'"{rel_model._table}"')
.replace("%", "%%")
)
sub_queries.append(f"EXISTS({sub_query_mogrified})")
query_str = " AND ".join(sub_queries)
else:
query_str = get_geo_func(
current_operator, operator, field_expr, value, params, model._table
)
else:
rel_query.add_where(
SQL("%s(%s, %s)", sql_operator, left_sql, right_sql)
)

sub_queries.append(SQL("EXISTS(%s)", rel_query.subselect(SQL("1"))))
return SQL(" AND ").join(sub_queries)
query_str = get_geo_func(
current_operator, operator, field_expr, value, params, alias
)
return SQL(query_str, *params)
return original___condition_to_sql(
self,
table=table,
field_expr=field_expr,
operator=operator,
value=value,
model=model,
alias=alias,
query=query,
)


Expand Down Expand Up @@ -155,37 +162,44 @@ def where_calc(model, domain, active_test=True, alias=None):
This method is copied from base, we need to create our own query.

It mirrors ``BaseModel._search``: besides ``active_test`` filtering, it also
applies record rules (``ir.rule``) to the resulting query. This matters for
the indirect geo-operators, whose spatial sub-query is built here: without
it, the sub-query would match related records the user is not allowed to
read (row-level security bypass).
applies access rights and record rules (``ir.access``) to the resulting
query. This matters for the indirect geo-operators, whose spatial
sub-query is built here: without it, the sub-query would match related
records the user is not allowed to read (row-level security bypass).
"""
# if the object has an active field ('active', 'x_active'), filter out all
# inactive records unless they were explicitly asked for
if model._active_name and active_test and model._context.get("active_test", True):
# the item[0] trick below works for domain items and '&'/'|'/'!'
# operators too
if not any(item[0] == model._active_name for item in domain):
domain = [(model._active_name, "=", 1)] + domain

query = Query(model.env, alias, model._table)
if domain:
# In Odoo 19, create Domain object and use its _to_sql method
domain_obj = Domain(domain)
optimized_domain = domain_obj.optimize_full(model)
sql_condition = optimized_domain._to_sql(model, alias, query)
query.add_where(sql_condition)

# Apply record rules, like BaseModel._search does. Skipped for the
# superuser (env.su), exactly as in core.
if not model.env.su:
model.browse().check_access("read")
# the item[0] trick below works for domain items and '&'/'|'/'!'
# operators too
if (
model._active_name
and active_test
and model.env.context.get("active_test", True)
and not any(item[0] == model._active_name for item in domain)
):
domain = [(model._active_name, "=", True)] + domain

# Apply access rights and record rules, like BaseModel._search does.
# Skipped for the superuser (env.su), exactly as in core.
if model.env.su:
sec_domain = Domain.TRUE
else:
sec_domain = model._access_domain("read")
if sec_domain.is_false():
model.browse().check_access("read")

domain_obj = Domain(domain).optimize_full(model)

query = Query(model, alias)
if not domain_obj.is_true():
query.add_where(domain_obj._to_sql(query.table))

if not sec_domain.is_true():
model_sudo = model.sudo().with_context(active_test=False)
sec_domain = model.env["ir.rule"]._compute_domain(model._name, "read")
sec_domain = sec_domain.optimize_full(model_sudo)
sec_domain = sec_domain.optimize_full(model_sudo, search_domain=domain_obj)
if sec_domain.is_false():
query.add_where(SQL("FALSE"))
elif not sec_domain.is_true():
query.add_where(sec_domain._to_sql(model_sudo, alias, query))
query.add_where(sec_domain._to_sql(query.table._with_model(model_sudo)))

return query
Loading
Loading