-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5ce51ce
commit f6124b9
Showing
11 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from . import pydantic_models | ||
from . import services |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
] | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
mozaik_thesaurus_api/pydantic_models/thesaurus_term_info.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
13 changes: 13 additions & 0 deletions
13
mozaik_thesaurus_api/pydantic_models/thesaurus_term_search_filter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from . import service | ||
from . import thesaurus_term |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../mozaik_thesaurus_api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |