Commit e7f15989 by Omar Al-Ithawi

Bayt integration

Conflicts:
	lms/envs/aws.py
	lms/templates/dashboard.html
parent 1ba53506
"""
Edraak.org and bayt integration.
"""
# -*- coding: utf-8 -*-
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
def forwards(self, orm):
# Adding model 'BaytPublishedCertificate'
db.create_table('edraak_bayt_baytpublishedcertificate', (
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('user_id', self.gf('django.db.models.fields.IntegerField')(db_index=True, null=True, blank=True)),
('course_id', self.gf('django.db.models.fields.CharField')(db_index=True, max_length=255, null=True, blank=True)),
))
db.send_create_signal('edraak_bayt', ['BaytPublishedCertificate'])
def backwards(self, orm):
# Deleting model 'BaytPublishedCertificate'
db.delete_table('edraak_bayt_baytpublishedcertificate')
models = {
'edraak_bayt.baytpublishedcertificate': {
'Meta': {'object_name': 'BaytPublishedCertificate'},
'course_id': ('django.db.models.fields.CharField', [], {'db_index': 'True', 'max_length': '255', 'null': 'True', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'user_id': ('django.db.models.fields.IntegerField', [], {'db_index': 'True', 'null': 'True', 'blank': 'True'})
}
}
complete_apps = ['edraak_bayt']
\ No newline at end of file
from django.db import models
class BaytPublishedCertificate(models.Model):
user_id = models.IntegerField(blank=True, null=True, db_index=True)
course_id = models.CharField(blank=True, null=True, db_index=True, max_length=255)
\ No newline at end of file
from django.conf import settings
from django.conf.urls import patterns, url
from django.core.urlresolvers import LocaleRegexURLResolver
urlpatterns = patterns('',
url(r'^get_student_email_for_bayt$', 'edraak_bayt.views.get_student_email', name="edraak_bayt_get_student_email"),
url(r'^bayt-activation$', 'edraak_bayt.views.activation', name="bayt_activation"),
)
from .models import BaytPublishedCertificate
def check_user_publish(user_id, course_id):
if BaytPublishedCertificate.objects.filter(user_id=user_id, course_id=course_id).count() > 0:
return True
else:
return False
\ No newline at end of file
import hashlib
import urllib
from django.http import HttpResponseServerError
import simplejson
from django.conf import settings
from django.contrib.auth.models import User
from django.core.mail import send_mail
from django.utils.translation import ugettext as _
from django.views.decorators.http import require_POST
from django.contrib.auth.decorators import login_required
from httplib2 import Http
import logging
from django.core.validators import validate_email
from django.core.exceptions import ValidationError
from simplejson.scanner import JSONDecodeError
from edxmako.shortcuts import render_to_response, render_to_string
from util.json_request import JsonResponse
from microsite_configuration import microsite
from .models import BaytPublishedCertificate
log = logging.getLogger(__name__)
@require_POST
@login_required
def get_student_email(request):
user_email = request.POST['bayt_email']
course_name = request.POST['course_name']
course_id = request.POST['course_id']
user_id = request.user.id
try:
validate_email(user_email)
except ValidationError:
return JsonResponse({"success": False, "error": _('Invalid Email')})
try:
user = User.objects.get(id=user_id)
except User.DoesNotExist:
## close the pop-up because there is sth wrong
return JsonResponse({"success": False, "error": 'Invalid ID'})
if user.email == user_email:
h = Http()
param = {
'secret_key': settings.BAYT_SECRET_KEY,
'valid_until': '06-2015',
'certificate_name': course_name.encode('UTF-8'),
'email_address': user_email
}
url = "https://api.bayt.com/api/edraak-api/post.adp?" + urllib.urlencode(param)
print url
resp, content = h.request(url)
json_content = simplejson.loads(content)
if json_content['status'] == "NOT EXISTS":
return JsonResponse({"success": True, "error": False, "redirect_to": True, "response": content})
else:
BaytPublishedCertificate.objects.create(user_id=int(user_id), course_id=course_id)
return JsonResponse({"success": True, "error": False, "redirect_to": False, "response": content})
else:
secret_key = settings.BAYT_SECRET_KEY
my_string = user_email + course_name + secret_key
access_token = hashlib.md5(my_string.encode('UTF-8')).hexdigest()
param = {
'email': user_email,
'course_name': course_name.encode('UTF-8'),
'access_token': access_token,
'course_id': course_id.encode('UTF-8'),
'user_id': user_id
}
if not settings.DEBUG:
url = "https://edraak.org/bayt-activation?" + urllib.urlencode(param)
else:
url = request.META['HTTP_HOST'] + "/bayt-activation?" + urllib.urlencode(param)
context = {
'encoded_url': url
}
message = render_to_string('bayt/verification_email.txt', context)
if not (settings.FEATURES.get('AUTOMATIC_AUTH_FOR_TESTING')):
from_address = microsite.get_value('email_from_address', settings.DEFAULT_FROM_EMAIL)
try:
send_mail('Bayt Edraak Verification', message, from_address, [user_email], fail_silently=False)
# here we have to send an auth email to user email that contains a link to
# get back to here and then post the certificate
return JsonResponse({"success": True, "error": False, "redirect_to": False})
except:
log.warning('Unable to send activation email to user', exc_info=True)
return JsonResponse({"success": False, "error": True, "redirect_to": False})
else:
return JsonResponse({"success": False, "error": True, "redirect_to": False})
@login_required
def activation(request):
access_token = request.GET['access_token']
user_email = request.GET['email']
course_name = request.GET['course_name']
course_id = request.GET['course_id']
user_id = request.user.id
secret_key = settings.BAYT_SECRET_KEY
my_string = user_email + course_name + secret_key
current_access_token = hashlib.md5(my_string.encode('UTF-8')).hexdigest()
if current_access_token == access_token:
h = Http()
param = {
'secret_key': settings.BAYT_SECRET_KEY,
'valid_until': '06-2015',
'certificate_name': course_name.encode('UTF-8'),
'email_address': user_email
}
url = "https://api.bayt.com/api/edraak-api/post.adp?" + urllib.urlencode(param)
resp, content = h.request(url)
json_content = simplejson.loads(content)
if json_content['status'] == "NOT EXISTS":
# redirect user to bayt registration
return render_to_response("bayt/callback.html", {"status": False})
BaytPublishedCertificate.objects.create(user_id=user_id, course_id=course_id)
return render_to_response("bayt/callback.html", {"status": True})
else:
return JsonResponse({"success": False, "error": True, "redirect_to": False})
......@@ -17,4 +17,5 @@ def i18n_patterns(prefix, *args):
urlpatterns = patterns('',
url(r'^setlang/$', 'edraak_misc.views.set_language', name='edraak_setlang'),
url(r'^check_student_grades/$', 'edraak_misc.views.check_student_grades', name='edraak_check_student_grades'),
)
from courseware.access import has_access
from django.conf import settings
def is_certificate_allowed(user, course):
return (course.has_ended()
and settings.FEATURES.get('ENABLE_ISSUE_CERTIFICATE')
or has_access(user, 'staff', course.id))
from django.views.i18n import set_language as django_set_language
from django.views.decorators.csrf import csrf_exempt
from xmodule.modulestore.django import modulestore
from courseware.access import has_access
from util.json_request import JsonResponse
from courseware.grades import grade
from opaque_keys.edx import locator
@csrf_exempt
def set_language(request):
return django_set_language(request)
\ No newline at end of file
return django_set_language(request)
def check_student_grades(request):
user = request.user
course_id = request.POST['course_id']
course_key = locator.CourseLocator.from_string(course_id)
course = modulestore().get_course(course_key)
# If user is course staff don't grade the user
if has_access(user, 'staff', course):
request.session['course_pass_%s' % course_id] = True
return JsonResponse({'success': True, 'error': False})
try:
if grade(user, request, course)['grade']:
request.session['course_pass_%s' % course_id] = True
return JsonResponse({'success': True, 'error': False})
else:
return JsonResponse({'success': False, 'error': False})
except:
return JsonResponse({'success': False, 'error': True})
\ No newline at end of file
......@@ -467,6 +467,9 @@ OPTIMIZELY_PROJECT_ID = AUTH_TOKENS.get('OPTIMIZELY_PROJECT_ID', OPTIMIZELY_PROJ
#### Course Registration Code length ####
REGISTRATION_CODE_LENGTH = ENV_TOKENS.get('REGISTRATION_CODE_LENGTH', 8)
# BAYT KEYS
BAYT_SECRET_KEY = AUTH_TOKENS.get("BAYT_SECRET_KEY")
# REGISTRATION CODES DISPLAY INFORMATION
INVOICE_CORP_ADDRESS = ENV_TOKENS.get('INVOICE_CORP_ADDRESS', INVOICE_CORP_ADDRESS)
INVOICE_PAYMENT_INSTRUCTIONS = ENV_TOKENS.get('INVOICE_PAYMENT_INSTRUCTIONS', INVOICE_PAYMENT_INSTRUCTIONS)
......@@ -1479,6 +1479,7 @@ INSTALLED_APPS = (
# Edraak specific modules
'edraak_misc',
'edraak_bayt',
)
######################### MARKETING SITE ###############################
......
## Just an empty placeholder
## Actual implementation is available in Edraak theme
## Just an empty placeholder
## Actual implementation is available in Edraak theme
## Just an empty placeholder
## Actual implementation is available in Edraak theme
## Just an empty placeholder
## Actual implementation is available in Edraak theme
......@@ -394,3 +394,14 @@
</form>
</div>
</section>
<script>
$(function() {
$("#unregister_block_course").click( function(event) {
$("#unenroll_course_id").val( $(event.target).data("course-id") );
$("#unenroll_course_number").text( $(event.target).data("course-number") );
});
});
</script>
## Available in Edraak theme
<%include file="bayt/_dashboard_popups.html" />
......@@ -224,6 +224,8 @@
% endif
% endif
## Available in Edraak theme
<%include file="../bayt/_dashboard_certificate_link.html" args="course=course" />
</section>
......
......@@ -69,6 +69,7 @@ urlpatterns = ('', # nopep8
url(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^edraak/', include('edraak_misc.urls')),
url(r'', include('edraak_bayt.urls')),
url(r'^embargo$', 'student.views.embargo', name="embargo"),
......
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