diff --git a/common/djangoapps/student/admin.py b/common/djangoapps/student/admin.py index bceea4f4b9..bea29aab9d 100644 --- a/common/djangoapps/student/admin.py +++ b/common/djangoapps/student/admin.py @@ -5,7 +5,9 @@ from django import forms from config_models.admin import ConfigurationModelAdmin from student.models import UserProfile, UserTestGroup, CourseEnrollmentAllowed, DashboardConfiguration -from student.models import CourseEnrollment, Registration, PendingNameChange, CourseAccessRole +from student.models import ( + CourseEnrollment, Registration, PendingNameChange, CourseAccessRole, LinkedInAddToProfileConfiguration +) from ratelimitbackend import admin from student.roles import REGISTERED_ACCESS_ROLES @@ -42,3 +44,5 @@ admin.site.register(PendingNameChange) admin.site.register(CourseAccessRole, CourseAccessRoleAdmin) admin.site.register(DashboardConfiguration, ConfigurationModelAdmin) + +admin.site.register(LinkedInAddToProfileConfiguration) diff --git a/common/djangoapps/student/migrations/0043_auto__add_linkedinaddtoprofileconfiguration.py b/common/djangoapps/student/migrations/0043_auto__add_linkedinaddtoprofileconfiguration.py new file mode 100644 index 0000000000..41ec46effd --- /dev/null +++ b/common/djangoapps/student/migrations/0043_auto__add_linkedinaddtoprofileconfiguration.py @@ -0,0 +1,187 @@ +# -*- 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 'LinkedInAddToProfileConfiguration' + db.create_table('student_linkedinaddtoprofileconfiguration', ( + ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), + ('change_date', self.gf('django.db.models.fields.DateTimeField')(auto_now_add=True, blank=True)), + ('changed_by', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['auth.User'], null=True, on_delete=models.PROTECT)), + ('enabled', self.gf('django.db.models.fields.BooleanField')(default=False)), + ('dashboard_tracking_code', self.gf('django.db.models.fields.TextField')(blank=True)), + )) + db.send_create_signal('student', ['LinkedInAddToProfileConfiguration']) + + + def backwards(self, orm): + # Deleting model 'LinkedInAddToProfileConfiguration' + db.delete_table('student_linkedinaddtoprofileconfiguration') + + + models = { + 'auth.group': { + 'Meta': {'object_name': 'Group'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}), + 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}) + }, + 'auth.permission': { + 'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'}, + 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}) + }, + 'auth.user': { + 'Meta': {'object_name': 'User'}, + 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), + 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}), + 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), + 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), + 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), + 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}), + 'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}), + 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'}) + }, + 'contenttypes.contenttype': { + 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"}, + 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}) + }, + 'student.anonymoususerid': { + 'Meta': {'object_name': 'AnonymousUserId'}, + 'anonymous_user_id': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '32'}), + 'course_id': ('xmodule_django.models.CourseKeyField', [], {'db_index': 'True', 'max_length': '255', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"}) + }, + 'student.courseaccessrole': { + 'Meta': {'unique_together': "(('user', 'org', 'course_id', 'role'),)", 'object_name': 'CourseAccessRole'}, + 'course_id': ('xmodule_django.models.CourseKeyField', [], {'db_index': 'True', 'max_length': '255', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'org': ('django.db.models.fields.CharField', [], {'db_index': 'True', 'max_length': '64', 'blank': 'True'}), + 'role': ('django.db.models.fields.CharField', [], {'max_length': '64', 'db_index': 'True'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"}) + }, + 'student.courseenrollment': { + 'Meta': {'ordering': "('user', 'course_id')", 'unique_together': "(('user', 'course_id'),)", 'object_name': 'CourseEnrollment'}, + 'course_id': ('xmodule_django.models.CourseKeyField', [], {'max_length': '255', 'db_index': 'True'}), + 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'null': 'True', 'db_index': 'True', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'mode': ('django.db.models.fields.CharField', [], {'default': "'honor'", 'max_length': '100'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"}) + }, + 'student.courseenrollmentallowed': { + 'Meta': {'unique_together': "(('email', 'course_id'),)", 'object_name': 'CourseEnrollmentAllowed'}, + 'auto_enroll': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'course_id': ('xmodule_django.models.CourseKeyField', [], {'max_length': '255', 'db_index': 'True'}), + 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'null': 'True', 'db_index': 'True', 'blank': 'True'}), + 'email': ('django.db.models.fields.CharField', [], {'max_length': '255', 'db_index': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}) + }, + 'student.dashboardconfiguration': { + 'Meta': {'object_name': 'DashboardConfiguration'}, + 'change_date': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'changed_by': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'null': 'True', 'on_delete': 'models.PROTECT'}), + 'enabled': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'recent_enrollment_time_delta': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}) + }, + 'student.linkedinaddtoprofileconfiguration': { + 'Meta': {'object_name': 'LinkedInAddToProfileConfiguration'}, + 'change_date': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'changed_by': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'null': 'True', 'on_delete': 'models.PROTECT'}), + 'dashboard_tracking_code': ('django.db.models.fields.TextField', [], {'blank': 'True'}), + 'enabled': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}) + }, + 'student.loginfailures': { + 'Meta': {'object_name': 'LoginFailures'}, + 'failure_count': ('django.db.models.fields.IntegerField', [], {'default': '0'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'lockout_until': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"}) + }, + 'student.passwordhistory': { + 'Meta': {'object_name': 'PasswordHistory'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}), + 'time_set': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"}) + }, + 'student.pendingemailchange': { + 'Meta': {'object_name': 'PendingEmailChange'}, + 'activation_key': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '32', 'db_index': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'new_email': ('django.db.models.fields.CharField', [], {'db_index': 'True', 'max_length': '255', 'blank': 'True'}), + 'user': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['auth.User']", 'unique': 'True'}) + }, + 'student.pendingnamechange': { + 'Meta': {'object_name': 'PendingNameChange'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'new_name': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}), + 'rationale': ('django.db.models.fields.CharField', [], {'max_length': '1024', 'blank': 'True'}), + 'user': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['auth.User']", 'unique': 'True'}) + }, + 'student.registration': { + 'Meta': {'object_name': 'Registration', 'db_table': "'auth_registration'"}, + 'activation_key': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '32', 'db_index': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'unique': 'True'}) + }, + 'student.userprofile': { + 'Meta': {'object_name': 'UserProfile', 'db_table': "'auth_userprofile'"}, + 'allow_certificate': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'city': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), + 'country': ('django_countries.fields.CountryField', [], {'max_length': '2', 'null': 'True', 'blank': 'True'}), + 'courseware': ('django.db.models.fields.CharField', [], {'default': "'course.xml'", 'max_length': '255', 'blank': 'True'}), + 'gender': ('django.db.models.fields.CharField', [], {'db_index': 'True', 'max_length': '6', 'null': 'True', 'blank': 'True'}), + 'goals': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'language': ('django.db.models.fields.CharField', [], {'db_index': 'True', 'max_length': '255', 'blank': 'True'}), + 'level_of_education': ('django.db.models.fields.CharField', [], {'db_index': 'True', 'max_length': '6', 'null': 'True', 'blank': 'True'}), + 'location': ('django.db.models.fields.CharField', [], {'db_index': 'True', 'max_length': '255', 'blank': 'True'}), + 'mailing_address': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), + 'meta': ('django.db.models.fields.TextField', [], {'blank': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'db_index': 'True', 'max_length': '255', 'blank': 'True'}), + 'user': ('django.db.models.fields.related.OneToOneField', [], {'related_name': "'profile'", 'unique': 'True', 'to': "orm['auth.User']"}), + 'year_of_birth': ('django.db.models.fields.IntegerField', [], {'db_index': 'True', 'null': 'True', 'blank': 'True'}) + }, + 'student.usersignupsource': { + 'Meta': {'object_name': 'UserSignupSource'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'site': ('django.db.models.fields.CharField', [], {'max_length': '255', 'db_index': 'True'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"}) + }, + 'student.userstanding': { + 'Meta': {'object_name': 'UserStanding'}, + 'account_status': ('django.db.models.fields.CharField', [], {'max_length': '31', 'blank': 'True'}), + 'changed_by': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'standing_last_changed_at': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'standing'", 'unique': 'True', 'to': "orm['auth.User']"}) + }, + 'student.usertestgroup': { + 'Meta': {'object_name': 'UserTestGroup'}, + 'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '32', 'db_index': 'True'}), + 'users': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.User']", 'db_index': 'True', 'symmetrical': 'False'}) + } + } + + complete_apps = ['student'] \ No newline at end of file diff --git a/common/djangoapps/student/models.py b/common/djangoapps/student/models.py index 227a97dd48..6631a2d5a4 100644 --- a/common/djangoapps/student/models.py +++ b/common/djangoapps/student/models.py @@ -20,7 +20,9 @@ from collections import defaultdict import dogstats_wrapper as dog_stats_api from django.db.models import Q import pytz +from urllib import urlencode +from django.utils.translation import ugettext_lazy from django.conf import settings from django.utils import timezone from django.contrib.auth.models import User @@ -1435,3 +1437,33 @@ class DashboardConfiguration(ConfigurationModel): @property def recent_enrollment_seconds(self): return self.recent_enrollment_time_delta + + +class LinkedInAddToProfileConfiguration(ConfigurationModel): + """ + LinkedIn Add to Profile Configuration + """ + # tracking code field + dashboard_tracking_code = models.TextField( + blank=True, + help_text=ugettext_lazy( + u"A dashboard tracking code field for LinkedIn Add-to-profile Certificates. " + u"e.g 0_0dPSPyS070e0HsE9HNz_13_d11_" + ) + ) + + @classmethod + def linked_in_dashboard_tracking_code_url(cls, params): + """ + Get the linked-in Configuration. + """ + config = cls.current() + if config.enabled: + return u'http://www.linkedin.com/profile/add?_ed={tracking_code}&{params}'.format( + tracking_code=config.dashboard_tracking_code, + params=urlencode(params) + ) + return None + + def __unicode__(self): + return self.dashboard_tracking_code diff --git a/common/djangoapps/student/tests/tests.py b/common/djangoapps/student/tests/tests.py index 4a4a619ca5..445e22ea67 100644 --- a/common/djangoapps/student/tests/tests.py +++ b/common/djangoapps/student/tests/tests.py @@ -21,7 +21,8 @@ from mock import Mock, patch from opaque_keys.edx.locations import SlashSeparatedCourseKey from student.models import ( - anonymous_id_for_user, user_by_anonymous_id, CourseEnrollment, unique_id_for_user + anonymous_id_for_user, user_by_anonymous_id, CourseEnrollment, unique_id_for_user, + LinkedInAddToProfileConfiguration ) from student.views import (process_survey_link, _cert_info, change_enrollment, complete_course_mode_info) @@ -37,6 +38,8 @@ from certificates.tests.factories import GeneratedCertificateFactory # pylint: from verify_student.models import SoftwareSecurePhotoVerification import shoppingcart # pylint: disable=import-error +# Explicitly import the cache from ConfigurationModel so we can reset it after each test +from config_models.models import cache log = logging.getLogger(__name__) @@ -59,9 +62,10 @@ class CourseEndingTest(TestCase): user = Mock(username="fred") survey_url = "http://a_survey.com" course = Mock(end_of_course_survey_url=survey_url, certificates_display_behavior='end') + course_mode = 'honor' self.assertEqual( - _cert_info(user, course, None), + _cert_info(user, course, None, course_mode), { 'status': 'processing', 'show_disabled_download_button': False, @@ -72,19 +76,20 @@ class CourseEndingTest(TestCase): cert_status = {'status': 'unavailable'} self.assertEqual( - _cert_info(user, course, cert_status), + _cert_info(user, course, cert_status, course_mode), { 'status': 'processing', 'show_disabled_download_button': False, 'show_download_url': False, 'show_survey_button': False, - 'mode': None + 'mode': None, + 'linked_in_url': None } ) cert_status = {'status': 'generating', 'grade': '67', 'mode': 'honor'} self.assertEqual( - _cert_info(user, course, cert_status), + _cert_info(user, course, cert_status, course_mode), { 'status': 'generating', 'show_disabled_download_button': True, @@ -92,13 +97,14 @@ class CourseEndingTest(TestCase): 'show_survey_button': True, 'survey_url': survey_url, 'grade': '67', - 'mode': 'honor' + 'mode': 'honor', + 'linked_in_url': None } ) cert_status = {'status': 'regenerating', 'grade': '67', 'mode': 'verified'} self.assertEqual( - _cert_info(user, course, cert_status), + _cert_info(user, course, cert_status, course_mode), { 'status': 'generating', 'show_disabled_download_button': True, @@ -106,7 +112,8 @@ class CourseEndingTest(TestCase): 'show_survey_button': True, 'survey_url': survey_url, 'grade': '67', - 'mode': 'verified' + 'mode': 'verified', + 'linked_in_url': None } ) @@ -116,8 +123,9 @@ class CourseEndingTest(TestCase): 'download_url': download_url, 'mode': 'honor' } + self.assertEqual( - _cert_info(user, course, cert_status), + _cert_info(user, course, cert_status, course_mode), { 'status': 'ready', 'show_disabled_download_button': False, @@ -126,7 +134,8 @@ class CourseEndingTest(TestCase): 'show_survey_button': True, 'survey_url': survey_url, 'grade': '67', - 'mode': 'honor' + 'mode': 'honor', + 'linked_in_url': None } ) @@ -136,7 +145,7 @@ class CourseEndingTest(TestCase): 'mode': 'honor' } self.assertEqual( - _cert_info(user, course, cert_status), + _cert_info(user, course, cert_status, course_mode), { 'status': 'notpassing', 'show_disabled_download_button': False, @@ -144,7 +153,8 @@ class CourseEndingTest(TestCase): 'show_survey_button': True, 'survey_url': survey_url, 'grade': '67', - 'mode': 'honor' + 'mode': 'honor', + 'linked_in_url': None } ) @@ -155,28 +165,97 @@ class CourseEndingTest(TestCase): 'download_url': download_url, 'mode': 'honor' } self.assertEqual( - _cert_info(user, course2, cert_status), + _cert_info(user, course2, cert_status, course_mode), { 'status': 'notpassing', 'show_disabled_download_button': False, 'show_download_url': False, 'show_survey_button': False, 'grade': '67', - 'mode': 'honor' + 'mode': 'honor', + 'linked_in_url': None } ) # test when the display is unavailable or notpassing, we get the correct results out course2.certificates_display_behavior = 'early_no_info' cert_status = {'status': 'unavailable'} - self.assertIsNone(_cert_info(user, course2, cert_status)) + self.assertIsNone(_cert_info(user, course2, cert_status, course_mode)) cert_status = { 'status': 'notpassing', 'grade': '67', 'download_url': download_url, 'mode': 'honor' } - self.assertIsNone(_cert_info(user, course2, cert_status)) + self.assertIsNone(_cert_info(user, course2, cert_status, course_mode)) + + def test_linked_in_url_with_unicode_course_display_name(self): + """Test with unicode display name values.""" + + user = Mock(username="fred") + survey_url = "http://a_survey.com" + course = Mock(end_of_course_survey_url=survey_url, certificates_display_behavior='end') + course.display_name = u'edx/abc/courseregisters®' + download_url = 'http://s3.edx/cert' + + cert_status = { + 'status': 'downloadable', 'grade': '67', + 'download_url': download_url, + 'mode': 'honor' + } + LinkedInAddToProfileConfiguration( + dashboard_tracking_code='0_mC_o2MizqdtZEmkVXjH4eYwMj4DnkCWrZP_D9', + enabled=True).save() + + status_dict = _cert_info(user, course, cert_status, 'honor') + self.assertIn( + 'http://www.linkedin.com/profile/add?_ed=0_mC_o2MizqdtZEmkVXjH4eYwMj4DnkCWrZP_D9', + status_dict['linked_in_url'] + ) + self.assertIn('pfCertificationName', status_dict['linked_in_url']) + self.assertIn('pfCertificationUrl', status_dict['linked_in_url']) + self.assertIn('courseregisters', status_dict['linked_in_url']) + self.assertIn('Honor+Code+Certificate', status_dict['linked_in_url']) + + def test_linked_in_url_not_exists_without_config(self): + # Test case with Linked-In URL empty with if linked-in-config is none. + cache.clear() + user = Mock(username="fred") + survey_url = "http://a_survey.com" + course = Mock(end_of_course_survey_url=survey_url, certificates_display_behavior='end') + + download_url = 'http://s3.edx/cert' + cert_status = { + 'status': 'downloadable', 'grade': '67', + 'download_url': download_url, + 'mode': 'verified' + } + + self.assertEqual( + _cert_info(user, course, cert_status, 'verified'), + { + 'status': 'ready', + 'show_disabled_download_button': False, + 'show_download_url': True, + 'download_url': download_url, + 'show_survey_button': True, + 'survey_url': survey_url, + 'grade': '67', + 'mode': 'verified', + 'linked_in_url': None + } + ) + + # adding config. linked-in-url will be return + LinkedInAddToProfileConfiguration( + dashboard_tracking_code='0_mC_o2MizqdtZEmkVXjH4eYwMj4DnkCWrZP_D9', + enabled=True).save() + + status_dict = _cert_info(user, course, cert_status, 'honor') + self.assertIn( + 'http://www.linkedin.com/profile/add?_ed=0_mC_o2MizqdtZEmkVXjH4eYwMj4DnkCWrZP_D9', + status_dict['linked_in_url'] + ) class DashboardTest(ModuleStoreTestCase): @@ -189,6 +268,7 @@ class DashboardTest(ModuleStoreTestCase): self.course = CourseFactory.create() self.user = UserFactory.create(username="jack", email="jack@fake.edx.org", password='test') self.client = Client() + cache.clear() @unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms') def _check_verification_status_on(self, mode, value): @@ -369,6 +449,7 @@ class DashboardTest(ModuleStoreTestCase): mode_display_name='Verified', expiration_datetime=datetime.now(pytz.UTC) + timedelta(days=1) ) + enrollment = CourseEnrollment.enroll(self.user, self.course.id, mode='verified') self.assertTrue(enrollment.refundable()) @@ -382,6 +463,94 @@ class DashboardTest(ModuleStoreTestCase): self.assertFalse(enrollment.refundable()) + @unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms') + def test_linked_in_add_to_profile_btn_not_appearing_without_config(self): + + """ + without linked-in config don't show Add Certificate to LinkedIn button + """ + self.client.login(username="jack", password="test") + + CourseModeFactory.create( + course_id=self.course.id, + mode_slug='verified', + mode_display_name='verified', + expiration_datetime=datetime.now(pytz.UTC) - timedelta(days=1) + ) + + CourseEnrollment.enroll(self.user, self.course.id, mode='honor') + + self.course.start = datetime.now(pytz.UTC) - timedelta(days=2) + self.course.end = datetime.now(pytz.UTC) - timedelta(days=1) + self.course.display_name = u"Omega" + self.course = self.update_course(self.course, self.user.id) + + download_url = 'www.edx.org' + GeneratedCertificateFactory.create( + user=self.user, + course_id=self.course.id, + status=CertificateStatuses.downloadable, + mode='honor', + grade='67', + download_url=download_url + ) + response = self.client.get(reverse('dashboard')) + + self.assertEquals(response.status_code, 200) + self.assertNotIn('Add Certificate to LinkedIn', response.content) + + response_url = 'http://www.linkedin.com/profile/add?_ed=' + self.assertNotContains(response, response_url) + + @unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms') + def test_linked_in_add_to_profile_btn_with_certificate(self): + + """ + If user has a certificate with valid linked-in config then Add Certificate to LinkedIn button + should be visible. and it has URL value with valid parameters. + """ + + self.client.login(username="jack", password="test") + tracking_code = '0_mC_o2MizqdtZEmkVXjH4eYwMj4DnkCWrZP_D9' + LinkedInAddToProfileConfiguration(dashboard_tracking_code=tracking_code, enabled=True).save() + + CourseModeFactory.create( + course_id=self.course.id, + mode_slug='verified', + mode_display_name='verified', + expiration_datetime=datetime.now(pytz.UTC) - timedelta(days=1) + ) + + CourseEnrollment.enroll(self.user, self.course.id, mode='honor') + + self.course.start = datetime.now(pytz.UTC) - timedelta(days=2) + self.course.end = datetime.now(pytz.UTC) - timedelta(days=1) + self.course.display_name = u"Omega" + self.course = self.update_course(self.course, self.user.id) + + download_url = 'www.edx.org' + GeneratedCertificateFactory.create( + user=self.user, + course_id=self.course.id, + status=CertificateStatuses.downloadable, + mode='honor', + grade='67', + download_url=download_url + ) + response = self.client.get(reverse('dashboard')) + + self.assertEquals(response.status_code, 200) + self.assertIn('Add Certificate to LinkedIn', response.content) + + response_url = ( + 'http://www.linkedin.com/profile/add?_ed=' + '{tracking_code}&pfCertificationUrl={download}&pfCertificationName=' + 'Honor+Code+Certificate+for+{name}' + ).format( + tracking_code=tracking_code, download=download_url, name='Omega' + ) + self.assertContains(response, response_url) + class EnrollInCourseTest(TestCase): """Tests enrolling and unenrolling in courses.""" diff --git a/common/djangoapps/student/views.py b/common/djangoapps/student/views.py index 4abf62ff55..1441a20cd8 100644 --- a/common/djangoapps/student/views.py +++ b/common/djangoapps/student/views.py @@ -54,7 +54,7 @@ from student.models import ( PendingEmailChange, CourseEnrollment, unique_id_for_user, CourseEnrollmentAllowed, UserStanding, LoginFailures, create_comments_service_user, PasswordHistory, UserSignupSource, - DashboardConfiguration) + DashboardConfiguration, LinkedInAddToProfileConfiguration) from student.forms import PasswordResetFormNoActive from verify_student.models import SoftwareSecurePhotoVerification, MidcourseReverificationWindow @@ -187,7 +187,7 @@ def process_survey_link(survey_link, user): return survey_link.format(UNIQUE_ID=unique_id_for_user(user)) -def cert_info(user, course): +def cert_info(user, course, course_mode): """ Get the certificate info needed to render the dashboard section for the given student and course. Returns a dictionary with keys: @@ -203,7 +203,7 @@ def cert_info(user, course): if not course.may_certify(): return {} - return _cert_info(user, course, certificate_status_for_student(user, course.id)) + return _cert_info(user, course, certificate_status_for_student(user, course.id), course_mode) def reverification_info(course_enrollment_pairs, user, statuses): @@ -293,7 +293,7 @@ def get_course_enrollment_pairs(user, course_org_filter, org_filter_out_set): ) -def _cert_info(user, course, cert_status): +def _cert_info(user, course, cert_status, course_mode): """ Implements the logic for cert_info -- split out for testing. """ @@ -328,7 +328,8 @@ def _cert_info(user, course, cert_status): 'status': status, 'show_download_url': status == 'ready', 'show_disabled_download_button': status == 'generating', - 'mode': cert_status.get('mode', None) + 'mode': cert_status.get('mode', None), + 'linked_in_url': None } if (status in ('generating', 'ready', 'notpassing', 'restricted') and @@ -350,6 +351,31 @@ def _cert_info(user, course, cert_status): else: status_dict['download_url'] = cert_status['download_url'] + # getting linkedin URL and then pass the params which appears + # on user profile. if linkedin config is empty don't show the button. + + modes_dict = { + "honor": "Honor Code Certificate", + "verified": "Verified Certificate", + "professional": "Professional Certificate", + } + + certification_name = u'{type} for {course_name}'.format( + type=modes_dict.get(course_mode, "Certificate"), course_name=course.display_name + ).encode('utf-8') + + params_dict = { + 'pfCertificationName': certification_name, + 'pfCertificationUrl': cert_status['download_url'], + } + + # following method will construct and return url if current enabled config exists otherwise return None + # In case of None linked-in-button will not appear on dashboard. + + status_dict['linked_in_url'] = LinkedInAddToProfileConfiguration.linked_in_dashboard_tracking_code_url( + params_dict + ) + if status in ('generating', 'ready', 'notpassing', 'restricted'): if 'grade' not in cert_status: # Note: as of 11/20/2012, we know there are students in this state-- cs169.1x, @@ -583,9 +609,8 @@ def dashboard(request): course_enrollment_pairs, all_course_modes ) - cert_statuses = { - course.id: cert_info(request.user, course) + course.id: cert_info(request.user, course, _enrollment.mode) for course, _enrollment in course_enrollment_pairs } diff --git a/lms/templates/dashboard/_dashboard_certificate_information.html b/lms/templates/dashboard/_dashboard_certificate_information.html index 951091f371..d9ff7eaa11 100644 --- a/lms/templates/dashboard/_dashboard_certificate_information.html +++ b/lms/templates/dashboard/_dashboard_certificate_information.html @@ -54,6 +54,14 @@ else: ${_("Download Your {cert_name_short} (PDF)").format(cert_name_short=cert_name_short,)} + + % if cert_status['linked_in_url']: +
  • + + ${_("Add Certificate to LinkedIn.")}
  • + % endif + % elif cert_status['show_download_url'] and enrollment.mode == 'verified' and cert_status['mode'] == 'honor':
  • ${_('Since we did not have a valid set of verification photos from you when your {cert_name_long} was generated, we could not grant you a verified {cert_name_short}. An honor code {cert_name_short} has been granted instead.').format(cert_name_short=cert_name_short, cert_name_long=cert_name_long)}