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

fastapi: add base service for exposing search/get #382

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions fastapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from . import models
from . import fastapi_dispatcher
from . import utils
27 changes: 27 additions & 0 deletions fastapi/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2023 Akretion (https://www.akretion.com).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this code has nothing to do with fastapi....

# @author Sébastien BEAU <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).


from odoo import _, models
from odoo.exceptions import MissingError
from odoo.osv import expression


class FilteredDomainAdapter:
def __init__(self, model: models.BaseModel, base_domain: list):
self._model = model
self._base_domain = base_domain

def get(self, record_id: int) -> models.BaseModel:
record = self._model.browse(record_id).filtered_domain(self._base_domain)
if record:
return record
else:
raise MissingError(_("The record do not exist"))

def search_with_count(self, domain: list, limit, offset):
domain = expression.AND([self._base_domain, domain])

count = self._model.search_count(domain)
return count, self._model.search(domain, limit=limit, offset=offset)
Loading