Skip to content

Commit

Permalink
#789 get cart route (#790)
Browse files Browse the repository at this point in the history
* Create route to fetch Cart data

* Add url. Create test

* Create getCart on front

* Fix cache issue for cart-get route

* Add comment about middleware ordering

* Remove ETAG condition from Cart view

* Test Cache-Control: no-store for Cart view

* Remove dangling imports
  • Loading branch information
ArtemijRodionov committed Mar 25, 2019
1 parent 63cb15c commit 10b86be
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 3 deletions.
2 changes: 2 additions & 0 deletions front/js/components/headerCart.es6
Expand Up @@ -8,6 +8,8 @@
};

const init = () => {
// @todo #789:60m Load cart lazily.
// See the parent task for details.
setUpListeners();
};

Expand Down
9 changes: 9 additions & 0 deletions front/js/shared/server.es6
Expand Up @@ -6,6 +6,7 @@ const server = (() => { // Ignore ESLintBear (no-unused-vars)
changeCartUrl: '/shop/cart-change/',
removeFromCartUrl: '/shop/cart-remove/',
flushCartUrl: '/shop/cart-flush/',
getCartUrl: '/shop/cart-get/',
yandexOrderUrl: '/shop/yandex-order/',
setViewTypeUrl: '/set-view-type/',
saveFeedback: '/save-feedback/',
Expand Down Expand Up @@ -103,12 +104,20 @@ const server = (() => { // Ignore ESLintBear (no-unused-vars)
);
}

/**
* Return $.get request, which gives Cart data.
*/
function getCart() {
return $.get(config.getCartUrl);
}

const sendYandexOrder = data => $.post(config.yandexOrderUrl, data);

return {
addToCart,
changeInCart,
deleteFeedback,
getCart,
loadProducts,
flushCart,
oneClickBuy,
Expand Down
8 changes: 5 additions & 3 deletions shopelectro/settings/base.py
Expand Up @@ -77,18 +77,20 @@
'shopelectro',
]

# Stick to this ordering
# https://docs.djangoproject.com/en/1.11/ref/middleware/#middleware-ordering
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.cache.UpdateCacheMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django_user_agents.middleware.UserAgentMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware',
'refarm_redirects.middleware.RedirectAllMiddleware',
Expand Down
18 changes: 18 additions & 0 deletions shopelectro/tests/tests_views.py
Expand Up @@ -788,3 +788,21 @@ def test_disabled_cache(self):
self.client.get(url)['Cache-Control'],
'max-age=0, no-cache, no-store, must-revalidate',
)


@tag('fast')
class Cart(TestCase):

fixtures = ['dump.json']

def test_get_cart(self):
response = self.client.get(reverse('cart_get'))
self.assertEqual(response.status_code, 200)
self.assertTrue(json_to_dict(response))

def test_disabled_cache(self):
"""Cache-Control is disabled for the cart-get."""
self.assertEqual(
self.client.get(reverse('cart_get'))['Cache-Control'],
'max-age=0, no-cache, no-store, must-revalidate',
)
1 change: 1 addition & 0 deletions shopelectro/urls.py
Expand Up @@ -87,6 +87,7 @@ def cache_page(arg): # Ignore PyFlakesBear
]

ecommerce_urls = [
url(r'^cart-get/$', never_cache(views.Cart.as_view()), name='cart_get'),
url(r'^cart-add/$', views.AddToCart.as_view(), name='cart_add'),
url(r'^cart-change/$', views.ChangeCount.as_view(), name='cart_set_count'),
url(r'^cart-flush/$', views.FlushCart.as_view(), name='cart_flush'),
Expand Down
13 changes: 13 additions & 0 deletions shopelectro/views/ecommerce.py
Expand Up @@ -34,6 +34,19 @@ def get_context_data(self, request, **kwargs):
}


# @todo #789:60m Make cart routes REST style instead of RPC.


class Cart(ec_views.CartModifier):

cart = SECart
position_model = Product
order_form = OrderForm

def get(self, request):
return self.json_response(request)


class AddToCart(ec_views.AddToCart):
cart = SECart
position_model = Product
Expand Down

2 comments on commit 10b86be

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 10b86be Mar 25, 2019

Choose a reason for hiding this comment

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

Puzzle 789-ab1e4d1d discovered in shopelectro/views/ecommerce.py and submitted as #793. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 10b86be Mar 25, 2019

Choose a reason for hiding this comment

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

Puzzle 789-a1cd638b discovered in front/js/components/headerCart.es6 and submitted as #794. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.