Skip to content

Commit

Permalink
WIP: [ADD] event_rest_api
Browse files Browse the repository at this point in the history
  • Loading branch information
qgroulard committed Dec 9, 2021
1 parent 095d604 commit 6e43f8c
Show file tree
Hide file tree
Showing 29 changed files with 985 additions and 0 deletions.
78 changes: 78 additions & 0 deletions event_rest_api/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
==============
Event Rest Api
==============

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |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-OCA%2Fevent-lightgray.png?logo=github
:target: https://github.com/OCA/event/tree/14.0/event_rest_api
:alt: OCA/event
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/event-14-0/event-14-0-event_rest_api
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/199/14.0
:alt: Try me on Runbot

|badge1| |badge2| |badge3| |badge4| |badge5|

TODO

**Table of contents**

.. contents::
:local:

Usage
=====

TODO

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/event/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/event/issues/new?body=module:%20event_rest_api%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

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

Credits
=======

Authors
~~~~~~~

* ACSONE SA/NV

Contributors
~~~~~~~~~~~~

* Quentin Groulard <[email protected]>

Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/event <https://github.com/OCA/event/tree/14.0/event_rest_api>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 2 additions & 0 deletions event_rest_api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import pydantic_models
from . import services
18 changes: 18 additions & 0 deletions event_rest_api/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2021 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"name": "Event Rest Api",
"summary": """Add a REST API to manage events""",
"version": "14.0.1.0.0",
"license": "AGPL-3",
"author": "ACSONE SA/NV,Odoo Community Association (OCA)",
"website": "https://github.com/OCA/event",
"depends": ["base_rest", "base_rest_pydantic", "event"],
"data": [],
"external_dependencies": {
"python": [
"pydantic",
]
},
}
9 changes: 9 additions & 0 deletions event_rest_api/pydantic_models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from . import event_info
from . import event_registration_info
from . import event_registration_request
from . import event_search_filter
from . import event_stage_info
from . import event_stage_search_filter
from . import event_ticket_info
from . import event_type_info
from . import event_type_search_filter
27 changes: 27 additions & 0 deletions event_rest_api/pydantic_models/event_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2021 ACSONE SA/NV
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from datetime import datetime
from typing import List

import pydantic

from odoo.addons.pydantic import models, utils

from .event_stage_info import EventStageInfo
from .event_ticket_info import EventTicketInfo
from .event_type_info import EventTypeInfo


class EventInfo(models.BaseModel):
id: int
name: str
date_begin: datetime
date_end: datetime
event_tickets: List[EventTicketInfo] = pydantic.Field(..., alias="event_ticket_ids")
event_type: EventTypeInfo = pydantic.Field(..., alias="event_type_id")
stage: EventStageInfo = pydantic.Field(..., alias="stage_id")

class Config:
orm_mode = True
getter_dict = utils.GenericOdooGetter
23 changes: 23 additions & 0 deletions event_rest_api/pydantic_models/event_registration_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2021 ACSONE SA/NV
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

import pydantic

from odoo.addons.pydantic import models, utils

from .event_info import EventInfo
from .event_ticket_info import EventTicketInfo


class EventRegistrationInfo(models.BaseModel):
id: int
partner_id: int = None
firstname: str = None
lastname: str = None
email: str = None
event: EventInfo = pydantic.Field(..., alias="event_id")
event_ticket: EventTicketInfo = pydantic.Field(..., alias="event_ticket_id")

class Config:
orm_mode = True
getter_dict = utils.GenericOdooGetter
20 changes: 20 additions & 0 deletions event_rest_api/pydantic_models/event_registration_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2021 ACSONE SA/NV
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from typing import List

from odoo.addons.pydantic import models


class EventRegistrationRequest(models.BaseModel):

firstname: str
lastname: str
email: str
phone: str = None
event_ticket_id: int = None


class EventRegistrationRequestList(models.BaseModel):

event_registration_requests: List[EventRegistrationRequest] = []
17 changes: 17 additions & 0 deletions event_rest_api/pydantic_models/event_search_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2021 ACSONE SA/NV
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from datetime import datetime
from typing import List

from odoo.addons.pydantic import models


class EventSearchFilter(models.BaseModel):

id: int = None
name: str = None
start_after: datetime = None
end_before: datetime = None
event_type_ids: List[int] = None
stage_ids: List[int] = None
15 changes: 15 additions & 0 deletions event_rest_api/pydantic_models/event_stage_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2021 ACSONE SA/NV
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from odoo.addons.pydantic import models, utils


class EventStageInfo(models.BaseModel):
id: int
name: str
sequence: int = None
pipe_end: bool = None

class Config:
orm_mode = True
getter_dict = utils.GenericOdooGetter
11 changes: 11 additions & 0 deletions event_rest_api/pydantic_models/event_stage_search_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright 2021 ACSONE SA/NV
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from odoo.addons.pydantic import models


class EventStageSearchFilter(models.BaseModel):

id: int = None
name: str = None
pipe_end: bool = None
20 changes: 20 additions & 0 deletions event_rest_api/pydantic_models/event_ticket_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2021 ACSONE SA/NV
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from datetime import date

from odoo.addons.pydantic import models, utils


class EventTicketInfo(models.BaseModel):
id: int
event_id: int
name: str
description: str = None
start_sale_date: date = None
end_sale_date: date = None
seats_available: int = None

class Config:
orm_mode = True
getter_dict = utils.GenericOdooGetter
13 changes: 13 additions & 0 deletions event_rest_api/pydantic_models/event_type_info.py
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 odoo.addons.pydantic import models, utils


class EventTypeInfo(models.BaseModel):
id: int
name: str

class Config:
orm_mode = True
getter_dict = utils.GenericOdooGetter
10 changes: 10 additions & 0 deletions event_rest_api/pydantic_models/event_type_search_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright 2021 ACSONE SA/NV
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from odoo.addons.pydantic import models


class EventTypeSearchFilter(models.BaseModel):

id: int = None
name: str = None
1 change: 1 addition & 0 deletions event_rest_api/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Quentin Groulard <[email protected]>
1 change: 1 addition & 0 deletions event_rest_api/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TODO
1 change: 1 addition & 0 deletions event_rest_api/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TODO
4 changes: 4 additions & 0 deletions event_rest_api/services/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from . import service
from . import event
from . import event_stage
from . import event_type
109 changes: 109 additions & 0 deletions event_rest_api/services/event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Copyright 2021 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

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.event_info import EventInfo
from ..pydantic_models.event_registration_info import EventRegistrationInfo
from ..pydantic_models.event_registration_request import (
EventRegistrationRequest,
EventRegistrationRequestList,
)
from ..pydantic_models.event_search_filter import EventSearchFilter


class EventService(Component):
_inherit = "base.event.rest.service"
_name = "event.rest.service"
_usage = "event"
_expose_model = "event.event"
_description = __doc__

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

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.start_after:
domain.append(("date_begin", ">", filters.start_after))
if filters.end_before:
domain.append(("date_end", "<", filters.end_before))
if filters.event_type_ids:
domain.append(("event_type_id", "in", filters.event_type_ids))
if filters.stage_ids:
domain.append(("stage_id", "in", filters.stage_ids))
return domain

@restapi.method(
routes=[(["/", "/search"], "GET")],
input_param=PydanticModel(EventSearchFilter),
output_param=PydanticModelList(EventInfo),
auth="public",
)
def search(self, event_search_filter: EventSearchFilter) -> List[EventInfo]:
domain = self._get_search_domain(event_search_filter)
res: List[EventInfo] = []
for e in self.env["event.event"].sudo().search(domain):
res.append(EventInfo.from_orm(e))
return res

def _prepare_event_registration_values(
self, event, event_registration_request: EventRegistrationRequest
) -> dict:
return {
"event_id": event.id,
"partner_id": self.env.context.get("authenticated_partner_id", False),
"firstname": event_registration_request.firstname,
"lastname": event_registration_request.lastname,
"email": event_registration_request.email,
"phone": event_registration_request.phone,
"event_ticket_id": event_registration_request.event_ticket_id,
}

@restapi.method(
routes=[(["/<int:_id>/registration"], "POST")],
input_param=PydanticModel(EventRegistrationRequestList),
output_param=PydanticModelList(EventRegistrationInfo),
auth="public_or_default",
)
def registration(
self, _id: int, event_registration_request_list: EventRegistrationRequestList
) -> List[EventRegistrationInfo]:
event = self._get(_id)
if event.seats_limited:
ordered_seats = len(
event_registration_request_list.event_registration_requests
)
if event.seats_available < ordered_seats:
raise ValidationError(
_("Not enough seats available: %s") % (event.seats_available)
)
res: List[EventRegistrationInfo] = []
for (
event_registration_request
) in event_registration_request_list.event_registration_requests:
event_registration_values = self._prepare_event_registration_values(
event, event_registration_request
)
event_registration = self.env["event.registration"].create(
event_registration_values
)
res.append(EventRegistrationInfo.from_orm(event_registration))
return res
Loading

0 comments on commit 6e43f8c

Please sign in to comment.