-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] shopinvader_api_sale: Add sale_line router
- Loading branch information
1 parent
eb2df78
commit e21f162
Showing
4 changed files
with
215 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from .sales import sale_router | ||
from .sale_lines import sale_line_router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# Copyright 2024 Akretion (http://www.akretion.com). | ||
# @author Florian Mounier <[email protected]> | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
from typing import Annotated | ||
|
||
from fastapi import APIRouter, Depends | ||
|
||
from odoo import api, fields, 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, | ||
authenticated_partner_env, | ||
paging, | ||
) | ||
from odoo.addons.fastapi.schemas import Paging | ||
from odoo.addons.sale.models.sale_order_line import SaleOrderLine | ||
from odoo.addons.shopinvader_filtered_model.utils import FilteredModelAdapter | ||
|
||
from ..schemas import SaleLineSearch, SaleLineWithSale | ||
|
||
sale_line_router = APIRouter(tags=["sales"]) | ||
|
||
|
||
@sale_line_router.get("/sale_lines") | ||
def search( | ||
params: Annotated[SaleLineSearch, Depends()], | ||
paging: Annotated[Paging, Depends(paging)], | ||
env: Annotated[api.Environment, Depends(authenticated_partner_env)], | ||
partner: Annotated[ResPartner, Depends(authenticated_partner)], | ||
) -> PagedCollection[SaleLineWithSale]: | ||
"""Get / search sale order lines. The list contains only sale order lines from the | ||
authenticated user""" | ||
count, sols = ( | ||
env["shopinvader_api_sale.sale_line_router.helper"] | ||
.new({"partner": partner}) | ||
._search(paging, params) | ||
) | ||
return PagedCollection[SaleLineWithSale]( | ||
count=count, | ||
items=[SaleLineWithSale.from_sale_order_line(sol) for sol in sols], | ||
) | ||
|
||
|
||
@sale_line_router.get("/sale_lines/{sale_line_id}") | ||
def get( | ||
sale_line_id: int, | ||
env: Annotated[api.Environment, Depends(authenticated_partner_env)], | ||
partner: Annotated[ResPartner, Depends(authenticated_partner)], | ||
) -> SaleLineWithSale: | ||
""" | ||
Get sale order of authenticated user with specific sale_id | ||
""" | ||
return SaleLineWithSale.from_sale_order_line( | ||
env["shopinvader_api_sale.sale_line_router.helper"] | ||
.new({"partner": partner}) | ||
._get(sale_line_id) | ||
) | ||
|
||
|
||
class ShopinvaderApiSaleSaleLineRouterHelper(models.AbstractModel): | ||
_name = "shopinvader_api_sale.sale_line_router.helper" | ||
_description = "Shopinvader Api Sale Line Service Helper" | ||
|
||
partner = fields.Many2one("res.partner") | ||
|
||
def _get_domain_adapter(self): | ||
return [ | ||
("order_id.partner_id", "=", self.partner.id), | ||
("order_id.typology", "=", "sale"), | ||
] | ||
|
||
@property | ||
def model_adapter(self) -> FilteredModelAdapter[SaleOrderLine]: | ||
return FilteredModelAdapter[SaleOrderLine](self.env, self._get_domain_adapter()) | ||
|
||
def _get(self, record_id) -> SaleOrderLine: | ||
return self.model_adapter.get(record_id) | ||
|
||
def _search(self, paging, params) -> tuple[int, SaleOrderLine]: | ||
return self.model_adapter.search_with_count( | ||
params.to_odoo_domain(self.env), | ||
limit=paging.limit, | ||
offset=paging.offset, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Copyright 2024 Akretion (http://www.akretion.com). | ||
# @author Florian Mounier <[email protected]> | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
from datetime import datetime | ||
from typing import Annotated | ||
|
||
from extendable_pydantic import StrictExtendableBaseModel | ||
from pydantic import Field | ||
|
||
from odoo import api | ||
|
||
from odoo.addons.shopinvader_schema_sale.schemas.sale_line import SaleLine | ||
|
||
|
||
class SaleOrderFromLine(StrictExtendableBaseModel): | ||
id: int | ||
name: str | ||
state: str | ||
date_order: datetime | ||
|
||
@classmethod | ||
def from_sale_order(cls, odoo_rec): | ||
return cls.model_construct( | ||
id=odoo_rec.id, | ||
name=odoo_rec.name, | ||
state=odoo_rec.state, | ||
date_order=odoo_rec.date_order, | ||
) | ||
|
||
|
||
class SaleLineWithSale(SaleLine): | ||
order: SaleOrderFromLine | ||
|
||
@classmethod | ||
def from_sale_order_line(cls, odoo_rec): | ||
res = super().from_sale_order_line(odoo_rec) | ||
res.order = SaleOrderFromLine.from_sale_order(odoo_rec.order_id) | ||
return res | ||
|
||
|
||
class SaleLineSearch(StrictExtendableBaseModel, extra="ignore"): | ||
order_name: Annotated[ | ||
str | None, | ||
Field( | ||
description="When used, the search look for any sale order lines " | ||
"where the order name contains the given value case insensitively." | ||
), | ||
] = None | ||
product_name: Annotated[ | ||
str | None, | ||
Field( | ||
description="When used, the search look for any sale order lines " | ||
"where the product name contains the given value case insensitively." | ||
), | ||
] = None | ||
|
||
def to_odoo_domain(self, env: api.Environment): | ||
domain = [] | ||
if self.order_name: | ||
domain.append(("order_id.name", "ilike", self.order_name)) | ||
|
||
if self.product_name: | ||
domain.append(("product_id.name", "ilike", self.product_name)) | ||
|
||
return domain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters