Skip to content

Commit

Permalink
Merge PR #387 into 16.0
Browse files Browse the repository at this point in the history
Signed-off-by dreispt
  • Loading branch information
OCA-git-bot committed May 3, 2024
2 parents 1c46409 + 792df9f commit 535f642
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
34 changes: 34 additions & 0 deletions fastapi/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,31 @@ def authenticated_partner_impl() -> Partner:
See the fastapi_endpoint_demo for an example"""


def optionally_authenticated_partner_impl() -> Partner | None:
"""This method has to be overriden when you create your fastapi app
and you need to get an optional authenticated partner into your endpoint.
"""


def authenticated_partner_env(
partner: Annotated[Partner, Depends(authenticated_partner_impl)]
) -> Environment:
"""Return an environment with the authenticated partner id in the context"""
return partner.with_context(authenticated_partner_id=partner.id).env


def optionally_authenticated_partner_env(
partner: Annotated[Partner | None, Depends(optionally_authenticated_partner_impl)],
env: Annotated[Environment, Depends(odoo_env)],
) -> Environment:
"""Return an environment with the authenticated partner id in the context if
the partner is not None
"""
if partner:
return partner.with_context(authenticated_partner_id=partner.id).env
return env


def authenticated_partner(
partner: Annotated[Partner, Depends(authenticated_partner_impl)],
partner_env: Annotated[Environment, Depends(authenticated_partner_env)],
Expand All @@ -66,6 +84,22 @@ def authenticated_partner(
return partner_env["res.partner"].browse(partner.id)


def optionally_authenticated_partner(
partner: Annotated[Partner | None, Depends(optionally_authenticated_partner_impl)],
partner_env: Annotated[Environment, Depends(optionally_authenticated_partner_env)],
) -> Partner | None:
"""If you need to get access to the authenticated partner if the call is
authenticated, you can add a dependency into the endpoint definition on this
method.
This method defer from authenticated_partner by the fact that it returns
None if the partner is not authenticated .
"""
if partner:
return partner_env["res.partner"].browse(partner.id)
return None


def paging(
page: Annotated[int, Query(gte=1)] = 1, page_size: Annotated[int, Query(gte=1)] = 80
) -> Paging:
Expand Down
14 changes: 13 additions & 1 deletion fastapi/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
from fastapi.testclient import TestClient

from ..context import odoo_env_ctx
from ..dependencies import authenticated_partner_impl
from ..dependencies import (
authenticated_partner_impl,
optionally_authenticated_partner_impl,
)


@tagged("post_install", "-at_install")
Expand Down Expand Up @@ -106,6 +109,15 @@ def _create_test_client(
)
if partner or authenticated_partner_impl not in dependencies:
dependencies[authenticated_partner_impl] = partial(lambda a: a, partner)
if partner and optionally_authenticated_partner_impl in dependencies:
raise ValueError(
"You cannot provide an override for the optionally_authenticated_partner_impl "
"dependency when creating a test client with a partner."
)
if partner or optionally_authenticated_partner_impl not in dependencies:
dependencies[optionally_authenticated_partner_impl] = partial(
lambda a: a, partner
)
app = app or self.default_fastapi_app or FastAPI()
router = router or self.default_fastapi_router
if router:
Expand Down

0 comments on commit 535f642

Please sign in to comment.