diff --git a/mozaik_thesaurus_api/__init__.py b/mozaik_thesaurus_api/__init__.py new file mode 100644 index 000000000..882bbab34 --- /dev/null +++ b/mozaik_thesaurus_api/__init__.py @@ -0,0 +1,2 @@ +from . import pydantic_models +from . import services diff --git a/mozaik_thesaurus_api/__manifest__.py b/mozaik_thesaurus_api/__manifest__.py new file mode 100644 index 000000000..271cdcdbf --- /dev/null +++ b/mozaik_thesaurus_api/__manifest__.py @@ -0,0 +1,26 @@ +# Copyright 2021 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "Mozaik Thesaurus Rest", + "summary": """ + Add a REST API to manage thesaurus""", + "version": "14.0.1.0.0", + "license": "AGPL-3", + "author": "ACSONE SA/NV", + "website": "https://github.com/mozaik-association/mozaik", + "depends": [ + "base_rest", + "base_rest_pydantic", + "mozaik_thesaurus", + "extendable", + "pydantic", + ], + "data": [], + "external_dependencies": { + "python": [ + "extendable_pydantic<1", + "pydantic<2", + ] + }, +} diff --git a/mozaik_thesaurus_api/pydantic_models/__init__.py b/mozaik_thesaurus_api/pydantic_models/__init__.py new file mode 100644 index 000000000..1c9a28856 --- /dev/null +++ b/mozaik_thesaurus_api/pydantic_models/__init__.py @@ -0,0 +1,2 @@ +from . import thesaurus_term_info +from . import thesaurus_term_search_filter diff --git a/mozaik_thesaurus_api/pydantic_models/thesaurus_term_info.py b/mozaik_thesaurus_api/pydantic_models/thesaurus_term_info.py new file mode 100644 index 000000000..8199e1710 --- /dev/null +++ b/mozaik_thesaurus_api/pydantic_models/thesaurus_term_info.py @@ -0,0 +1,21 @@ +# Copyright 2021 ACSONE SA/NV +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +from datetime import datetime + +from extendable_pydantic import ExtendableModelMeta +from pydantic import BaseModel + +from odoo.addons.pydantic import utils + + +class ThesaurusTermInfo(BaseModel, metaclass=ExtendableModelMeta): + id: int + name: str + active: bool + main_term: bool = None + write_date: datetime + + class Config: + orm_mode = True + getter_dict = utils.GenericOdooGetter diff --git a/mozaik_thesaurus_api/pydantic_models/thesaurus_term_search_filter.py b/mozaik_thesaurus_api/pydantic_models/thesaurus_term_search_filter.py new file mode 100644 index 000000000..9856b2753 --- /dev/null +++ b/mozaik_thesaurus_api/pydantic_models/thesaurus_term_search_filter.py @@ -0,0 +1,13 @@ +# Copyright 2021 ACSONE SA/NV +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +from extendable_pydantic import ExtendableModelMeta +from pydantic import BaseModel + + +class ThesaurusTermSearchFilter(BaseModel, metaclass=ExtendableModelMeta): + + id: int = None + name: str = None + active: bool = None + main_term: bool = None diff --git a/mozaik_thesaurus_api/services/__init__.py b/mozaik_thesaurus_api/services/__init__.py new file mode 100644 index 000000000..3f95e090a --- /dev/null +++ b/mozaik_thesaurus_api/services/__init__.py @@ -0,0 +1,2 @@ +from . import service +from . import thesaurus_term diff --git a/mozaik_thesaurus_api/services/service.py b/mozaik_thesaurus_api/services/service.py new file mode 100644 index 000000000..d47888393 --- /dev/null +++ b/mozaik_thesaurus_api/services/service.py @@ -0,0 +1,24 @@ +# Copyright 2021 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import _ +from odoo.exceptions import MissingError + +from odoo.addons.component.core import AbstractComponent + + +class BaseThesaurusService(AbstractComponent): + _inherit = "base.rest.service" + _name = "base.thesaurus.rest.service" + _collection = "thesaurus.rest.services" + _expose_model = None + + def _get(self, _id): + domain = [("id", "=", _id)] + record = self.env[self._expose_model].search(domain) + if not record: + raise MissingError( + _("The record %s %s does not exist") % (self._expose_model, _id) + ) + else: + return record diff --git a/mozaik_thesaurus_api/services/thesaurus_term.py b/mozaik_thesaurus_api/services/thesaurus_term.py new file mode 100644 index 000000000..fcf87f519 --- /dev/null +++ b/mozaik_thesaurus_api/services/thesaurus_term.py @@ -0,0 +1,55 @@ +# Copyright 2021 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from typing import List + +from odoo.addons.base_rest import restapi +from odoo.addons.base_rest_pydantic.restapi import PydanticModel, PydanticModelList +from odoo.addons.component.core import Component + +from ..pydantic_models.thesaurus_term_info import ThesaurusTermInfo +from ..pydantic_models.thesaurus_term_search_filter import ThesaurusTermSearchFilter + + +class ThesaurusTermService(Component): + _inherit = "base.thesaurus.rest.service" + _name = "thesaurus.term.rest.service" + _usage = "thesaurus_term" + _expose_model = "thesaurus.term" + _description = __doc__ + + @restapi.method( + routes=[(["/"], "GET")], + output_param=PydanticModel(ThesaurusTermInfo), + ) + def get(self, _id: int) -> ThesaurusTermInfo: + thesaurus_term = self._get(_id) + return ThesaurusTermInfo.from_orm(thesaurus_term) + + def _get_search_domain(self, filters): + domain = [] + if filters.name: + domain.append(("name", "like", filters.name)) + if filters.id: + domain.append(("id", "=", filters.id)) + if filters.active is not None: + domain.append(("active", "=", filters.active)) + else: + domain.extend(["|", ("active", "=", True), ("active", "=", False)]) + if filters.main_term is not None: + domain.append(("main_term", "=", filters.main_term)) + return domain + + @restapi.method( + routes=[(["/", "/search"], "GET")], + input_param=PydanticModel(ThesaurusTermSearchFilter), + output_param=PydanticModelList(ThesaurusTermInfo), + ) + def search( + self, thesaurus_term_search_filter: ThesaurusTermSearchFilter + ) -> List[ThesaurusTermInfo]: + domain = self._get_search_domain(thesaurus_term_search_filter) + res: List[ThesaurusTermInfo] = [] + for e in self.env["thesaurus.term"].sudo().search(domain): + res.append(ThesaurusTermInfo.from_orm(e)) + return res diff --git a/requirements.txt b/requirements.txt index 24a5abe94..985f27b4c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,7 @@ # generated from manifests external_dependencies +extendable_pydantic<1 freezegun graphene openupgradelib phonenumbers +pydantic<2 diff --git a/setup/mozaik_thesaurus_api/odoo/addons/mozaik_thesaurus_api b/setup/mozaik_thesaurus_api/odoo/addons/mozaik_thesaurus_api new file mode 120000 index 000000000..bdd0d45bf --- /dev/null +++ b/setup/mozaik_thesaurus_api/odoo/addons/mozaik_thesaurus_api @@ -0,0 +1 @@ +../../../../mozaik_thesaurus_api \ No newline at end of file diff --git a/setup/mozaik_thesaurus_api/setup.py b/setup/mozaik_thesaurus_api/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/mozaik_thesaurus_api/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)