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
54cdf27a
Commit
54cdf27a
authored
Jun 02, 2016
by
Michael Frey
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #771 from edx/mjfrey/course-api-util
DRY: Use utility function to get course info from LMS
parents
edccf515
2d211084
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
51 additions
and
17 deletions
+51
-17
ecommerce/coupons/views.py
+2
-5
ecommerce/courses/tests/test_utils.py
+26
-1
ecommerce/courses/utils.py
+21
-0
ecommerce/extensions/basket/views.py
+2
-11
No files found.
ecommerce/coupons/views.py
View file @
54cdf27a
...
@@ -14,12 +14,12 @@ from django.utils.translation import ugettext_lazy as _
...
@@ -14,12 +14,12 @@ from django.utils.translation import ugettext_lazy as _
from
django.shortcuts
import
render
from
django.shortcuts
import
render
from
django.utils
import
timezone
from
django.utils
import
timezone
from
django.views.generic
import
TemplateView
,
View
from
django.views.generic
import
TemplateView
,
View
from
edx_rest_api_client.client
import
EdxRestApiClient
from
edx_rest_api_client.exceptions
import
SlumberHttpBaseException
from
edx_rest_api_client.exceptions
import
SlumberHttpBaseException
from
oscar.core.loading
import
get_class
,
get_model
from
oscar.core.loading
import
get_class
,
get_model
from
ecommerce.core.url_utils
import
get_ecommerce_url
,
get_lms_url
from
ecommerce.core.url_utils
import
get_ecommerce_url
,
get_lms_url
from
ecommerce.core.views
import
StaffOnlyMixin
from
ecommerce.core.views
import
StaffOnlyMixin
from
ecommerce.courses.utils
import
get_course_info_from_lms
from
ecommerce.extensions.api
import
exceptions
from
ecommerce.extensions.api
import
exceptions
from
ecommerce.extensions.analytics.utils
import
prepare_analytics_data
from
ecommerce.extensions.analytics.utils
import
prepare_analytics_data
from
ecommerce.extensions.api.constants
import
APIConstants
as
AC
from
ecommerce.extensions.api.constants
import
APIConstants
as
AC
...
@@ -136,11 +136,8 @@ class CouponOfferView(TemplateView):
...
@@ -136,11 +136,8 @@ class CouponOfferView(TemplateView):
}
}
valid_voucher
,
msg
=
voucher_is_valid
(
voucher
,
product
,
self
.
request
)
valid_voucher
,
msg
=
voucher_is_valid
(
voucher
,
product
,
self
.
request
)
if
valid_voucher
:
if
valid_voucher
:
api
=
EdxRestApiClient
(
get_lms_url
(
'api/courses/v1/'
),
)
try
:
try
:
course
=
api
.
courses
(
product
.
course_id
)
.
get
(
)
course
=
get_course_info_from_lms
(
product
.
course_id
)
except
SlumberHttpBaseException
as
e
:
except
SlumberHttpBaseException
as
e
:
logger
.
exception
(
'Could not get course information. [
%
s]'
,
e
)
logger
.
exception
(
'Could not get course information. [
%
s]'
,
e
)
return
{
return
{
...
...
ecommerce/courses/tests/test_utils.py
View file @
54cdf27a
import
hashlib
import
ddt
import
ddt
import
httpretty
from
django.core.cache
import
cache
from
ecommerce.core.url_utils
import
get_lms_url
from
ecommerce.courses.models
import
Course
from
ecommerce.courses.models
import
Course
from
ecommerce.courses.utils
import
mode_for_seat
from
ecommerce.courses.tests.factories
import
CourseFactory
from
ecommerce.courses.utils
import
get_course_info_from_lms
,
mode_for_seat
from
ecommerce.extensions.catalogue.tests.mixins
import
CourseCatalogTestMixin
from
ecommerce.extensions.catalogue.tests.mixins
import
CourseCatalogTestMixin
from
ecommerce.tests.testcases
import
TestCase
from
ecommerce.tests.testcases
import
TestCase
@httpretty.activate
@ddt.ddt
@ddt.ddt
class
UtilsTests
(
CourseCatalogTestMixin
,
TestCase
):
class
UtilsTests
(
CourseCatalogTestMixin
,
TestCase
):
@ddt.unpack
@ddt.unpack
...
@@ -24,3 +32,20 @@ class UtilsTests(CourseCatalogTestMixin, TestCase):
...
@@ -24,3 +32,20 @@ class UtilsTests(CourseCatalogTestMixin, TestCase):
course
=
Course
.
objects
.
create
(
id
=
'edx/Demo_Course/DemoX'
)
course
=
Course
.
objects
.
create
(
id
=
'edx/Demo_Course/DemoX'
)
seat
=
course
.
create_or_update_seat
(
certificate_type
,
id_verification_required
,
10.00
,
self
.
partner
)
seat
=
course
.
create_or_update_seat
(
certificate_type
,
id_verification_required
,
10.00
,
self
.
partner
)
self
.
assertEqual
(
mode_for_seat
(
seat
),
mode
)
self
.
assertEqual
(
mode_for_seat
(
seat
),
mode
)
def
test_get_course_info_from_lms
(
self
):
""" Check to see if course info gets cached """
course
=
CourseFactory
()
course_url
=
get_lms_url
(
'api/courses/v1/courses/{}/'
.
format
(
course
.
id
))
httpretty
.
register_uri
(
httpretty
.
GET
,
course_url
,
body
=
course
.
name
,
status
=
200
,
content_type
=
'application/json'
)
cache_key
=
'courses_api_detail_{}'
.
format
(
course
.
id
)
cache_hash
=
hashlib
.
md5
(
cache_key
)
.
hexdigest
()
cached_course
=
cache
.
get
(
cache_hash
)
self
.
assertIsNone
(
cached_course
)
response
=
get_course_info_from_lms
(
course
.
id
)
self
.
assertEqual
(
response
,
course
.
name
)
cached_course
=
cache
.
get
(
cache_hash
)
self
.
assertEqual
(
cached_course
,
response
)
ecommerce/courses/utils.py
View file @
54cdf27a
import
hashlib
from
django.conf
import
settings
from
django.core.cache
import
cache
from
edx_rest_api_client.client
import
EdxRestApiClient
from
ecommerce.core.url_utils
import
get_lms_url
def
mode_for_seat
(
seat
):
def
mode_for_seat
(
seat
):
""" Returns the Enrollment mode for a given seat product. """
""" Returns the Enrollment mode for a given seat product. """
certificate_type
=
getattr
(
seat
.
attr
,
'certificate_type'
,
''
)
certificate_type
=
getattr
(
seat
.
attr
,
'certificate_type'
,
''
)
...
@@ -8,3 +17,15 @@ def mode_for_seat(seat):
...
@@ -8,3 +17,15 @@ def mode_for_seat(seat):
return
'audit'
return
'audit'
return
certificate_type
return
certificate_type
def
get_course_info_from_lms
(
course_key
):
""" Get course information from LMS via the course api and cache """
api
=
EdxRestApiClient
(
get_lms_url
(
'api/courses/v1/'
))
cache_key
=
'courses_api_detail_{}'
.
format
(
course_key
)
cache_hash
=
hashlib
.
md5
(
cache_key
)
.
hexdigest
()
course
=
cache
.
get
(
cache_hash
)
if
not
course
:
# pragma: no cover
course
=
api
.
courses
(
course_key
)
.
get
()
cache
.
set
(
cache_hash
,
course
,
settings
.
COURSES_API_CACHE_TIMEOUT
)
return
course
ecommerce/extensions/basket/views.py
View file @
54cdf27a
from
__future__
import
unicode_literals
from
__future__
import
unicode_literals
import
hashlib
import
logging
import
logging
from
django.conf
import
settings
from
django.http
import
HttpResponseBadRequest
,
HttpResponseRedirect
from
django.http
import
HttpResponseBadRequest
,
HttpResponseRedirect
from
django.core.cache
import
cache
from
django.core.urlresolvers
import
reverse
from
django.core.urlresolvers
import
reverse
from
django.utils.translation
import
ugettext_lazy
as
_
from
django.utils.translation
import
ugettext_lazy
as
_
from
requests.exceptions
import
ConnectionError
,
Timeout
from
requests.exceptions
import
ConnectionError
,
Timeout
...
@@ -17,7 +14,7 @@ from slumber.exceptions import SlumberBaseException
...
@@ -17,7 +14,7 @@ from slumber.exceptions import SlumberBaseException
from
ecommerce.core.constants
import
ENROLLMENT_CODE_PRODUCT_CLASS_NAME
,
SEAT_PRODUCT_CLASS_NAME
from
ecommerce.core.constants
import
ENROLLMENT_CODE_PRODUCT_CLASS_NAME
,
SEAT_PRODUCT_CLASS_NAME
from
ecommerce.core.url_utils
import
get_lms_url
,
get_lms_enrollment_base_api_url
from
ecommerce.core.url_utils
import
get_lms_url
,
get_lms_enrollment_base_api_url
from
ecommerce.coupons.views
import
get_voucher_from_code
from
ecommerce.coupons.views
import
get_voucher_from_code
from
ecommerce.courses.utils
import
mode_for_seat
from
ecommerce.courses.utils
import
get_course_info_from_lms
,
mode_for_seat
from
ecommerce.extensions.analytics.utils
import
prepare_analytics_data
from
ecommerce.extensions.analytics.utils
import
prepare_analytics_data
from
ecommerce.extensions.basket.utils
import
get_certificate_type_display_value
,
prepare_basket
,
get_basket_switch_data
from
ecommerce.extensions.basket.utils
import
get_certificate_type_display_value
,
prepare_basket
,
get_basket_switch_data
from
ecommerce.extensions.offer.utils
import
format_benefit_value
from
ecommerce.extensions.offer.utils
import
format_benefit_value
...
@@ -111,22 +108,16 @@ class BasketSummaryView(BasketView):
...
@@ -111,22 +108,16 @@ class BasketSummaryView(BasketView):
formset
=
context
.
get
(
'formset'
,
[])
formset
=
context
.
get
(
'formset'
,
[])
lines
=
context
.
get
(
'line_list'
,
[])
lines
=
context
.
get
(
'line_list'
,
[])
lines_data
=
[]
lines_data
=
[]
api
=
EdxRestApiClient
(
get_lms_url
(
'api/courses/v1/'
))
is_verification_required
=
is_bulk_purchase
=
False
is_verification_required
=
is_bulk_purchase
=
False
switch_link_text
=
partner_sku
=
''
switch_link_text
=
partner_sku
=
''
for
line
in
lines
:
for
line
in
lines
:
course_key
=
CourseKey
.
from_string
(
line
.
product
.
attr
.
course_key
)
course_key
=
CourseKey
.
from_string
(
line
.
product
.
attr
.
course_key
)
cache_key
=
'courses_api_detail_{}'
.
format
(
course_key
)
cache_hash
=
hashlib
.
md5
(
cache_key
)
.
hexdigest
()
course_name
=
None
course_name
=
None
image_url
=
None
image_url
=
None
short_description
=
None
short_description
=
None
try
:
try
:
course
=
cache
.
get
(
cache_hash
)
course
=
get_course_info_from_lms
(
course_key
)
if
not
course
:
course
=
api
.
courses
(
course_key
)
.
get
()
cache
.
set
(
cache_hash
,
course
,
settings
.
COURSES_API_CACHE_TIMEOUT
)
image_url
=
get_lms_url
(
course
[
'media'
][
'course_image'
][
'uri'
])
image_url
=
get_lms_url
(
course
[
'media'
][
'course_image'
][
'uri'
])
short_description
=
course
[
'short_description'
]
short_description
=
course
[
'short_description'
]
course_name
=
course
[
'name'
]
course_name
=
course
[
'name'
]
...
...
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