|
| 1 | +# Copyright 2022 ACSONE SA/NV |
| 2 | +# License LGPL-3.0 or later (http://www.gnu.org/licenses/LGPL). |
| 3 | + |
| 4 | +from typing import TYPE_CHECKING, Annotated |
| 5 | + |
| 6 | +from odoo.api import Environment |
| 7 | +from odoo.exceptions import AccessDenied |
| 8 | + |
| 9 | +from odoo.addons.base.models.res_partner import Partner |
| 10 | +from odoo.addons.base.models.res_users import Users |
| 11 | + |
| 12 | +from fastapi import Depends, Header, HTTPException, Query, status |
| 13 | +from fastapi.security import HTTPBasic, HTTPBasicCredentials |
| 14 | + |
| 15 | +from .context import odoo_env_ctx |
| 16 | +from .schemas import Paging |
| 17 | + |
| 18 | +if TYPE_CHECKING: |
| 19 | + from .models.fastapi_endpoint import FastapiEndpoint |
| 20 | + |
| 21 | + |
| 22 | +def company_id() -> int | None: |
| 23 | + """This method may be overriden by the FastAPI app to set the allowed company |
| 24 | + in the Odoo env of the endpoint. By default, the company defined on the |
| 25 | + endpoint record is used. |
| 26 | + """ |
| 27 | + return None |
| 28 | + |
| 29 | + |
| 30 | +def odoo_env(company_id: Annotated[int | None, Depends(company_id)]) -> Environment: |
| 31 | + env = odoo_env_ctx.get() |
| 32 | + if company_id is not None: |
| 33 | + env = env(context=dict(env.context, allowed_company_ids=[company_id])) |
| 34 | + |
| 35 | + yield env |
| 36 | + |
| 37 | + |
| 38 | +def authenticated_partner_impl() -> Partner: |
| 39 | + """This method has to be overriden when you create your fastapi app |
| 40 | + to declare the way your partner will be provided. In some case, this |
| 41 | + partner will come from the authentication mechanism (ex jwt token) in other cases |
| 42 | + it could comme from a lookup on an email received into an HTTP header ... |
| 43 | + See the fastapi_endpoint_demo for an example""" |
| 44 | + |
| 45 | + |
| 46 | +def optionally_authenticated_partner_impl() -> Partner | None: |
| 47 | + """This method has to be overriden when you create your fastapi app |
| 48 | + and you need to get an optional authenticated partner into your endpoint. |
| 49 | + """ |
| 50 | + |
| 51 | + |
| 52 | +def authenticated_partner_env( |
| 53 | + partner: Annotated[Partner, Depends(authenticated_partner_impl)], |
| 54 | +) -> Environment: |
| 55 | + """Return an environment with the authenticated partner id in the context""" |
| 56 | + return partner.with_context(authenticated_partner_id=partner.id).env |
| 57 | + |
| 58 | + |
| 59 | +def optionally_authenticated_partner_env( |
| 60 | + partner: Annotated[Partner | None, Depends(optionally_authenticated_partner_impl)], |
| 61 | + env: Annotated[Environment, Depends(odoo_env)], |
| 62 | +) -> Environment: |
| 63 | + """Return an environment with the authenticated partner id in the context if |
| 64 | + the partner is not None |
| 65 | + """ |
| 66 | + if partner: |
| 67 | + return partner.with_context(authenticated_partner_id=partner.id).env |
| 68 | + return env |
| 69 | + |
| 70 | + |
| 71 | +def authenticated_partner( |
| 72 | + partner: Annotated[Partner, Depends(authenticated_partner_impl)], |
| 73 | + partner_env: Annotated[Environment, Depends(authenticated_partner_env)], |
| 74 | +) -> Partner: |
| 75 | + """If you need to get access to the authenticated partner into your |
| 76 | + endpoint, you can add a dependency into the endpoint definition on this |
| 77 | + method. |
| 78 | + This method is a safe way to declare a dependency without requiring a |
| 79 | + specific implementation. It depends on `authenticated_partner_impl`. The |
| 80 | + concrete implementation of authenticated_partner_impl has to be provided |
| 81 | + when the FastAPI app is created. |
| 82 | + This method return a partner into the authenticated_partner_env |
| 83 | + """ |
| 84 | + return partner_env["res.partner"].browse(partner.id) |
| 85 | + |
| 86 | + |
| 87 | +def optionally_authenticated_partner( |
| 88 | + partner: Annotated[Partner | None, Depends(optionally_authenticated_partner_impl)], |
| 89 | + partner_env: Annotated[Environment, Depends(optionally_authenticated_partner_env)], |
| 90 | +) -> Partner | None: |
| 91 | + """If you need to get access to the authenticated partner if the call is |
| 92 | + authenticated, you can add a dependency into the endpoint definition on this |
| 93 | + method. |
| 94 | +
|
| 95 | + This method defer from authenticated_partner by the fact that it returns |
| 96 | + None if the partner is not authenticated . |
| 97 | + """ |
| 98 | + if partner: |
| 99 | + return partner_env["res.partner"].browse(partner.id) |
| 100 | + return None |
| 101 | + |
| 102 | + |
| 103 | +def paging( |
| 104 | + page: Annotated[int, Query(ge=1)] = 1, page_size: Annotated[int, Query(ge=1)] = 80 |
| 105 | +) -> Paging: |
| 106 | + """Return a Paging object from the page and page_size parameters""" |
| 107 | + return Paging(limit=page_size, offset=(page - 1) * page_size) |
| 108 | + |
| 109 | + |
| 110 | +def basic_auth_user( |
| 111 | + credential: Annotated[HTTPBasicCredentials, Depends(HTTPBasic())], |
| 112 | + env: Annotated[Environment, Depends(odoo_env)], |
| 113 | +) -> Users: |
| 114 | + username = credential.username |
| 115 | + password = credential.password |
| 116 | + try: |
| 117 | + response = ( |
| 118 | + env["res.users"] |
| 119 | + .sudo() |
| 120 | + .authenticate( |
| 121 | + db=env.cr.dbname, |
| 122 | + credential={ |
| 123 | + "type": "password", |
| 124 | + "login": username, |
| 125 | + "password": password, |
| 126 | + }, |
| 127 | + user_agent_env=None, |
| 128 | + ) |
| 129 | + ) |
| 130 | + return env["res.users"].browse(response.get("uid")) |
| 131 | + except AccessDenied as ad: |
| 132 | + raise HTTPException( |
| 133 | + status_code=status.HTTP_401_UNAUTHORIZED, |
| 134 | + detail="Incorrect username or password", |
| 135 | + headers={"WWW-Authenticate": "Basic"}, |
| 136 | + ) from ad |
| 137 | + |
| 138 | + |
| 139 | +def authenticated_partner_from_basic_auth_user( |
| 140 | + user: Annotated[Users, Depends(basic_auth_user)], |
| 141 | + env: Annotated[Environment, Depends(odoo_env)], |
| 142 | +) -> Partner: |
| 143 | + return env["res.partner"].browse(user.sudo().partner_id.id) |
| 144 | + |
| 145 | + |
| 146 | +def fastapi_endpoint_id() -> int: |
| 147 | + """This method is overriden by the FastAPI app to make the fastapi.endpoint record |
| 148 | + available for your endpoint method. To get the fastapi.endpoint record |
| 149 | + in your method, you just need to add a dependency on the fastapi_endpoint method |
| 150 | + defined below |
| 151 | + """ |
| 152 | + |
| 153 | + |
| 154 | +def fastapi_endpoint( |
| 155 | + _id: Annotated[int, Depends(fastapi_endpoint_id)], |
| 156 | + env: Annotated[Environment, Depends(odoo_env)], |
| 157 | +) -> "FastapiEndpoint": |
| 158 | + """Return the fastapi.endpoint record""" |
| 159 | + return env["fastapi.endpoint"].browse(_id) |
| 160 | + |
| 161 | + |
| 162 | +def accept_language( |
| 163 | + accept_language: Annotated[ |
| 164 | + str | None, |
| 165 | + Header( |
| 166 | + alias="Accept-Language", |
| 167 | + description="The Accept-Language header is used to specify the language " |
| 168 | + "of the content to be returned. If a language is not available, the " |
| 169 | + "server will return the content in the default language.", |
| 170 | + ), |
| 171 | + ] = None, |
| 172 | +) -> str: |
| 173 | + """This dependency is used at application level to document the way the language |
| 174 | + to use for the response is specified. The header is processed outside of the |
| 175 | + fastapi app to initialize the odoo environment with the right language. |
| 176 | + """ |
| 177 | + return accept_language |
0 commit comments