Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
ecommerce
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
ecommerce
Commits
7985cc96
Commit
7985cc96
authored
Jul 13, 2016
by
Michael Frey
Committed by
GitHub
Jul 13, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #857 from edx/mjfrey/cache-catalog-contains
Mjfrey/cache catalog contains
parents
bccd44b3
e70276fd
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
0 deletions
+22
-0
ecommerce/extensions/offer/models.py
+10
-0
ecommerce/extensions/offer/tests/test_models.py
+12
-0
No files found.
ecommerce/extensions/offer/models.py
View file @
7985cc96
# noinspection PyUnresolvedReferences
# noinspection PyUnresolvedReferences
import
hashlib
from
django.conf
import
settings
from
django.core.cache
import
cache
from
django.db
import
models
from
django.db
import
models
from
oscar.apps.offer.abstract_models
import
AbstractRange
from
oscar.apps.offer.abstract_models
import
AbstractRange
from
threadlocals.threadlocals
import
get_current_request
from
threadlocals.threadlocals
import
get_current_request
...
@@ -15,14 +19,20 @@ class Range(AbstractRange):
...
@@ -15,14 +19,20 @@ class Range(AbstractRange):
"""
"""
Retrieve the results from running the query contained in catalog_query field.
Retrieve the results from running the query contained in catalog_query field.
"""
"""
cache_key
=
'catalog_query_contains [{}] [{}]'
.
format
(
self
.
catalog_query
,
product
.
course_id
)
cache_hash
=
hashlib
.
md5
(
cache_key
)
.
hexdigest
()
response
=
cache
.
get
(
cache_hash
)
if
not
response
:
# pragma: no cover
request
=
get_current_request
()
request
=
get_current_request
()
try
:
try
:
response
=
request
.
site
.
siteconfiguration
.
course_catalog_api_client
.
course_runs
.
contains
.
get
(
response
=
request
.
site
.
siteconfiguration
.
course_catalog_api_client
.
course_runs
.
contains
.
get
(
query
=
self
.
catalog_query
,
query
=
self
.
catalog_query
,
course_run_ids
=
product
.
course_id
course_run_ids
=
product
.
course_id
)
)
cache
.
set
(
cache_hash
,
response
,
settings
.
COURSES_API_CACHE_TIMEOUT
)
except
:
# pylint: disable=bare-except
except
:
# pylint: disable=bare-except
raise
Exception
(
'Could not contact Course Catalog Service.'
)
raise
Exception
(
'Could not contact Course Catalog Service.'
)
return
response
return
response
def
contains_product
(
self
,
product
):
def
contains_product
(
self
,
product
):
...
...
ecommerce/extensions/offer/tests/test_models.py
View file @
7985cc96
import
hashlib
import
httpretty
import
httpretty
import
mock
import
mock
from
django.core.cache
import
cache
from
django.test
import
RequestFactory
from
django.test
import
RequestFactory
from
oscar.core.loading
import
get_model
from
oscar.core.loading
import
get_model
from
oscar.test
import
factories
from
oscar.test
import
factories
...
@@ -73,10 +76,18 @@ class RangeTests(CouponMixin, CourseCatalogTestMixin, CourseCatalogMockMixin, Te
...
@@ -73,10 +76,18 @@ class RangeTests(CouponMixin, CourseCatalogTestMixin, CourseCatalogMockMixin, Te
self
.
mock_dynamic_catalog_contains_api
(
query
=
'key:*'
,
course_run_ids
=
[
course
.
id
])
self
.
mock_dynamic_catalog_contains_api
(
query
=
'key:*'
,
course_run_ids
=
[
course
.
id
])
request
=
RequestFactory
()
request
=
RequestFactory
()
request
.
site
=
self
.
site
request
.
site
=
self
.
site
self
.
range
.
catalog_query
=
'key:*'
cache_key
=
'catalog_query_contains [{}] [{}]'
.
format
(
'key:*'
,
seat
.
course_id
)
cache_hash
=
hashlib
.
md5
(
cache_key
)
.
hexdigest
()
cached_response
=
cache
.
get
(
cache_hash
)
self
.
assertIsNone
(
cached_response
)
with
mock
.
patch
(
'ecommerce.core.url_utils.get_current_request'
,
mock
.
Mock
(
return_value
=
request
)):
with
mock
.
patch
(
'ecommerce.core.url_utils.get_current_request'
,
mock
.
Mock
(
return_value
=
request
)):
response
=
self
.
range
.
run_catalog_query
(
seat
)
response
=
self
.
range
.
run_catalog_query
(
seat
)
self
.
assertTrue
(
response
[
'course_runs'
][
course
.
id
])
self
.
assertTrue
(
response
[
'course_runs'
][
course
.
id
])
cached_response
=
cache
.
get
(
cache_hash
)
self
.
assertEqual
(
response
,
cached_response
)
@httpretty.activate
@httpretty.activate
@mock_course_catalog_api_client
@mock_course_catalog_api_client
...
@@ -86,6 +97,7 @@ class RangeTests(CouponMixin, CourseCatalogTestMixin, CourseCatalogMockMixin, Te
...
@@ -86,6 +97,7 @@ class RangeTests(CouponMixin, CourseCatalogTestMixin, CourseCatalogMockMixin, Te
"""
"""
course
,
seat
=
self
.
create_course_and_seat
()
course
,
seat
=
self
.
create_course_and_seat
()
self
.
mock_dynamic_catalog_contains_api
(
query
=
'key:*'
,
course_run_ids
=
[
course
.
id
])
self
.
mock_dynamic_catalog_contains_api
(
query
=
'key:*'
,
course_run_ids
=
[
course
.
id
])
false_response
=
self
.
range
.
contains_product
(
seat
)
false_response
=
self
.
range
.
contains_product
(
seat
)
self
.
assertFalse
(
false_response
)
self
.
assertFalse
(
false_response
)
...
...
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