Skip to content

Commit

Permalink
refactor!: ModelData has naive filters/search enabled by default
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Jul 1, 2024
1 parent d557e3a commit cf16a86
Show file tree
Hide file tree
Showing 6 changed files with 623 additions and 187 deletions.
9 changes: 6 additions & 3 deletions ckanext/collection/utils/collection/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ class Collection(types.BaseCollection):
"serializer",
)

def __str__(self):
return f"{self.__class__.__name__}:{self.name or 'anonymous'}"

def __iter__(self) -> Iterator[Any]:
yield from self.data.range(self.pager.start, self.pager.end)

def __init__(
self,
name: str = "",
Expand Down Expand Up @@ -166,9 +172,6 @@ def replace_service(self, service: types.Service) -> types.Service | None:
setattr(self, service.service_name, service)
return old_service

def __iter__(self) -> Iterator[Any]:
yield from self.data.range(self.pager.start, self.pager.end)

def make_serializer(self, **kwargs: Any) -> Serializer[Any, Self]:
"""Return serializer."""
return self.SerializerFactory(self, **kwargs)
Expand Down
53 changes: 31 additions & 22 deletions ckanext/collection/utils/data/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class BaseSaData(
"""Data source for custom SQL statement."""

_data: cached_property[TStatement]
use_naive_filters: bool = internal.configurable_attribute(False)
use_naive_search: bool = internal.configurable_attribute(False)
use_naive_filters: bool = internal.configurable_attribute(True)
use_naive_search: bool = internal.configurable_attribute(True)
session: AlchemySession = internal.configurable_attribute(
default_factory=lambda self: model.Session,
)
Expand Down Expand Up @@ -86,31 +86,40 @@ def statement_with_filters(self, stmt: TStatement) -> TStatement:

params = self.attached.params
if self.use_naive_filters:
stmt = stmt.where(
sa.and_(
sa.true(),
*[
self._into_clause(stmt.selected_columns[name], params[name])
for name in self.attached.columns.filterable
if name in params
and params[name] != ""
and name in stmt.selected_columns
],
),
)

if self.use_naive_search:
if (q := params.get("q")) and "q" not in self.attached.columns.searchable:
filters = [
name
for name in self.attached.columns.filterable
if name in params
and params[name] != ""
and name in stmt.selected_columns
]

if filters:
stmt = stmt.where(
sa.or_(
sa.false(),
sa.and_(
*[
stmt.selected_columns[name].ilike(f"%{q}%")
for name in self.attached.columns.searchable
if name in stmt.selected_columns
self._into_clause(stmt.selected_columns[name], params[name])
for name in filters
],
),
)

if self.use_naive_search:
if (q := params.get("q")) and "q" not in self.attached.columns.searchable:
filters = [
name
for name in self.attached.columns.searchable
if name in stmt.selected_columns
]
if filters:
stmt = stmt.where(
sa.or_(
*[
stmt.selected_columns[name].ilike(f"%{q}%")
for name in filters
],
),
)
for name in self.attached.columns.searchable:
if name not in params or name not in stmt.selected_columns:
continue
Expand Down
Loading

0 comments on commit cf16a86

Please sign in to comment.