Skip to content

Commit

Permalink
[ADD] Address router to app + cart sub-app
Browse files Browse the repository at this point in the history
  • Loading branch information
marielejeune committed Jun 21, 2023
1 parent 68c1bd5 commit 0dcf906
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 40 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# generated from manifests external_dependencies
openupgradelib
fastapi
openupgradelib
11 changes: 10 additions & 1 deletion shopinvader_v2_app_demo/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,17 @@
"license": "AGPL-3",
"author": "ACSONE SA/NV",
"website": "https://github.com/shopinvader/odoo-shopinvader",
"depends": ["fastapi"],
"depends": [
"fastapi",
"shopinvader_api_address",
"shopinvader_api_sale",
"shopinvader_anonymous_partner",
"shopinvader_fastapi_auth_jwt",
],
"external_dependencies": {"python": ["fastapi"]},
"data": [
"views/fastapi_endpoint.xml",
],
"demo": [
"demo/res_users.xml",
"demo/res_groups.xml",
Expand Down
1 change: 0 additions & 1 deletion shopinvader_v2_app_demo/demo/fastapi_endpoint.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
]]></field>
<field name="app">shopinvader_demo</field>
<field name="root_path">/shopinvader_demo</field>
<field name="shopinvader_demo_auth_method">http_basic</field>
<field name="user_id" ref="shopinvader_app_demo_user" />
</record>
</odoo>
10 changes: 9 additions & 1 deletion shopinvader_v2_app_demo/demo/res_groups.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
<record id="shopinvader_app_demo_group" model="res.groups">
<field name="name">Shopinvader App Demo Group</field>
<field name="users" eval="[(4, ref('shopinvader_app_demo_user'))]" />
<field name="implied_ids" eval="[(4, ref('group_fastapi_endpoint_runner'))]" />
<field
name="implied_ids"
eval="[
(4, ref('fastapi.group_fastapi_endpoint_runner')),
(4, ref('shopinvader_api_address.shopinvader_address_user_group')),
(4, ref('shopinvader_api_cart.sale_cart_user_group')),
]"
/>

</record>
</odoo>
94 changes: 58 additions & 36 deletions shopinvader_v2_app_demo/models/fastapi_endpoint.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
# Copyright 2023 ACSONE SA/NV
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from typing import List
from functools import partial
from typing import Any, List

from fastapi import APIRouter
from fastapi import APIRouter, FastAPI

from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
from odoo import api, fields, models

from odoo.addons.fastapi.depends import (
authenticated_partner_from_basic_auth_user,
authenticated_partner_impl,
from odoo.addons.fastapi.dependencies import authenticated_partner_impl
from odoo.addons.fastapi_auth_jwt.dependencies import (
auth_jwt_authenticated_partner,
auth_jwt_default_validator_name,
)
from odoo.addons.shopinvader_api_address.routers.address_service import address_router
from odoo.addons.shopinvader_api_sale.routers import cart_router
from odoo.addons.shopinvader_fastapi_auth_jwt.dependencies import (
auth_jwt_authenticated_or_anonymous_partner_autocreate,
)


Expand All @@ -22,11 +28,7 @@ class FastapiEndpoint(models.Model):
selection_add=[("shopinvader_demo", "Shopinvader Demo Endpoint")],
ondelete={"shopinvader_demo": "cascade"},
)

shopinvader_demo_auth_method = fields.Selection(
selection=[("http_basic", "HTTP Basic")],
string="Authentication method",
)
auth_jwt_validator_id = fields.Many2one("auth.jwt.validator")

@api.model
def _get_fastapi_routers(self):
Expand All @@ -36,35 +38,55 @@ def _get_fastapi_routers(self):

@api.model
def _get_shopinvader_demo_fastapi_routers(self) -> List[APIRouter]:
return []
if "address" not in address_router.tags:
address_router.tags.append("address")
return [address_router]

@api.constrains("app", "shopinvader_demo_auth_method")
def _validate_demo_auth_method(self):
for rec in self:
if rec.app == "shopinvader_demo" and not rec.shopinvader_demo_auth_method:
raise ValidationError(
_(
"The authentication method is required for app %(app)s",
app=rec.app,
)
)
def _get_shopinvader_demo_tags(self, params) -> list:
tags_metadata = params.get("openapi_tags", []) or []
tags_metadata.append(
{
"name": "address",
"description": "Set of services to manage addresses",
}
)
base_url = self.env["ir.config_parameter"].sudo().get_param("web.base.url")
tags_metadata.append(
{
"name": "cart",
"description": "Set of services to manage carts",
"externalDocs": {
"description": "Cart services are available under "
"a specific authentication mechanism",
"url": f"{base_url}{self.root_path}/cart/docs",
},
}
)
return tags_metadata

@api.model
def _fastapi_app_fields(self) -> List[str]:
fields = super()._fastapi_app_fields()
fields.append("shopinvader_demo_auth_method")
return fields
def _prepare_fastapi_app_params(self) -> dict[str, Any]:
params = super()._prepare_fastapi_app_params()
if self.app == "shopinvader_demo":
params["openapi_tags"] = self._get_shopinvader_demo_tags(params)
return params

def _get_app(self):
app = super()._get_app()
if self.app == "shopinvader_demo_auth_method":
# Here we add the overrides to the authenticated_partner_impl method
# according to the authentication method configured on the demo app
if self.demo_auth_method == "http_basic":
authenticated_partner_impl_override = (
authenticated_partner_from_basic_auth_user
)
if self.app == "shopinvader_demo":
app.dependency_overrides[
authenticated_partner_impl
] = authenticated_partner_impl_override
] = auth_jwt_authenticated_partner
app.dependency_overrides[auth_jwt_default_validator_name] = partial(
lambda a: a, self.auth_jwt_validator_id.name or None
)
cart_app = FastAPI()
cart_app.include_router(cart_router)
cart_app.dependency_overrides[
authenticated_partner_impl
] = auth_jwt_authenticated_or_anonymous_partner_autocreate
cart_app.dependency_overrides[auth_jwt_default_validator_name] = partial(
lambda a: a, self.auth_jwt_validator_id.name or None
)
app.mount(self.root_path + "/carts", cart_app)

return app
25 changes: 25 additions & 0 deletions shopinvader_v2_app_demo/views/fastapi_endpoint.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2023 ACSONE SA/NV
License LGPL-3.0 or later (http://www.gnu.org/licenses/LGPL). -->
<odoo>

<record model="ir.ui.view" id="fastapi_endpoint_shopinvader_v2_app_demo_form_view">
<field name="model">fastapi.endpoint</field>
<field name="inherit_id" ref="fastapi.fastapi_endpoint_form_view" />
<field name="arch" type="xml">
<span name="configuration" position="after">
<group
name="shopinvader_configuration"
title="Configuration"
attrs="{'invisible': [('app', '!=', 'shopinvader_demo')]}"
>
<field
name="auth_jwt_validator_id"
attrs="{'required': [('app', '=', 'shopinvader_demo')]}"
/>
</group>
</span>
</field>
</record>

</odoo>
9 changes: 9 additions & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ vcrpy
vcrpy-unittest
unittest2 # For shopinvader test_controller, which inherits component
odoo-test-helper
fastapi @ git+https://github.com/tiangolo/fastapi@refs/pull/5452/head
odoo-addon-shopinvader-schema-address @ git+https://github.com/shopinvader/odoo-shopinvader@refs/pull/1361/head#subdirectory=setup/shopinvader_schema_address
odoo-addon-shopinvader-api-address @ git+https://github.com/shopinvader/odoo-shopinvader@refs/pull/1361/head#subdirectory=setup/shopinvader_api_address
odoo-addon-shopinvader-api-sale @ git+https://github.com/shopinvader/odoo-shopinvader@refs/pull/1369/head#subdirectory=setup/shopinvader_api_sale
odoo-addon-shopinvader-anonymous-partner @ git+https://github.com/shopinvader/odoo-shopinvader@refs/pull/1366/head#subdirectory=setup/shopinvader_anonymous_partner
odoo-addon-shopinvader-fastapi-auth-jwt @ git+https://github.com/shopinvader/odoo-shopinvader@refs/pull/1366/head#subdirectory=setup/shopinvader_fastapi_auth_jwt
odoo-addon-auth_jwt @ git+https://github.com/oca/server-auth@refs/pull/520/head#subdirectory=setup/auth_jwt
odoo-addon-fastapi_auth_jwt @ git+https://github.com/oca/rest-framework@refs/pull/347/head#subdirectory=setup/fastapi_auth_jwt
httpx # For FastAPI tests

0 comments on commit 0dcf906

Please sign in to comment.