Skip to content

Commit

Permalink
[ADD] mozaik_thesaurus_api
Browse files Browse the repository at this point in the history
  • Loading branch information
marielejeune committed Nov 20, 2024
1 parent 5ce51ce commit f6124b9
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 0 deletions.
2 changes: 2 additions & 0 deletions mozaik_thesaurus_api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import pydantic_models
from . import services
26 changes: 26 additions & 0 deletions mozaik_thesaurus_api/__manifest__.py
Original file line number Diff line number Diff line change
@@ -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",
]
},
}
2 changes: 2 additions & 0 deletions mozaik_thesaurus_api/pydantic_models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import thesaurus_term_info
from . import thesaurus_term_search_filter
21 changes: 21 additions & 0 deletions mozaik_thesaurus_api/pydantic_models/thesaurus_term_info.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions mozaik_thesaurus_api/services/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import service
from . import thesaurus_term
24 changes: 24 additions & 0 deletions mozaik_thesaurus_api/services/service.py
Original file line number Diff line number Diff line change
@@ -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
55 changes: 55 additions & 0 deletions mozaik_thesaurus_api/services/thesaurus_term.py
Original file line number Diff line number Diff line change
@@ -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=[(["/<int:_id>"], "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
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# generated from manifests external_dependencies
extendable_pydantic<1
freezegun
graphene
openupgradelib
phonenumbers
pydantic<2
6 changes: 6 additions & 0 deletions setup/mozaik_thesaurus_api/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit f6124b9

Please sign in to comment.