diff --git a/setup/shopinvader_api_lead/odoo/addons/shopinvader_api_lead b/setup/shopinvader_api_lead/odoo/addons/shopinvader_api_lead new file mode 120000 index 0000000000..f1369af6e2 --- /dev/null +++ b/setup/shopinvader_api_lead/odoo/addons/shopinvader_api_lead @@ -0,0 +1 @@ +../../../../shopinvader_api_lead \ No newline at end of file diff --git a/setup/shopinvader_api_lead/setup.py b/setup/shopinvader_api_lead/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/shopinvader_api_lead/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/shopinvader_api_lead/README.rst b/shopinvader_api_lead/README.rst new file mode 100644 index 0000000000..f42a25e9e7 --- /dev/null +++ b/shopinvader_api_lead/README.rst @@ -0,0 +1,61 @@ +==================== +Shopinvader API Lead +==================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:d2d1149686c5507f0e6531ff1fede8002b86efb86e63f5800a695e13abe424df + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-shopinvader%2Fodoo--shopinvader-lightgray.png?logo=github + :target: https://github.com/shopinvader/odoo-shopinvader/tree/16.0/shopinvader_api_lead + :alt: shopinvader/odoo-shopinvader + +|badge1| |badge2| |badge3| + +This addon defines a new Lead router and a new service to be able +to create simple CRM leads. + +**Table of contents** + +.. contents:: + :local: + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* ACSONE SA/NV + +Contributors +~~~~~~~~~~~~ + +* Marie Lejeune + +Maintainers +~~~~~~~~~~~ + +This module is part of the `shopinvader/odoo-shopinvader `_ project on GitHub. + +You are welcome to contribute. diff --git a/shopinvader_api_lead/__init__.py b/shopinvader_api_lead/__init__.py new file mode 100644 index 0000000000..629a7fe100 --- /dev/null +++ b/shopinvader_api_lead/__init__.py @@ -0,0 +1,2 @@ +from . import schemas +from . import routers diff --git a/shopinvader_api_lead/__manifest__.py b/shopinvader_api_lead/__manifest__.py new file mode 100644 index 0000000000..7242eea180 --- /dev/null +++ b/shopinvader_api_lead/__manifest__.py @@ -0,0 +1,27 @@ +# Copyright 2024 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "Shopinvader API Lead", + "summary": """ + Lead FastAPI adding a service for creating CRM leads.""", + "version": "16.0.1.0.0", + "license": "AGPL-3", + "author": "ACSONE SA/NV", + "website": "https://github.com/shopinvader/odoo-shopinvader", + "depends": [ + "crm", + "extendable_fastapi", + ], + "data": [ + "security/groups.xml", + "security/acl_crm_lead.xml", + ], + "external_dependencies": { + "python": [ + "fastapi", + "pydantic>=2.0.0", + "extendable-pydantic>=1.2.0", + ] + }, +} diff --git a/shopinvader_api_lead/readme/CONTRIBUTORS.rst b/shopinvader_api_lead/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000000..fa723c29e4 --- /dev/null +++ b/shopinvader_api_lead/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Marie Lejeune diff --git a/shopinvader_api_lead/readme/DESCRIPTION.rst b/shopinvader_api_lead/readme/DESCRIPTION.rst new file mode 100644 index 0000000000..ed420f699e --- /dev/null +++ b/shopinvader_api_lead/readme/DESCRIPTION.rst @@ -0,0 +1,2 @@ +This addon defines a new Lead router and a new service to be able +to create simple CRM leads. diff --git a/shopinvader_api_lead/routers/__init__.py b/shopinvader_api_lead/routers/__init__.py new file mode 100644 index 0000000000..06f2ed51e0 --- /dev/null +++ b/shopinvader_api_lead/routers/__init__.py @@ -0,0 +1 @@ +from .lead import lead_router diff --git a/shopinvader_api_lead/routers/lead.py b/shopinvader_api_lead/routers/lead.py new file mode 100644 index 0000000000..e1abf52b8c --- /dev/null +++ b/shopinvader_api_lead/routers/lead.py @@ -0,0 +1,22 @@ +# Copyright 2024 ACSONE SA/NV +# 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 + +from odoo.addons.fastapi.dependencies import odoo_env + +from ..schemas import Lead, LeadInput + +lead_router = APIRouter(tags=["leads"]) + + +@lead_router.post("/leads", status_code=201) +def create( + data: LeadInput, + env: Annotated[api.Environment, Depends(odoo_env)], +) -> Lead | None: + lead = env["crm.lead"].create(data.to_crm_lead_vals()) + return Lead.from_crm_lead(lead) diff --git a/shopinvader_api_lead/schemas/__init__.py b/shopinvader_api_lead/schemas/__init__.py new file mode 100644 index 0000000000..69df115dad --- /dev/null +++ b/shopinvader_api_lead/schemas/__init__.py @@ -0,0 +1 @@ +from .lead import Lead, LeadInput diff --git a/shopinvader_api_lead/schemas/lead.py b/shopinvader_api_lead/schemas/lead.py new file mode 100644 index 0000000000..e5bff4f377 --- /dev/null +++ b/shopinvader_api_lead/schemas/lead.py @@ -0,0 +1,34 @@ +# Copyright 2024 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + + +from extendable_pydantic import StrictExtendableBaseModel + + +class Lead(StrictExtendableBaseModel): + id: int + subject: str + email: str | None = None + description: str | None = None + + @classmethod + def from_crm_lead(cls, lead): + return cls.model_construct( + id=lead.id, + subject=lead.name, + email=lead.email_from or None, + description=lead.description or None, + ) + + +class LeadInput(StrictExtendableBaseModel): + subject: str + email: str | None = None + description: str | None = None + + def to_crm_lead_vals(self) -> dict: + return { + "name": self.subject, + "email_from": self.email, + "description": self.description, + } diff --git a/shopinvader_api_lead/security/acl_crm_lead.xml b/shopinvader_api_lead/security/acl_crm_lead.xml new file mode 100644 index 0000000000..57892ed86a --- /dev/null +++ b/shopinvader_api_lead/security/acl_crm_lead.xml @@ -0,0 +1,16 @@ + + + + + + crm.lead shopinvader lead user access + + + + + + + + + diff --git a/shopinvader_api_lead/security/groups.xml b/shopinvader_api_lead/security/groups.xml new file mode 100644 index 0000000000..c1767cbd35 --- /dev/null +++ b/shopinvader_api_lead/security/groups.xml @@ -0,0 +1,15 @@ + + + + + CRM Lead user + + + + diff --git a/shopinvader_api_lead/static/description/index.html b/shopinvader_api_lead/static/description/index.html new file mode 100644 index 0000000000..4a048153b1 --- /dev/null +++ b/shopinvader_api_lead/static/description/index.html @@ -0,0 +1,417 @@ + + + + + + +Shopinvader API Lead + + + +
+

Shopinvader API Lead

+ + +

Beta License: AGPL-3 shopinvader/odoo-shopinvader

+

This addon defines a new Lead router and a new service to be able +to create simple CRM leads.

+

Table of contents

+ +
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • ACSONE SA/NV
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is part of the shopinvader/odoo-shopinvader project on GitHub.

+

You are welcome to contribute.

+
+
+
+ + diff --git a/shopinvader_api_lead/tests/__init__.py b/shopinvader_api_lead/tests/__init__.py new file mode 100644 index 0000000000..28d0bf4da1 --- /dev/null +++ b/shopinvader_api_lead/tests/__init__.py @@ -0,0 +1 @@ +from . import test_post_lead diff --git a/shopinvader_api_lead/tests/test_post_lead.py b/shopinvader_api_lead/tests/test_post_lead.py new file mode 100644 index 0000000000..ae8a458a84 --- /dev/null +++ b/shopinvader_api_lead/tests/test_post_lead.py @@ -0,0 +1,80 @@ +# Copyright 2024 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +import json + +from fastapi import status +from requests import Response + +from odoo.exceptions import AccessError +from odoo.tests.common import tagged + +from odoo.addons.extendable_fastapi.tests.common import FastAPITransactionCase + +from ..routers.lead import lead_router + + +@tagged("post_install", "-at_install") +class TestShopinvaderAPILead(FastAPITransactionCase): + @classmethod + def setUpClass(cls) -> None: + super().setUpClass() + + cls.user_no_rights = cls.env["res.users"].create( + { + "name": "Test User Without Rights", + "login": "user_no_rights", + "groups_id": [(6, 0, [])], + } + ) + user_with_rights = cls.env["res.users"].create( + { + "name": "Test User With Rights", + "login": "user_with_rights", + "groups_id": [ + ( + 6, + 0, + [ + cls.env.ref( + "shopinvader_api_lead.shopinvader_lead_user_group" + ).id, + ], + ) + ], + } + ) + cls.default_fastapi_running_user = user_with_rights + cls.default_fastapi_router = lead_router + + def _create_unauthenticated_user_client(self): + return self._create_test_client(user=self.user_no_rights) + + def test_post_lead(self): + data = { + "email": "harry.potter@hogwarts.com", + "subject": "Question about your site", + "description": "

I would need more info. Can you email me?

", + } + with self._create_test_client(router=lead_router) as test_client: + response: Response = test_client.post("/leads", content=json.dumps(data)) + self.assertEqual( + response.status_code, + status.HTTP_201_CREATED, + msg=f"error message: {response.text}", + ) + + lead = self.env["crm.lead"].search([], order="id desc", limit=1) + self.assertEqual(lead.email_from, data["email"]) + self.assertEqual(lead.name, data["subject"]) + self.assertEqual(lead.description, data["description"]) + + def test_post_lead_authenticated_user(self) -> None: + data = { + "email": "harry.potter@hogwarts.com", + "subject": "Question about your site", + "description": "

I would need more info. Can you email me?

", + } + with self._create_unauthenticated_user_client() as test_client, self.assertRaises( + AccessError + ): + test_client.post("/leads", content=json.dumps(data))