Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
ec887120
Commit
ec887120
authored
Oct 16, 2013
by
Jason Bau
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use context processor instead of middleware
parent
6e543c14
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
36 additions
and
49 deletions
+36
-49
lms/djangoapps/shoppingcart/context_processor.py
+16
-24
lms/djangoapps/shoppingcart/tests/test_context_processor.py
+16
-21
lms/envs/common.py
+3
-3
lms/templates/navigation.html
+1
-1
No files found.
lms/djangoapps/shoppingcart/
middleware
.py
→
lms/djangoapps/shoppingcart/
context_processor
.py
View file @
ec887120
"""
This is the shoppingcart
middleware class
.
Currently the only
middleware is
detects whether request.user has a cart that should be displayed in the
navigation. We want to do this in the
middleware
to
This is the shoppingcart
context_processor module
.
Currently the only
context_processor
detects whether request.user has a cart that should be displayed in the
navigation. We want to do this in the
context_processor
to
1) keep database accesses out of templates (this led to a transaction bug with user email changes)
2) because navigation.html is "called" by being included in other templates, there's no "views.py" to put this.
"""
...
...
@@ -9,26 +9,18 @@ from django.conf import settings
import
shoppingcart
class
UserHasCartMiddleware
(
objec
t
):
def
user_has_cart_context_processor
(
reques
t
):
"""
Detects whether request.user has a cart and sets it as part of the request
***
Because this relies on request.user, it needs to enabled in settings after
django.contrib.auth.middleware.AuthenticationMiddleware (or equivalent)
***
Checks if request has an authenticated user. If so, checks if request.user has a cart that should
be displayed. Anonymous users don't.
Adds `display_shopping_cart` to the context
"""
def
process_request
(
self
,
request
):
"""
Checks if request has an authenticated user. If so, checks if request.user has a cart that should
be displayed. Anonymous users don't.
"""
request
.
display_shopping_cart
=
False
if
(
request
.
user
.
is_authenticated
()
and
# user exists
settings
.
MITX_FEATURES
.
get
(
'ENABLE_PAID_COURSE_REGISTRATION'
)
and
# settings is set
settings
.
MITX_FEATURES
.
get
(
'ENABLE_SHOPPING_CART'
)
and
# setting is set
shoppingcart
.
models
.
Order
.
user_cart_has_items
(
request
.
user
)
# user's cart is non-empty
):
request
.
display_shopping_cart
=
True
return
None
display_shopping_cart
=
False
if
(
request
.
user
.
is_authenticated
()
and
# user exists
settings
.
MITX_FEATURES
.
get
(
'ENABLE_PAID_COURSE_REGISTRATION'
)
and
# settings is set
settings
.
MITX_FEATURES
.
get
(
'ENABLE_SHOPPING_CART'
)
and
# setting is set
shoppingcart
.
models
.
Order
.
user_cart_has_items
(
request
.
user
)
# user's cart is non-empty
):
display_shopping_cart
=
True
return
{
'display_shopping_cart'
:
display_shopping_cart
}
lms/djangoapps/shoppingcart/tests/test_
middleware
.py
→
lms/djangoapps/shoppingcart/tests/test_
context_processor
.py
View file @
ec887120
"""
Unit tests for shoppingcart
middleware
Unit tests for shoppingcart
context_processor
"""
from
mock
import
patch
,
Mock
from
django.conf
import
settings
...
...
@@ -10,31 +10,26 @@ from courseware.tests.tests import TEST_DATA_MONGO_MODULESTORE
from
xmodule.modulestore.tests.django_utils
import
ModuleStoreTestCase
from
xmodule.modulestore.tests.factories
import
CourseFactory
from
student.tests.factories
import
UserFactory
from
course_modes.
models
import
CourseMode
from
course_modes.
tests.factories
import
CourseModeFactory
from
shoppingcart.models
import
Order
,
PaidCourseRegistration
from
shoppingcart.
middleware
import
UserHasCartMiddleware
from
shoppingcart.
context_processor
import
user_has_cart_context_processor
@override_settings
(
MODULESTORE
=
TEST_DATA_MONGO_MODULESTORE
)
class
UserCart
Middleware
UnitTest
(
ModuleStoreTestCase
):
class
UserCart
ContextProcessor
UnitTest
(
ModuleStoreTestCase
):
"""
Unit test for shoppingcart
middleware UserHasCartMiddleware
Unit test for shoppingcart
context_processor
"""
def
setUp
(
self
):
self
.
user
=
UserFactory
.
create
()
self
.
request
=
Mock
()
self
.
middleware
=
UserHasCartMiddleware
()
def
add_to_cart
(
self
):
"""
Adds content to self.user's cart
"""
course
=
CourseFactory
.
create
(
org
=
'MITx'
,
number
=
'999'
,
display_name
=
'Robot Super Course'
)
course_mode
=
CourseMode
(
course_id
=
course
.
id
,
mode_slug
=
"honor"
,
mode_display_name
=
"honor cert"
,
min_price
=
40
)
course_mode
.
save
()
CourseModeFactory
(
course_id
=
course
.
id
)
cart
=
Order
.
get_cart_for_user
(
self
.
user
)
PaidCourseRegistration
.
add_to_order
(
cart
,
course
.
id
)
...
...
@@ -45,8 +40,8 @@ class UserCartMiddlewareUnitTest(ModuleStoreTestCase):
"""
self
.
add_to_cart
()
self
.
request
.
user
=
self
.
user
self
.
middleware
.
process_request
(
self
.
request
)
self
.
assertFalse
(
self
.
request
.
display_shopping_cart
)
context
=
user_has_cart_context_processor
(
self
.
request
)
self
.
assertFalse
(
context
[
'display_shopping_cart'
]
)
@patch.dict
(
settings
.
MITX_FEATURES
,
{
'ENABLE_SHOPPING_CART'
:
True
,
'ENABLE_PAID_COURSE_REGISTRATION'
:
False
})
def
test_no_enable_paid_course_registration
(
self
):
...
...
@@ -55,8 +50,8 @@ class UserCartMiddlewareUnitTest(ModuleStoreTestCase):
"""
self
.
add_to_cart
()
self
.
request
.
user
=
self
.
user
self
.
middleware
.
process_request
(
self
.
request
)
self
.
assertFalse
(
self
.
request
.
display_shopping_cart
)
context
=
user_has_cart_context_processor
(
self
.
request
)
self
.
assertFalse
(
context
[
'display_shopping_cart'
]
)
@patch.dict
(
settings
.
MITX_FEATURES
,
{
'ENABLE_SHOPPING_CART'
:
True
,
'ENABLE_PAID_COURSE_REGISTRATION'
:
True
})
def
test_anonymous_user
(
self
):
...
...
@@ -64,8 +59,8 @@ class UserCartMiddlewareUnitTest(ModuleStoreTestCase):
Tests when request.user is anonymous
"""
self
.
request
.
user
=
AnonymousUser
()
self
.
middleware
.
process_request
(
self
.
request
)
self
.
assertFalse
(
self
.
request
.
display_shopping_cart
)
context
=
user_has_cart_context_processor
(
self
.
request
)
self
.
assertFalse
(
context
[
'display_shopping_cart'
]
)
@patch.dict
(
settings
.
MITX_FEATURES
,
{
'ENABLE_SHOPPING_CART'
:
True
,
'ENABLE_PAID_COURSE_REGISTRATION'
:
True
})
def
test_no_items_in_cart
(
self
):
...
...
@@ -73,8 +68,8 @@ class UserCartMiddlewareUnitTest(ModuleStoreTestCase):
Tests when request.user doesn't have a cart with items
"""
self
.
request
.
user
=
self
.
user
self
.
middleware
.
process_request
(
self
.
request
)
self
.
assertFalse
(
self
.
request
.
display_shopping_cart
)
context
=
user_has_cart_context_processor
(
self
.
request
)
self
.
assertFalse
(
context
[
'display_shopping_cart'
]
)
@patch.dict
(
settings
.
MITX_FEATURES
,
{
'ENABLE_SHOPPING_CART'
:
True
,
'ENABLE_PAID_COURSE_REGISTRATION'
:
True
})
def
test_items_in_cart
(
self
):
...
...
@@ -83,5 +78,5 @@ class UserCartMiddlewareUnitTest(ModuleStoreTestCase):
"""
self
.
add_to_cart
()
self
.
request
.
user
=
self
.
user
self
.
middleware
.
process_request
(
self
.
request
)
self
.
assertTrue
(
self
.
request
.
display_shopping_cart
)
context
=
user_has_cart_context_processor
(
self
.
request
)
self
.
assertTrue
(
context
[
'display_shopping_cart'
]
)
lms/envs/common.py
View file @
ec887120
...
...
@@ -272,6 +272,9 @@ TEMPLATE_CONTEXT_PROCESSORS = (
# Hack to get required link URLs to password reset templates
'mitxmako.shortcuts.marketing_link_context_processor'
,
# Shoppingcart processor (detects if request.user has a cart)
'shoppingcart.context_processor.user_has_cart_context_processor'
,
)
# use the ratelimit backend to prevent brute force attacks
...
...
@@ -610,9 +613,6 @@ MIDDLEWARE_CLASSES = (
# For A/B testing
'waffle.middleware.WaffleMiddleware'
,
# Shoppingcart middleware (detects if request.user has a cart)
'shoppingcart.middleware.UserHasCartMiddleware'
,
)
############################### Pipeline #######################################
...
...
lms/templates/navigation.html
View file @
ec887120
...
...
@@ -81,7 +81,7 @@ site_status_msg = get_site_status_msg(course_id)
</ul>
</li>
</ol>
% if
getattr(request, 'display_shopping_cart', False): # see shoppingcart.middleware.UserHasCartMiddleware
% if
display_shopping_cart: # see shoppingcart.context_processor.user_has_cart_context_processor
<ol
class=
"user"
>
<li
class=
"primary"
>
<a
class=
"shopping-cart"
href=
"${reverse('shoppingcart.views.show_cart')}"
>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment