Skip to content

Commit

Permalink
Oscarapi always accessed and prcessed the basket even if it wasn't ne…
Browse files Browse the repository at this point in the history
…eded.

This solves that and also solves a problem with logout being broken because
cookies failed to be deleted.

Fixes #311
  • Loading branch information
specialunderwear committed Jul 14, 2023
1 parent aef18fa commit 8e63a6e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 21 deletions.
53 changes: 36 additions & 17 deletions oscarapi/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
get_basket,
)

from django.utils.functional import SimpleLazyObject, empty
from oscarapi.utils.loading import get_api_class
from oscarapi.utils.request import get_domain
from oscarapi.utils.session import session_id_from_parsed_session_uri, get_session
Expand Down Expand Up @@ -156,34 +157,52 @@ class ApiBasketMiddleWare(BasketMiddleware, IsApiRequest):

def __call__(self, request):
if self.is_api_request(request):
request.cookies_to_delete = []
# we should make sure that any cookie baskets are turned into
# session baskets, since oscarapi uses only baskets from the
# session.
cookie_key = self.get_cookie_key(request)

basket = self.get_cookie_basket(
cookie_key,
request,
Exception("get_cookie_basket doesn't use the manager argument"),
)
def load_basket_into_session():
request.cookies_to_delete = []
# we should make sure that any cookie baskets are turned into
# session baskets, since oscarapi uses only baskets from the
# session.
cookie_key = self.get_cookie_key(request)

basket = self.get_cookie_basket(
cookie_key,
request,
Exception("get_cookie_basket doesn't use the manager argument"),
)

if basket is not None:
# when a basket exists and we are already allowed to access
# this basket
if request_allows_access_to_basket(request, basket):
pass
else:
store_basket_in_session(basket, request.session)

request.basket = SimpleLazyObject(load_basket_into_session)

if basket is not None:
# when a basket exists and we are already allowed to access
# this basket
if request_allows_access_to_basket(request, basket):
pass
else:
store_basket_in_session(basket, request.session)
response = self.get_response(request)
return self.process_response(request, response)

return super(ApiBasketMiddleWare, self).__call__(request)

def process_response(self, request, response):
if not hasattr(request, "basket"):
return response

# If the basket was never initialized we can safely return
if (
isinstance(request.basket, SimpleLazyObject)
and request.basket._wrapped is empty # pylint: disable=protected-access
):
return response

if (
self.is_api_request(request)
and hasattr(request, "user")
and request.session
):
print("HIJ HEEFT WEL DIE SHIT GAST")
# at this point we are sure a basket can be found in the session
# (if the session hasn't been destroyed by logging out),
# because it is enforced in process_request.
Expand Down
2 changes: 1 addition & 1 deletion oscarapi/tests/unit/testbasket.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json

from mock import patch
from unittest.mock import patch

from django.contrib.auth import get_user_model
from django.urls import reverse
Expand Down
2 changes: 1 addition & 1 deletion oscarapi/tests/unit/testcheckout.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from decimal import Decimal
from mock import patch
from unittest.mock import patch

from django.contrib.auth import get_user_model
from django.urls import reverse
Expand Down
2 changes: 1 addition & 1 deletion oscarapi/tests/unit/testproduct.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import mock
from unittest import mock
import decimal
import datetime
import json
Expand Down
2 changes: 1 addition & 1 deletion oscarapi/tests/unit/testuser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from mock import patch
from unittest.mock import patch
from django.contrib.auth import get_user_model
from django.urls import reverse

Expand Down

0 comments on commit 8e63a6e

Please sign in to comment.