Skip to content

Commit

Permalink
[PORT] Port partner_rest_api from OCA to mozaik
Browse files Browse the repository at this point in the history
  • Loading branch information
qgroulard committed Jul 24, 2023
1 parent 13a8228 commit d219ae1
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 16 deletions.
1 change: 1 addition & 0 deletions mozaik_country_rest/pydantic_models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from . import country_info
from . import country_search_filter
from . import country_state_info
24 changes: 24 additions & 0 deletions mozaik_country_rest/pydantic_models/country_state_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2022 ACSONE SA/NV
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from datetime import datetime

import pydantic
from extendable_pydantic import ExtendableModelMeta
from pydantic import BaseModel

from odoo.addons.pydantic import utils

from .country_info import CountryInfo


class CountryStateInfo(BaseModel, metaclass=ExtendableModelMeta):
id: int
name: str
code: str
country: CountryInfo = pydantic.Field(..., alias="country_id")
write_date: datetime

class Config:
orm_mode = True
getter_dict = utils.GenericOdooGetter
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from odoo.addons.mozaik_thesaurus_api.pydantic_models.thesaurus_term_info import (
ThesaurusTermInfo,
)
from odoo.addons.partner_rest_api.pydantic_models.partner_info import PartnerInfo
from odoo.addons.mozaik_partner_rest.pydantic_models.partner_info import PartnerInfo
from odoo.addons.pydantic import utils


Expand Down
5 changes: 4 additions & 1 deletion mozaik_partner_rest/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
"author": "ACSONE SA/NV",
"website": "https://github.com/OCA/mozaik",
"depends": [
"partner_rest_api",
"base_rest",
"base_rest_pydantic",
"extendable",
"mozaik_address",
"mozaik_country_rest",
"mozaik_involvement_rest",
"mozaik_membership",
"mozaik_membership_rest",
Expand Down
1 change: 1 addition & 0 deletions mozaik_partner_rest/pydantic_models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from . import co_residency_info
from . import partner_info_update
from . import partner_info
from . import partner_search_filter
2 changes: 1 addition & 1 deletion mozaik_partner_rest/pydantic_models/address_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from extendable_pydantic import ExtendableModelMeta
from pydantic import BaseModel

from odoo.addons.partner_rest_api.pydantic_models.country import CountryInfo
from odoo.addons.mozaik_country_rest.pydantic_models.country_info import CountryInfo
from odoo.addons.pydantic import utils


Expand Down
34 changes: 27 additions & 7 deletions mozaik_partner_rest/pydantic_models/partner_info.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# Copyright 2022 ACSONE SA/NV
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from datetime import date
from datetime import date, datetime
from typing import List

import pydantic
from extendable_pydantic import ExtendableModelMeta
from pydantic import BaseModel

from odoo.addons.mozaik_country_rest.pydantic_models.country_info import CountryInfo
from odoo.addons.mozaik_country_rest.pydantic_models.country_state_info import (
CountryStateInfo,
)
from odoo.addons.mozaik_involvement_rest.pydantic_models.involvement_info import (
InvolvementInfo,
)
Expand All @@ -15,22 +21,36 @@
from odoo.addons.mozaik_membership_rest.pydantic_models.subscription_info import (
SubscriptionInfo,
)
from odoo.addons.partner_rest_api.pydantic_models.partner_info import (
PartnerInfo as BasePartnerInfo,
PartnerShortInfo as BasePartnerShortInfo,
)
from odoo.addons.pydantic import utils

from .address_info import AddressInfo
from .co_residency_info import CoResidencyInfo


class PartnerShortInfo(BasePartnerShortInfo, extends=BasePartnerShortInfo):
class PartnerShortInfo(BaseModel, metaclass=ExtendableModelMeta):
id: int
name: str
email: str = None
street: str = None
street2: str = None
city: str = None
zip: str = None
state: CountryStateInfo = pydantic.Field(None, alias="state_id")
country: CountryInfo = pydantic.Field(None, alias="country_id")
phone: str = None
mobile: str = None
ref: str = None
write_date: datetime
city_id: int = None
address: str = None
address_addres: AddressInfo = pydantic.Field(None, alias="address_address_id")

class Config:
orm_mode = True
getter_dict = utils.GenericOdooGetter


class PartnerInfo(BasePartnerInfo, extends=BasePartnerInfo):
class PartnerInfo(PartnerShortInfo):
firstname: str = None
lastname: str = None
birthdate: date = pydantic.Field(None, alias="birthdate_date")
Expand Down
14 changes: 10 additions & 4 deletions mozaik_partner_rest/pydantic_models/partner_info_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
from datetime import date
from typing import List

from odoo.addons.partner_rest_api.pydantic_models.partner_info_update import (
PartnerInfoUpdate as BasePartnerInfoUpdate,
)
from extendable_pydantic import ExtendableModelMeta
from pydantic import BaseModel


class PartnerInfoUpdate(BasePartnerInfoUpdate, extends=BasePartnerInfoUpdate):
class PartnerInfoUpdate(BaseModel, metaclass=ExtendableModelMeta):

email: str = None
city: str = None
zip: str = None
state_id: int = None
phone: str = None
mobile: str = None
firstname: str = None
lastname: str = None
birthdate_date: date = None
Expand Down
12 changes: 12 additions & 0 deletions mozaik_partner_rest/pydantic_models/partner_search_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright 2022 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 PartnerSearchFilter(BaseModel, metaclass=ExtendableModelMeta):

id: int = None
name: str = None
ref: str = None
1 change: 1 addition & 0 deletions mozaik_partner_rest/services/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import service
from . import partner
57 changes: 56 additions & 1 deletion mozaik_partner_rest/services/partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,57 @@
# flake8: noqa

from collections import OrderedDict
from typing import List

from odoo import _
from odoo.exceptions import ValidationError

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.partner_info import PartnerInfo, PartnerShortInfo
from ..pydantic_models.partner_info_update import PartnerInfoUpdate
from ..pydantic_models.partner_search_filter import PartnerSearchFilter


class PartnerService(Component):
_inherit = "partner.rest.service"
_inherit = "base.partner.rest.service"
_name = "partner.rest.service"
_usage = "partner"
_expose_model = "res.partner"
_description = __doc__

@restapi.method(
routes=[(["/<int:_id>"], "GET")], output_param=PydanticModel(PartnerInfo)
)
def get(self, _id: int) -> PartnerInfo:
partner = self._get(_id)
return PartnerInfo.from_orm(partner)

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.ref:
domain.append(("ref", "like", filters.ref))
return domain

@restapi.method(
routes=[(["/", "/search"], "GET")],
input_param=PydanticModel(PartnerSearchFilter),
output_param=PydanticModelList(PartnerShortInfo),
)
def search(
self, partner_search_filter: PartnerSearchFilter
) -> List[PartnerShortInfo]:
domain = self._get_search_domain(partner_search_filter)
res: List[PartnerShortInfo] = []
for e in self.env["res.partner"].sudo().search(domain):
res.append(PartnerShortInfo.from_orm(e))
return res

def _prepare_address_fields(self, values, partner):
street_man = (
Expand Down Expand Up @@ -151,3 +193,16 @@ def _prepare_update_values(self, values, partner):
}
).doit()
return res

@restapi.method(
routes=[(["/<int:_id>/update"], "POST")],
input_param=PydanticModel(PartnerInfoUpdate),
output_param=PydanticModel(PartnerInfo),
)
def update(self, _id: int, partner_info_update: PartnerInfoUpdate) -> PartnerInfo:
partner = self._get(_id)
values = self._prepare_update_values(
partner_info_update.dict(exclude_unset=True), partner
)
partner.write(values)
return PartnerInfo.from_orm(partner)
24 changes: 24 additions & 0 deletions mozaik_partner_rest/services/service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2022 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 BasePartnerService(AbstractComponent):
_inherit = "base.rest.service"
_name = "base.partner.rest.service"
_collection = "partner.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
2 changes: 1 addition & 1 deletion mozaik_sponsorship_rest/pydantic_models/partner_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import pydantic

from odoo.addons.partner_rest_api.pydantic_models.partner_info import (
from odoo.addons.mozaik_partner_rest.pydantic_models.partner_info import (
PartnerInfo as BasePartnerInfo,
PartnerShortInfo as BasePartnerShortInfo,
)
Expand Down

0 comments on commit d219ae1

Please sign in to comment.