Skip to content

Commit

Permalink
Added mettl code
Browse files Browse the repository at this point in the history
  • Loading branch information
Chandana3008 committed Jul 11, 2024
1 parent eb97ccd commit e573f9e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions common/djangoapps/student/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

re_path(r'^extras/reset_password_link', views.extras_reset_password_link, name = "extras_reset_password_link"),

re_path(r'^extras/start_mettl_test', views.extras_start_mettl_test, name = "extras_start_mettl_test"),

re_path(r'^extras/get_user_enrolled_courses', views.extras_get_user_enrolled_courses, name = "extras_get_user_enrolled_courses"),

re_path(r'^email_confirm/(?P<key>[^/]*)$', views.confirm_email_change, name='confirm_email_change'),
Expand Down
33 changes: 33 additions & 0 deletions common/djangoapps/student/views/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
import requests
import json

import time
import hmac
import base64
import hashlib
import pytz

from collections import namedtuple
Expand Down Expand Up @@ -1363,3 +1367,32 @@ def user_assessments_tracker_link(request):
log.info(output_dict)
return render(request, 'user_assessment_tracker_link.html', {'data': output_dict, 'program_image_url': configuration_helpers.get_value("MKTG_URLS", True)["HEADER_LOGO"]})

@login_required
def extras_start_mettl_test(request):
test_id = request.GET["test_id"]
HTTPVerb = "POST"
URL = "https://api.mettl.com/v1/schedules/" + test_id + "/candidates"
#PUBLICKEY = "95d3af46-0bd7-4610-a759-b46060e80760"
#PRIVATEKEY = "beedfbbe-b1a8-47c7-aaa8-f11843d29393"
PUBLICKEY = configuration_helpers.get_value("METTL_PUBLIC_KEY", "3c57d2f3-dd6c-4c10-9407-2f56a0b7ec1f")
PRIVATEKEY = configuration_helpers.get_value("METTL_PRIVATE_KEY", "bc9dac7c-04e5-4602-af62-ccc4894dc864")
registration_details = json.dumps({"registrationDetails":[{"Email Address" : request.user.email, "First Name" : request.user.username}]})
timestamp = str(int(time.time()))
message = HTTPVerb + URL + '\n' + PUBLICKEY + '\n' + registration_details + '\n' + timestamp
sign = urllib.parse.quote(str(base64.b64encode(hmac.new(bytes(PRIVATEKEY, 'UTF-8') ,bytes(message, 'UTF-8') , digestmod=hashlib.sha1).digest()), "utf-8"))
#sign = str(base64.b64encode(hmac.new(bytes(PRIVATEKEY, encoding='utf8'), bytes(message, encoding='utf8'), digestmod = hashlib.sha1).digest())).replace('+', '%2B')

headers={"Content-Type" : "application/x-www-form-urlencoded"}
payload = {"rd" : registration_details, "ak" : PUBLICKEY, "asgn" : sign, "ts" : timestamp}
response = requests.post(URL, headers = headers, data = payload).json()
log.error(payload)
log.error(response)
if response["status"] == "SUCCESS":
if response["registrationStatus"][0]["url"] is not None:
return redirect(response["registrationStatus"][0]["url"])
elif response["registrationStatus"][0]["status"] == "Completed":
return render(request, 'mettl_test_status.html', {"message" : "You have already completed your exam.Thank you!"})
elif response["registrationStatus"][0]["status"] == "InValid":
return render(request, 'mettl_test_status.html',{"message" : configuration_helpers.get_value("METTL_ERROR_MESSAGE", "You are not allowed to attend this exam owing to insufficient attendance. Please contact Support team")})

return render(request, 'mettl_test_status.html', {"message" : "Uh ho! Something went wrong. Please contact support <br><a href=mailto:{0}>{0}</a>".format(configuration_helpers.get_value("contact_mailing_address", ""))})
37 changes: 37 additions & 0 deletions lms/templates/mettl_test_status.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!-- <%page expression_filter="h"/>
<%namespace name='static' file='static_content.html'/>
<%!
from django.utils.translation import ugettext as _
from openedx.core.djangolib.markup import HTML, Text
%>
<%inherit file="main.html" />
-->


<style>
.confirm{

color: #00a651;
font-size: 50px;

}
.confirmation
{
margin: 20% 0;
text-align: center;
}
.main_link{
text-align: center;
font-size: 20px;
color: #ff5722;
text-decoration: none;
border-bottom:1px dashed #ff5722;
}
</style>


<div class="confirmation">
<h1 class="confirm">{{message | safe}}</h1>
<a href="/dashboard" class="main_link">Go back to Dashboard</a>
</div>

0 comments on commit e573f9e

Please sign in to comment.