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

[10.0][CHG] being able to make the difference between an update done by the customer and update done internally in Odoo #596

Merged
merged 1 commit into from
Mar 23, 2020
Merged
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
1 change: 1 addition & 0 deletions shopinvader/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from . import seo_title_mixin
from . import track_external_mixin
from . import product_filter
from . import product_product
from . import product_template
Expand Down
3 changes: 2 additions & 1 deletion shopinvader/models/sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class ShopinvaderCartStep(models.Model):


class SaleOrder(models.Model):
_inherit = "sale.order"
_name = "sale.order"
_inherit = ["sale.order", "track.external.mixin"]

typology = fields.Selection(
[("sale", "Sale"), ("cart", "Cart")], default="sale"
Expand Down
26 changes: 26 additions & 0 deletions shopinvader/models/track_external_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
# Copyright 2020 ACSONE SA/NV (<http://acsone.eu>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import api, fields, models


class TrackExternalMixin(models.AbstractModel):
_name = "track.external.mixin"
_description = "Track external update"

last_external_update_date = fields.Datetime()

def is_rest_request(self):
return self.env.context.get("shopinvader_request", False)

@api.multi
def _write(self, vals):
if self.is_rest_request():
vals["last_external_update_date"] = fields.Datetime.now()
return super(TrackExternalMixin, self)._write(vals)

@api.model
def create(self, vals):
if self.is_rest_request():
vals["last_external_update_date"] = fields.Datetime.now()
return super(TrackExternalMixin, self).create(vals)
7 changes: 7 additions & 0 deletions shopinvader/services/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from odoo.addons.component.core import AbstractComponent
from odoo.exceptions import MissingError, UserError
from odoo.osv import expression
from odoo.tools import frozendict

from .. import shopinvader_response

Expand All @@ -19,6 +20,12 @@ class BaseShopinvaderService(AbstractComponent):
_collection = "shopinvader.backend"
_expose_model = None

@property
def env(self):
env = self.work.env
env.context = frozendict(dict(env.context, shopinvader_request=True))
return env

@property
def partner(self):
return self.work.partner
Expand Down
12 changes: 12 additions & 0 deletions shopinvader/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
from odoo.addons.component.tests.common import ComponentMixin
from odoo.addons.queue_job.job import Job
from odoo.addons.server_environment import serv_config
from odoo.addons.shopinvader.models.track_external_mixin import (
TrackExternalMixin,
)
from odoo.exceptions import MissingError
from odoo.tests import SavepointCase

Expand Down Expand Up @@ -89,6 +92,15 @@ def _get_selection_label(self, record, field):
record[field], record
)

def _get_last_external_update_date(self, record):
if isinstance(record, TrackExternalMixin):
return record.last_external_update_date
return False

def _check_last_external_update_date(self, record, previous_date):
if isinstance(record, TrackExternalMixin):
self.assertTrue(record.last_external_update_date > previous_date)


class ProductCommonCase(CommonCase):
def setUp(self):
Expand Down
10 changes: 8 additions & 2 deletions shopinvader/tests/test_cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ def _sign_with(self, partner):
def test_anonymous_cart_then_sign(self):
cart = self.cart
partner = self.env.ref("shopinvader.partner_1")
last_external_update_date = self._get_last_external_update_date(cart)
self._sign_with(partner)
self._check_last_external_update_date(cart, last_external_update_date)
self.assertEqual(cart.partner_id, partner)
self.assertEqual(cart.partner_shipping_id, partner)
self.assertEqual(cart.partner_invoice_id, partner)
Expand Down Expand Up @@ -450,21 +452,25 @@ def test_cart_line_lang_logged(self):

class ConnectedCartCase(CommonConnectedCartCase, CartClearTest):
def test_set_shipping_address(self):
cart = self.cart
last_external_update_date = self._get_last_external_update_date(cart)
self.service.dispatch(
"update", params={"shipping": {"address": {"id": self.address.id}}}
)
cart = self.cart
self._check_last_external_update_date(cart, last_external_update_date)
self.assertEqual(cart.partner_id, self.partner)
self.assertEqual(cart.partner_shipping_id, self.address)
self.assertEqual(cart.partner_invoice_id, self.address)

def test_set_invoice_address(self):
cart = self.cart
last_external_update_date = self._get_last_external_update_date(cart)
self.service.dispatch(
"update",
params={"invoicing": {"address": {"id": self.address.id}}},
)
self._check_last_external_update_date(cart, last_external_update_date)

cart = self.cart
self.assertEqual(cart.partner_id, self.partner)
self.assertEqual(cart.partner_shipping_id, self.partner)
self.assertEqual(cart.partner_invoice_id, self.address)
Expand Down
32 changes: 32 additions & 0 deletions shopinvader/tests/test_cart_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# @author Sébastien BEAU <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import time

from odoo.tools import float_round

from .common import CommonCase
Expand Down Expand Up @@ -249,6 +251,36 @@ def test_pricelist_product_price_unit_with_discount(self):
# same for the total
self.assertEqual(amount["total"], 14.85)

def test_upgrade_last_update_date(self):
last_external_update_date = self._get_last_external_update_date(
self.cart
)
self.add_item(self.product_1.id, 2)
self._check_last_external_update_date(
self.cart, last_external_update_date
)

time.sleep(1)

last_external_update_date = self._get_last_external_update_date(
self.cart
)
line_id = self.cart.order_line[0].id
self.update_item(line_id, 5)
self._check_last_external_update_date(
self.cart, last_external_update_date
)

time.sleep(1)

last_external_update_date = self._get_last_external_update_date(
self.cart
)
self.delete_item(line_id)
self._check_last_external_update_date(
self.cart, last_external_update_date
)


class AnonymousItemCase(AbstractItemCase, CommonCase):
def setUp(self, *args, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion shopinvader_cart_expiry/models/shopinvader_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def manage_cart_expiry(self):
("shopinvader_backend_id", "=", self.id),
("typology", "=", "cart"),
("state", "=", "draft"),
("write_date", "<=", expiry_date),
("last_external_update_date", "<=", expiry_date),
]
cart_expired = self.env["sale.order"].search(domain)
if cart_expired:
Expand Down
8 changes: 5 additions & 3 deletions shopinvader_cart_expiry/tests/test_cart_expiry.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def setUp(self):
super(TestCartExpiry, self).setUp()
self.sale_obj = self.env["sale.order"]
self.sale = self.env.ref("shopinvader.sale_order_2")
self.sale.write({"last_external_update_date": fields.Datetime.now()})
self.so_date = self.sale.last_external_update_date

def test_cart_expiry_scheduler(self):
"""
Expand All @@ -34,7 +36,7 @@ def test_cart_expiry_scheduler(self):
return

def test_cart_expiry_cancel(self):
so_date = fields.Datetime.from_string(self.sale.write_date)
so_date = fields.Datetime.from_string(self.so_date)
today = fields.Datetime.to_string(so_date + timedelta(hours=5))
self.backend.write(
{"cart_expiry_delay": 1, "cart_expiry_policy": "cancel"}
Expand All @@ -52,7 +54,7 @@ def test_cart_expiry_cancel(self):
self.assertEqual(self.sale.state, "cancel")

def test_cart_expiry_delete(self):
so_date = fields.Datetime.from_string(self.sale.write_date)
so_date = fields.Datetime.from_string(self.so_date)
today = fields.Datetime.to_string(so_date + timedelta(hours=5))
self.backend.write(
{"cart_expiry_delay": 1, "cart_expiry_policy": "delete"}
Expand All @@ -74,7 +76,7 @@ def test_cart_expiry_not_draft(self):
Ensure the cart is not deleted/canceled when the state is not draft.
:return:
"""
so_date = fields.Datetime.from_string(self.sale.write_date)
so_date = fields.Datetime.from_string(self.so_date)
today = fields.Datetime.to_string(so_date + timedelta(hours=5))
self.sale.write({"state": "sent"})
self.backend.write(
Expand Down
2 changes: 1 addition & 1 deletion shopinvader_pending_cart_reminder/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def _get_sale_order_domain_for_reminder(self, backend, reminder_date):
("shopinvader_backend_id", "=", backend.id),
("typology", "=", "cart"),
("pending_cart_reminder_sent_dt", "=", False),
("write_date", "<=", reminder_date),
("last_external_update_date", "<=", reminder_date),
("create_date", ">=", backend.reminder_start_date),
]
return domain
Expand Down
1 change: 1 addition & 0 deletions shopinvader_pending_cart_reminder/tests/test_sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def setUp(self):
super(TestSaleOrder, self).setUp()
self.sale_obj = self.env["sale.order"]
self.sale = self.env.ref("shopinvader.sale_order_2")
self.sale.write({"last_external_update_date": fields.Datetime.now()})
self.template = self.env.ref(
"shopinvader_pending_cart_reminder."
"mail_template_shopinvader_sale_reminder"
Expand Down