Ejemplo n.º 1
0
def _does_name_change_require_verification(user_profile, old_name, new_name):
    """
    If name change requires verification, do not update it through this API.
    """

    profile_meta = user_profile.get_meta()
    old_names_list = profile_meta[
        'old_names'] if 'old_names' in profile_meta else []

    user = user_profile.user
    num_certs = len(get_certificates_for_user(user.username))

    validator = NameChangeValidator(old_names_list, num_certs, old_name,
                                    new_name)

    return (is_verified_name_enabled() and not validator.validate())
Ejemplo n.º 2
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)

        if is_verified_name_enabled() and should_use_verified_name_for_certs(user):
            verified_name_obj = 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
Ejemplo n.º 3
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)

    if is_verified_name_enabled() and should_use_verified_name_for_certs(user):
        verified_name_obj = 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
Ejemplo n.º 4
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,
            "is_verified_name_enabled":
            is_verified_name_enabled(),
        }

        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

        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)