Skip to content

Commit

Permalink
shopinvader_api_sale: explicit filtering on partner
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienbeau committed Nov 6, 2023
1 parent 4c6cf5f commit 4c9144c
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions shopinvader_api_sale/routers/sales.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@

from odoo import api, models

from odoo.addons.base.models.res_partner import Partner as ResPartner
from odoo.addons.extendable_fastapi.schemas import PagedCollection
from odoo.addons.fastapi.dependencies import authenticated_partner_env, paging
from odoo.addons.fastapi.dependencies import (
authenticated_partner,
authenticated_partner_env,
paging,
)
from odoo.addons.fastapi.schemas import Paging
from odoo.addons.fastapi.utils import FilteredDomainAdapter
from odoo.addons.shopinvader_schema_sale.schemas import Sale, SaleSearch
Expand All @@ -23,11 +28,12 @@ def search(
params: Annotated[SaleSearch, Depends()],
paging: Annotated[Paging, Depends(paging)],
env: Annotated[api.Environment, Depends(authenticated_partner_env)],
partner: Annotated[ResPartner, Depends(authenticated_partner)],
) -> PagedCollection[Sale]:
"""Get / search sale orders. The list contains only sale orders from the
authenticated user"""
count, orders = env["shopinvader_api_sale.sales_router.helper"]._search(
paging, params
partner, paging, params
)
return PagedCollection[Sale](
count=count,
Expand All @@ -39,30 +45,40 @@ def search(
def get(
sale_id: int,
env: Annotated[api.Environment, Depends(authenticated_partner_env)],
partner: Annotated[ResPartner, Depends(authenticated_partner)],
) -> Sale:
"""
Get sale order of authenticated user with specific sale_id
"""
return Sale.from_sale_order(
env["shopinvader_api_sale.sales_router.helper"]._get(sale_id)
env["shopinvader_api_sale.sales_router.helper"]._get(partner, sale_id)
)


class ShopinvaderApiSaleSalesRouterHelper(models.AbstractModel):
_name = "shopinvader_api_sale.sales_router.helper"
_description = "Shopinvader Api Sale Service Helper"

@property
def filtered_model(self):
def _get_domain_adapter(self, partner):
return [
("partner_id", "=", partner.id),
("typology", "=", "sale"),
]

def _get_model_adapter(self, partner):
return FilteredDomainAdapter(
self.env["sale.order"], [("typology", "=", "sale")]
self.env["sale.order"],
[
("partner_id", "=", partner.id),
("typology", "=", "sale"),
],
)

def _get(self, record_id):
return self.filtered_model.get(record_id)
def _get(self, partner, record_id):
return self._get_model_adapter(partner).get(record_id)

def _search(self, paging, params):
return self.filtered_model.search_with_count(
def _search(self, partner, paging, params):
return self._get_model_adapter(partner).search_with_count(
params.to_odoo_domain(self.env),
limit=paging.limit,
offset=paging.offset,
Expand Down

0 comments on commit 4c9144c

Please sign in to comment.