Commit 6e788d95 by Tasawer

new view added "get_provider_detail" for getting provider info with provider_id

ECOM-1858
parent c3f96faf
...@@ -155,12 +155,14 @@ def is_user_eligible_for_credit(username, course_key): ...@@ -155,12 +155,14 @@ def is_user_eligible_for_credit(username, course_key):
return CreditEligibility.is_user_eligible_for_credit(course_key, username) return CreditEligibility.is_user_eligible_for_credit(course_key, username)
def get_eligibilities_for_user(username): def get_eligibilities_for_user(username, course_key=None):
""" """
Retrieve all courses for which the user is eligible for credit. Retrieve all courses or particular course for which the user is eligible
for credit.
Arguments: Arguments:
username (unicode): Identifier of the user. username (unicode): Identifier of the user.
course_key (unicode): Identifier of the course.
Example: Example:
>>> get_eligibilities_for_user("ron") >>> get_eligibilities_for_user("ron")
...@@ -179,12 +181,17 @@ def get_eligibilities_for_user(username): ...@@ -179,12 +181,17 @@ def get_eligibilities_for_user(username):
Returns: list Returns: list
""" """
eligibilities = CreditEligibility.get_user_eligibilities(username)
if course_key:
eligibilities = eligibilities.filter(course_key=course_key)
return [ return [
{ {
"course_key": eligibility.course.course_key, "course_key": eligibility.course.course_key,
"deadline": eligibility.deadline, "deadline": eligibility.deadline,
} }
for eligibility in CreditEligibility.get_user_eligibilities(username) for eligibility in eligibilities
] ]
......
...@@ -30,66 +30,38 @@ from util.date_utils import to_timestamp ...@@ -30,66 +30,38 @@ from util.date_utils import to_timestamp
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def get_credit_providers(): def get_credit_providers(providers_list=None):
""" """Retrieve all available credit providers or filter on given providers_list.
Retrieve all available credit providers.
Example:
>>> get_credit_providers()
[
{
"id": "hogwarts",
"display_name": "Hogwarts School of Witchcraft and Wizardry"
},
...
]
Returns: list
"""
return CreditProvider.get_credit_providers()
def get_credit_provider_info(provider_id):
"""Retrieve the 'CreditProvider' model data against provided
credit provider.
Args: Arguments:
provider_id (str): The identifier for the credit provider providers_list (list of strings or None): contains list of ids of credit providers
or None.
Returns: 'CreditProvider' data dictionary
Example Usage: Returns:
>>> get_credit_provider_info("hogwarts") list of credit providers represented as dictionaries
{
"provider_id": "hogwarts", Response Values:
"display_name": "Hogwarts School of Witchcraft and Wizardry", >>> get_credit_providers(['hogwarts'])
"provider_url": "https://credit.example.com/", [
"provider_status_url": "https://credit.example.com/status/", {
"provider_description: "A new model for the Witchcraft and Wizardry School System.", "id": "hogwarts",
"enable_integration": False, "name": "Hogwarts School of Witchcraft and Wizardry",
"fulfillment_instructions": " "url": "https://credit.example.com/",
"status_url": "https://credit.example.com/status/",
"description: "A new model for the Witchcraft and Wizardry School System.",
"enable_integration": false,
"fulfillment_instructions": "
<p>In order to fulfill credit, Hogwarts School of Witchcraft and Wizardry requires learners to:</p> <p>In order to fulfill credit, Hogwarts School of Witchcraft and Wizardry requires learners to:</p>
<ul> <ul>
<li>Sample instruction abc</li> <li>Sample instruction abc</li>
<li>Sample instruction xyz</li> <li>Sample instruction xyz</li>
</ul>", </ul>",
} },
...
]
""" """
credit_provider = CreditProvider.get_credit_provider(provider_id=provider_id)
credit_provider_data = {}
if credit_provider:
credit_provider_data = {
"provider_id": credit_provider.provider_id,
"display_name": credit_provider.display_name,
"provider_url": credit_provider.provider_url,
"provider_status_url": credit_provider.provider_status_url,
"provider_description": credit_provider.provider_description,
"enable_integration": credit_provider.enable_integration,
"fulfillment_instructions": credit_provider.fulfillment_instructions
}
return credit_provider_data return CreditProvider.get_credit_providers(providers_list=providers_list)
@transaction.commit_on_success @transaction.commit_on_success
......
...@@ -114,31 +114,50 @@ class CreditProvider(TimeStampedModel): ...@@ -114,31 +114,50 @@ class CreditProvider(TimeStampedModel):
CREDIT_PROVIDERS_CACHE_KEY = "credit.providers.list" CREDIT_PROVIDERS_CACHE_KEY = "credit.providers.list"
@classmethod @classmethod
def get_credit_providers(cls): def get_credit_providers(cls, providers_list=None):
""" """
Retrieve a list of all credit providers, represented Retrieve a list of all credit providers or filter on providers_list, represented
as dictionaries. as dictionaries.
Arguments:
provider_list (list of strings or None): contains list of ids if required results
to be filtered, None for all providers.
Returns:
list of providers represented as dictionaries.
""" """
# Attempt to retrieve the credit provider list from the cache # Attempt to retrieve the credit provider list from the cache if provider_list is None
# The cache key is invalidated when the provider list is updated # The cache key is invalidated when the provider list is updated
# (a post-save signal handler on the CreditProvider model) # (a post-save signal handler on the CreditProvider model)
# This doesn't happen very often, so we would expect a *very* high # This doesn't happen very often, so we would expect a *very* high
# cache hit rate. # cache hit rate.
providers = cache.get(cls.CREDIT_PROVIDERS_CACHE_KEY)
# Cache miss: construct the provider list and save it in the cache credit_providers = cache.get(cls.CREDIT_PROVIDERS_CACHE_KEY)
if providers is None: if credit_providers is None:
providers = [ # Cache miss: construct the provider list and save it in the cache
credit_providers = CreditProvider.objects.filter(active=True)
credit_providers = [
{ {
"id": provider.provider_id, "id": provider.provider_id,
"display_name": provider.display_name, "display_name": provider.display_name,
"url": provider.provider_url,
"status_url": provider.provider_status_url, "status_url": provider.provider_status_url,
"description": provider.provider_description,
"enable_integration": provider.enable_integration,
"fulfillment_instructions": provider.fulfillment_instructions,
} }
for provider in CreditProvider.objects.filter(active=True) for provider in credit_providers
] ]
cache.set(cls.CREDIT_PROVIDERS_CACHE_KEY, providers)
return providers cache.set(cls.CREDIT_PROVIDERS_CACHE_KEY, credit_providers)
if providers_list:
credit_providers = [provider for provider in credit_providers if provider['id'] in providers_list]
return credit_providers
@classmethod @classmethod
def get_credit_provider(cls, provider_id): def get_credit_provider(cls, provider_id):
......
...@@ -440,7 +440,11 @@ class CreditProviderIntegrationApiTests(CreditApiTestBase): ...@@ -440,7 +440,11 @@ class CreditProviderIntegrationApiTests(CreditApiTestBase):
{ {
"id": self.PROVIDER_ID, "id": self.PROVIDER_ID,
"display_name": self.PROVIDER_NAME, "display_name": self.PROVIDER_NAME,
"url": self.PROVIDER_URL,
"status_url": self.PROVIDER_STATUS_URL, "status_url": self.PROVIDER_STATUS_URL,
"description": self.PROVIDER_DESCRIPTION,
"enable_integration": self.ENABLE_INTEGRATION,
"fulfillment_instructions": self.FULFILLMENT_INSTRUCTIONS
} }
]) ])
...@@ -452,25 +456,25 @@ class CreditProviderIntegrationApiTests(CreditApiTestBase): ...@@ -452,25 +456,25 @@ class CreditProviderIntegrationApiTests(CreditApiTestBase):
result = api.get_credit_providers() result = api.get_credit_providers()
self.assertEqual(result, []) self.assertEqual(result, [])
def test_get_credit_provider_details(self): def test_get_credit_providers_details(self):
"""Test that credit api method 'test_get_credit_provider_details' """Test that credit api method 'test_get_credit_provider_details'
returns dictionary data related to provided credit provider. returns dictionary data related to provided credit provider.
""" """
expected_result = { expected_result = [{
"provider_id": self.PROVIDER_ID, "id": self.PROVIDER_ID,
"display_name": self.PROVIDER_NAME, "display_name": self.PROVIDER_NAME,
"provider_url": self.PROVIDER_URL, "url": self.PROVIDER_URL,
"provider_status_url": self.PROVIDER_STATUS_URL, "status_url": self.PROVIDER_STATUS_URL,
"provider_description": self.PROVIDER_DESCRIPTION, "description": self.PROVIDER_DESCRIPTION,
"enable_integration": self.ENABLE_INTEGRATION, "enable_integration": self.ENABLE_INTEGRATION,
"fulfillment_instructions": self.FULFILLMENT_INSTRUCTIONS "fulfillment_instructions": self.FULFILLMENT_INSTRUCTIONS,
} }]
result = api.get_credit_provider_info(self.PROVIDER_ID) result = api.get_credit_providers([self.PROVIDER_ID])
self.assertEqual(result, expected_result) self.assertEqual(result, expected_result)
# now test that user gets empty dict for non existent credit provider # now test that user gets empty dict for non existent credit provider
result = api.get_credit_provider_info('fake_provider_id') result = api.get_credit_providers(['fake_provider_id'])
self.assertEqual(result, {}) self.assertEqual(result, [])
def test_credit_request(self): def test_credit_request(self):
# Initiate a credit request # Initiate a credit request
......
...@@ -3,20 +3,35 @@ URLs for the credit app. ...@@ -3,20 +3,35 @@ URLs for the credit app.
""" """
from django.conf.urls import patterns, url from django.conf.urls import patterns, url
from .views import create_credit_request, credit_provider_callback from .views import create_credit_request, credit_provider_callback, get_providers_detail, get_eligibility_for_user
PROVIDER_ID_PATTERN = r'(?P<provider_id>[^/]+)'
urlpatterns = patterns( urlpatterns = patterns(
'', '',
url( url(
r"^v1/provider/(?P<provider_id>[^/]+)/request/$", r"^v1/providers/$",
get_providers_detail,
name="providers_detail"
),
url(
r"^v1/providers/{provider_id}/request/$".format(provider_id=PROVIDER_ID_PATTERN),
create_credit_request, create_credit_request,
name="create_request" name="create_request"
), ),
url( url(
r"^v1/provider/(?P<provider_id>[^/]+)/callback/?$", r"^v1/providers/{provider_id}/callback/?$".format(provider_id=PROVIDER_ID_PATTERN),
credit_provider_callback, credit_provider_callback,
name="provider_callback" name="provider_callback"
), ),
url(
r"^v1/eligibility/$",
get_eligibility_for_user,
name="eligibility_details"
),
) )
...@@ -12,7 +12,7 @@ from django.http import ( ...@@ -12,7 +12,7 @@ from django.http import (
HttpResponseForbidden, HttpResponseForbidden,
Http404 Http404
) )
from django.views.decorators.http import require_POST from django.views.decorators.http import require_POST, require_GET
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
from django.conf import settings from django.conf import settings
...@@ -29,6 +29,54 @@ from openedx.core.djangoapps.credit.exceptions import CreditApiBadRequest, Credi ...@@ -29,6 +29,54 @@ from openedx.core.djangoapps.credit.exceptions import CreditApiBadRequest, Credi
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@require_GET
def get_providers_detail(request):
"""
**User Cases**
Returns details of the credit providers filtered by provided query parameters.
**Parameters:**
* provider_id (list of provider ids separated with ","): The identifiers for the providers for which
user requested
**Example Usage:**
GET /api/credit/v1/providers?provider_id=asu,hogwarts
"response": [
"id": "hogwarts",
"display_name": "Hogwarts School of Witchcraft and Wizardry",
"url": "https://credit.example.com/",
"status_url": "https://credit.example.com/status/",
"description": "A new model for the Witchcraft and Wizardry School System.",
"enable_integration": false,
"fulfillment_instructions": "
<p>In order to fulfill credit, Hogwarts School of Witchcraft and Wizardry requires learners to:</p>
<ul>
<li>Sample instruction abc</li>
<li>Sample instruction xyz</li>
</ul>",
},
...
]
**Responses:**
* 200 OK: The request was created successfully. Returned content
is a JSON-encoded dictionary describing what the client should
send to the credit provider.
* 404 Not Found: The provider does not exist.
"""
provider_id = request.GET.get("provider_id", None)
providers_list = provider_id.split(",") if provider_id else None
providers = api.get_credit_providers(providers_list)
return JsonResponse(providers)
@require_POST @require_POST
def create_credit_request(request, provider_id): def create_credit_request(request, provider_id):
""" """
...@@ -44,7 +92,7 @@ def create_credit_request(request, provider_id): ...@@ -44,7 +92,7 @@ def create_credit_request(request, provider_id):
**Example Usage:** **Example Usage:**
POST /api/credit/v1/provider/hogwarts/request/ POST /api/credit/v1/providers/hogwarts/request/
{ {
"username": "ron", "username": "ron",
"course_key": "edX/DemoX/Demo_Course" "course_key": "edX/DemoX/Demo_Course"
...@@ -136,7 +184,7 @@ def credit_provider_callback(request, provider_id): ...@@ -136,7 +184,7 @@ def credit_provider_callback(request, provider_id):
**Example Usage:** **Example Usage:**
POST /api/credit/v1/provider/{provider-id}/callback POST /api/credit/v1/providers/{provider-id}/callback
{ {
"request_uuid": "557168d0f7664fe59097106c67c3f847", "request_uuid": "557168d0f7664fe59097106c67c3f847",
"status": "approved", "status": "approved",
...@@ -200,6 +248,39 @@ def credit_provider_callback(request, provider_id): ...@@ -200,6 +248,39 @@ def credit_provider_callback(request, provider_id):
return HttpResponse() return HttpResponse()
@require_GET
def get_eligibility_for_user(request):
"""
**User Cases**
Retrieve user eligibility against course.
**Parameters:**
* course_key (unicode): Identifier of course.
* username (unicode): Username of current User.
**Example Usage:**
GET /api/credit/v1/eligibility?username=user&course_key=edX/Demo_101/Fall
"response": {
"course_key": "edX/Demo_101/Fall",
"deadline": "2015-10-23"
}
**Responses:**
* 200 OK: The request was created successfully.
* 404 Not Found: The provider does not exist.
"""
course_key = request.GET.get("course_key", None)
username = request.GET.get("username", None)
return JsonResponse(api.get_eligibilities_for_user(username=username, course_key=course_key))
def _validate_json_parameters(params_string, expected_parameters): def _validate_json_parameters(params_string, expected_parameters):
""" """
Load the request parameters as a JSON dictionary and check that Load the request parameters as a JSON dictionary and check that
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment