Skip to content

Commit

Permalink
[ADD] shopinvader_auth_jwt_transfer_cart
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxxxzero committed Mar 4, 2024
1 parent b749f3c commit 5b4f289
Show file tree
Hide file tree
Showing 16 changed files with 914 additions and 0 deletions.
6 changes: 6 additions & 0 deletions setup/shopinvader_auth_jwt_transfer_cart/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
77 changes: 77 additions & 0 deletions shopinvader_auth_jwt_transfer_cart/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
===========================================
Shopinvader JWT Authentication Tranfer Cart
===========================================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! 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%2Fshopinvader-lightgray.png?logo=github
:target: https://github.com/OCA/shopinvader/tree/14.0/shopinvader_auth_jwt_transfer_cart
:alt: OCA/shopinvader
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/shopinvader-14-0/shopinvader-14-0-shopinvader_auth_jwt_transfer_cart
:alt: Translate me on Weblate

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

This module adds a new transfer route to cart service that takes a JWT token as POST parameter and transfer the cart from the logged in partner to the partner in the given jwt token.

**Table of contents**

.. contents::
:local:

Usage
=====

Activation is automatic.

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

Bugs are tracked on `GitHub Issues <https://github.com/OCA/shopinvader/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/shopinvader/issues/new?body=module:%20shopinvader_auth_jwt_transfer_cart%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
~~~~~~~

* Akretion

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

* `Akretion <https://www.akretion.com>`_:

* Florian Mounier

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/shopinvader <https://github.com/OCA/shopinvader/tree/14.0/shopinvader_auth_jwt_transfer_cart>`_ 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 shopinvader_auth_jwt_transfer_cart/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from . import services
12 changes: 12 additions & 0 deletions shopinvader_auth_jwt_transfer_cart/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "Shopinvader JWT Authentication Tranfer Cart",
"version": "14.0.1.0.0",
"summary": "Add a cart route to transfer cart from a partner to another",
"website": "https://github.com/shopinvader/odoo-shopinvader",
"depends": ["shopinvader_auth_jwt"],
"data": [
"views/shopinvader_backend.xml",
],
"author": "Akretion, Odoo Community Association (OCA)",
"license": "AGPL-3",
}
1 change: 1 addition & 0 deletions shopinvader_auth_jwt_transfer_cart/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import shopinvader_backend
11 changes: 11 additions & 0 deletions shopinvader_auth_jwt_transfer_cart/models/shopinvader_backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from odoo import fields, models


class ShopinvaderBackend(models.Model):
_inherit = "shopinvader.backend"

merge_cart_on_transfer = fields.Boolean(
string="Merge Cart on Transfer",
help="If checked, the cart will be merged with the existing one if "
"any on transfer.",
)
3 changes: 3 additions & 0 deletions shopinvader_auth_jwt_transfer_cart/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* `Akretion <https://www.akretion.com>`_:

* Florian Mounier
1 change: 1 addition & 0 deletions shopinvader_auth_jwt_transfer_cart/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module adds a new transfer route to cart service that takes a JWT token as POST parameter and transfer the cart from the logged in partner to the partner in the given jwt token.
1 change: 1 addition & 0 deletions shopinvader_auth_jwt_transfer_cart/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Activation is automatic.
1 change: 1 addition & 0 deletions shopinvader_auth_jwt_transfer_cart/services/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import cart
67 changes: 67 additions & 0 deletions shopinvader_auth_jwt_transfer_cart/services/cart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from odoo import _, fields
from odoo.exceptions import AccessDenied

from odoo.addons.component.core import Component


class CartService(Component):
_inherit = "shopinvader.cart.service"

def _decode_token(self, token):
validator = self.env["auth.jwt.validator"]._get_validator_by_name(
validator_name="shopinvader"
)
while validator:
try:
return validator._decode(token)
except Exception:
validator = validator.next_validator_id

def _validator_transfer(self):
return {
"token": {"type": "string", "required": True},
}

def transfer(self, token=None):
cart = self._get()
auth_token = self._decode_token(token)
if not auth_token or not auth_token.get("email"):
raise AccessDenied(_("Invalid new auth token"))

partner = self.env["shopinvader.partner"].search(
[("partner_email", "=", auth_token["email"])]
)

if len(partner) != 1:
raise AccessDenied(_("Invalid partner email in token"))

old_cart = None
if self.shopinvader_backend.merge_cart_on_transfer:
old_partner = self.work.partner
self.work.partner = partner.record_id
old_cart = self._get(False)
self.work.partner = old_partner

# Change cart partner:
res_partner_id = partner.record_id.id
cart.date_order = fields.Datetime.now()
cart.write_with_onchange(
{
"partner_id": res_partner_id,
"partner_shipping_id": res_partner_id,
"partner_invoice_id": res_partner_id,
}
)

if old_cart and self.shopinvader_backend.merge_cart_on_transfer:
# Merge cart:
for line in old_cart.order_line:
self._add_item(
cart,
{
"product_id": line.product_id.id,
"item_qty": line.product_uom_qty,
},
)

return self._to_json(cart)
Loading

0 comments on commit 5b4f289

Please sign in to comment.