diff --git a/shopinvader_membership/services/membership.py b/shopinvader_membership/services/membership.py index c993e48121..20423fec1b 100644 --- a/shopinvader_membership/services/membership.py +++ b/shopinvader_membership/services/membership.py @@ -1,13 +1,18 @@ # Copyright 2020 ACSONE SA/NV # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +import logging + from odoo.exceptions import UserError from odoo.osv import expression from odoo.tools.translate import _ +from odoo.addons.base_rest import restapi from odoo.addons.base_rest.components.service import to_int from odoo.addons.component.core import Component +_logger = logging.getLogger(__name__) + class MembershipService(Component): _inherit = "base.shopinvader.service" @@ -41,21 +46,41 @@ def search(self, **params): """ return self._paginate_search(**params) - def subscribe(self, _id): + @restapi.method( + [(["//subscribe"], "GET")], + input_param=restapi.CerberusValidator({}), + output_param=restapi.CerberusValidator("_validator_return_subscribe"), + ) + def get_subscribe(self, _id): """ + DEPRECATED: you should use `subscribe` with a POST. Subscribe to a membership product with logged user :param _id: id of product.product :return: dict with invoice_id """ + _logger.warning("DEPRECATED: You should use `subscribe` with a POST") + return self.post_subscribe(**{"membership_product_id": _id}) + + @restapi.method( + [(["/subscribe"], "POST")], + input_param=restapi.CerberusValidator("_validator_subscribe"), + output_param=restapi.CerberusValidator("_validator_return_subscribe"), + ) + def post_subscribe(self, **params): + """ + Subscribe to a membership product with logged user + :return: dict with invoice_id + """ + product_id = params.get("membership_product_id") if not self._is_logged_in(): raise UserError(_("A user should be logged")) membership_product = self.env["product.product"].search( - [("id", "=", _id), ("membership", "=", True)] + [("id", "=", product_id), ("membership", "=", True)], limit=1 ) if not membership_product: - raise UserError(_("No membership product found with id %s") % _id) + raise UserError(_("No membership product found with id %s") % product_id) wizard = self.env["membership.invoice"].create( - {"product_id": _id, "member_price": membership_product.list_price} + {"product_id": product_id, "member_price": membership_product.list_price} ) invoices_views_dict = wizard.with_context( active_ids=self.partner.ids @@ -67,7 +92,7 @@ def _validator_subscribe(self): Validator for the subscribe :return: dict """ - return {"membership_product_id": {"type": "integer"}} + return {"membership_product_id": {"type": "integer", "required": True}} def _validator_return_subscribe(self): """ diff --git a/shopinvader_membership/tests/test_membership_service.py b/shopinvader_membership/tests/test_membership_service.py index 64491839d9..7db512335d 100644 --- a/shopinvader_membership/tests/test_membership_service.py +++ b/shopinvader_membership/tests/test_membership_service.py @@ -144,16 +144,16 @@ def test_get_multi_membership_lines(self): def test_subscription(self): # Check first not logged with self.assertRaises(UserError) as e: - self.service_guest.dispatch("subscribe", self.product.id) + self.service_guest.dispatch("get_subscribe", self.product.id) self.assertEqual(e.exception.args[0], "A user should be logged") # Then with a logged user but with a non membership product self.product.write({"membership": False}) with self.assertRaises(UserError) as e: - self.service.dispatch("subscribe", self.product.id) + self.service.dispatch("get_subscribe", self.product.id) self.assertIn("No membership product found with", e.exception.args[0]) # Then user logged and real membership product self.product.write({"membership": True}) - result = self.service.dispatch("subscribe", self.product.id) + result = self.service.dispatch("get_subscribe", self.product.id) invoice_id = result.get("invoice_id") invoice = self.env["account.move"].browse(invoice_id) self.assertEqual(self.partner, invoice.partner_id)