Beispiel #1
0
 def ready(self):
     """
     Connect services.
     """
     if settings.FEATURES.get('ENABLE_SPECIAL_EXAMS'):
         name_affirmation_service = get_name_affirmation_service()
         if name_affirmation_service:
             set_runtime_service('name_affirmation', name_affirmation_service)
Beispiel #2
0
 def test_get_name_affirmation_service(self, name_affirmation_installed,
                                       mock_service):
     with patch(
             'openedx.features.name_affirmation_api.utils.is_name_affirmation_installed',
             return_value=name_affirmation_installed):
         name_affirmation_service = get_name_affirmation_service()
         if name_affirmation_installed:
             self.assertEqual(name_affirmation_service, mock_service())
         else:
             self.assertIsNone(name_affirmation_service)
Beispiel #3
0
    def _get_preferred_certificate_name(self, user):
        """
        Copy of `get_preferred_certificate_name` from utils.py - importing it here would introduce
        a circular dependency.
        """
        name_to_use = student_api.get_name(user.id)
        name_affirmation_service = get_name_affirmation_service()

        if name_affirmation_service and name_affirmation_service.should_use_verified_name_for_certs(
                user):
            verified_name_obj = name_affirmation_service.get_verified_name(
                user, is_verified=True)
            if verified_name_obj:
                name_to_use = verified_name_obj.verified_name

        if not name_to_use:
            name_to_use = ''

        return name_to_use
Beispiel #4
0
def get_preferred_certificate_name(user):
    """
    If the verified name feature is enabled and the user has their preference set to use their
    verified name for certificates, return their verified name. Else, return the user's profile
    name, or an empty string if it doesn't exist.
    """
    name_to_use = student_api.get_name(user.id)
    name_affirmation_service = get_name_affirmation_service()

    if name_affirmation_service and name_affirmation_service.should_use_verified_name_for_certs(
            user):
        verified_name_obj = name_affirmation_service.get_verified_name(
            user, is_verified=True)
        if verified_name_obj:
            name_to_use = verified_name_obj.verified_name

    if not name_to_use:
        name_to_use = ''

    return name_to_use
Beispiel #5
0
from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase  # lint-amnesty, pylint: disable=wrong-import-order
from xmodule.modulestore.tests.factories import CourseFactory  # lint-amnesty, pylint: disable=wrong-import-order

ENROLLMENT_METHOD = 'common.djangoapps.student.models.CourseEnrollment.enrollment_mode_for_user'
PROFILE_METHOD = 'common.djangoapps.student.models_api.get_name'

FEATURES_INVALID_FILE_PATH = settings.FEATURES.copy()
FEATURES_INVALID_FILE_PATH[
    'CERTS_HTML_VIEW_CONFIG_PATH'] = 'invalid/path/to/config.json'

TEST_DIR = path(__file__).dirname()
TEST_DATA_DIR = 'common/test/data/'
PLATFORM_ROOT = TEST_DIR.parent.parent.parent.parent
TEST_DATA_ROOT = PLATFORM_ROOT / TEST_DATA_DIR

name_affirmation_service = get_name_affirmation_service()


class ExampleCertificateTest(TestCase, OpenEdxEventsTestMixin):
    """Tests for the ExampleCertificate model. """

    COURSE_KEY = CourseLocator(org='test', course='test', run='test')

    DESCRIPTION = 'test'
    TEMPLATE = 'test.pdf'
    DOWNLOAD_URL = 'https://www.example.com'
    ERROR_REASON = 'Kaboom!'

    ENABLED_OPENEDX_EVENTS = []

    @classmethod
Beispiel #6
0
    def to_representation(self, user):  # lint-amnesty, pylint: disable=arguments-differ
        """
        Overwrite to_native to handle custom logic since we are serializing three models as one here
        :param user: User object
        :return: Dict serialized account
        """
        try:
            user_profile = user.profile
        except ObjectDoesNotExist:
            user_profile = None
            LOGGER.warning("user profile for the user [%s] does not exist",
                           user.username)

        try:
            account_recovery = user.account_recovery
        except ObjectDoesNotExist:
            account_recovery = None

        try:
            activation_key = user.registration.activation_key
        except ObjectDoesNotExist:
            activation_key = None

        accomplishments_shared = badges_enabled()
        data = {
            "username":
            user.username,
            "url":
            self.context.get('request').build_absolute_uri(
                reverse('accounts_api', kwargs={'username': user.username})),
            "email":
            user.email,
            "id":
            user.id,
            # For backwards compatibility: Tables created after the upgrade to Django 1.8 will save microseconds.
            # However, mobile apps are not expecting microsecond in the serialized value. If we set it to zero the
            # DRF JSONEncoder will not include it in the serialized value.
            # https://docs.djangoproject.com/en/1.8/ref/databases/#fractional-seconds-support-for-time-and-datetime-fields
            "date_joined":
            user.date_joined.replace(microsecond=0),
            "last_login":
            user.last_login,
            "is_active":
            user.is_active,
            "activation_key":
            activation_key,
            "bio":
            None,
            "country":
            None,
            "state":
            None,
            "profile_image":
            None,
            "language_proficiencies":
            None,
            "name":
            None,
            "gender":
            None,
            "goals":
            None,
            "year_of_birth":
            None,
            "level_of_education":
            None,
            "mailing_address":
            None,
            "requires_parental_consent":
            None,
            "accomplishments_shared":
            accomplishments_shared,
            "account_privacy":
            self.configuration.get('default_visibility'),
            "social_links":
            None,
            "extended_profile_fields":
            None,
            "phone_number":
            None,
            "pending_name_change":
            None,
            "verified_name":
            None,
        }

        if user_profile:
            data.update({
                "bio":
                AccountLegacyProfileSerializer.convert_empty_to_None(
                    user_profile.bio),
                "country":
                AccountLegacyProfileSerializer.convert_empty_to_None(
                    user_profile.country.code),
                "state":
                AccountLegacyProfileSerializer.convert_empty_to_None(
                    user_profile.state),
                "profile_image":
                AccountLegacyProfileSerializer.get_profile_image(
                    user_profile, user, self.context.get('request')),
                "language_proficiencies":
                LanguageProficiencySerializer(
                    user_profile.language_proficiencies.all().order_by('code'),
                    many=True).data,
                "name":
                user_profile.name,
                "gender":
                AccountLegacyProfileSerializer.convert_empty_to_None(
                    user_profile.gender),
                "goals":
                user_profile.goals,
                "year_of_birth":
                user_profile.year_of_birth,
                "level_of_education":
                AccountLegacyProfileSerializer.convert_empty_to_None(
                    user_profile.level_of_education),
                "mailing_address":
                user_profile.mailing_address,
                "requires_parental_consent":
                user_profile.requires_parental_consent(),
                "account_privacy":
                get_profile_visibility(user_profile, user, self.configuration),
                "social_links":
                SocialLinkSerializer(
                    user_profile.social_links.all().order_by('platform'),
                    many=True).data,
                "extended_profile":
                get_extended_profile(user_profile),
                "phone_number":
                user_profile.phone_number,
            })

        try:
            pending_name_change = PendingNameChange.objects.get(user=user)
            data.update({"pending_name_change": pending_name_change.new_name})
        except PendingNameChange.DoesNotExist:
            pass

        name_affirmation_service = get_name_affirmation_service()
        if name_affirmation_service:
            verified_name_obj = name_affirmation_service.get_verified_name(
                user, is_verified=True)
            if verified_name_obj:
                data.update({"verified_name": verified_name_obj.verified_name})

        if is_secondary_email_feature_enabled():
            data.update({
                "secondary_email":
                account_recovery.secondary_email if account_recovery else None,
                "secondary_email_enabled":
                True,
            })

        if self.custom_fields:
            fields = self.custom_fields
        elif user_profile:
            fields = _visible_fields(user_profile, user, self.configuration)
        else:
            fields = self.configuration.get('public_fields')

        return self._filter_fields(fields, data)