Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0][FIX] shopinvader_membership subscribe #1207

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last thing: I'd rather avoid using the wizard and call res.partner.create_membership_invoice() instead. The code will be easier to understand and the wizard will stay where it needs to stay (i.e. in the UI realm and not the business logic realm).

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