Skip to content

Commit

Permalink
[14.0][FIX] shopinvader_membership subscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
acsonefho committed Mar 22, 2022
1 parent 05bf9c9 commit b831ff8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
35 changes: 30 additions & 5 deletions shopinvader_membership/services/membership.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -41,21 +46,41 @@ def search(self, **params):
"""
return self._paginate_search(**params)

def subscribe(self, _id):
@restapi.method(
[(["/<int:_id>/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
Expand All @@ -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):
"""
Expand Down
6 changes: 3 additions & 3 deletions shopinvader_membership/tests/test_membership_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b831ff8

Please sign in to comment.