예제 #1
0
def get_experiment_user_metadata_context(course, user):
    """
    Return a context dictionary with the keys used by the user_metadata.html.
    """
    enrollment_mode = None
    enrollment_time = None
    enrollment = None
    # TODO: clean up as part of REVO-28 (START)
    has_non_audit_enrollments = None
    # TODO: clean up as part of REVO-28 (END)
    try:
        # TODO: clean up as part of REVO-28 (START)
        user_enrollments = CourseEnrollment.objects.select_related(
            'course').filter(user_id=user.id)
        audit_enrollments = user_enrollments.filter(mode='audit')
        has_non_audit_enrollments = (len(audit_enrollments) !=
                                     len(user_enrollments))
        # TODO: clean up as part of REVO-28 (END)
        enrollment = CourseEnrollment.objects.select_related('course').get(
            user_id=user.id, course_id=course.id)
        if enrollment.is_active:
            enrollment_mode = enrollment.mode
            enrollment_time = enrollment.created
    except CourseEnrollment.DoesNotExist:
        pass  # Not enrolled, used the default None values

    upgrade_link, upgrade_date = check_and_get_upgrade_link_and_date(
        user, enrollment, course)
    has_staff_access = has_staff_access_to_preview_mode(user, course.id)
    forum_roles = []
    if user.is_authenticated:
        forum_roles = list(
            Role.objects.filter(
                users=user,
                course_id=course.id).values_list('name').distinct())

    # get user partition data
    partition_groups = get_all_partitions_for_course(course)
    user_partitions = get_user_partition_groups(course.id, partition_groups,
                                                user, 'name')

    return {
        'upgrade_link': upgrade_link,
        'upgrade_price': unicode(get_cosmetic_verified_display_price(course)),
        'enrollment_mode': enrollment_mode,
        'enrollment_time': enrollment_time,
        'pacing_type':
        'self_paced' if course.self_paced else 'instructor_paced',
        'upgrade_deadline': upgrade_date,
        'course_key': course.id,
        'course_start': course.start,
        'course_end': course.end,
        'has_staff_access': has_staff_access,
        'forum_roles': forum_roles,
        'partition_groups': user_partitions,
        # TODO: clean up as part of REVO-28 (START)
        'has_non_audit_enrollments': has_non_audit_enrollments,
        # TODO: clean up as part of REVO-28 (END)
    }
예제 #2
0
    def transform(self, usage_info, block_structure):
        user = usage_info.user
        SplitTestTransformer().transform(usage_info, block_structure)
        staff_access = has_access(user, 'staff', usage_info.course_key)

        # If you have staff access, you are allowed access to the entire result list
        if staff_access:
            return

        user_partitions = block_structure.get_transformer_data(
            self, 'user_partitions')
        if not user_partitions:
            return

        user_groups = get_user_partition_groups(usage_info.course_key,
                                                user_partitions, user, 'id')

        for block_key in block_structure.topological_traversal():
            transformer_block_field = block_structure.get_transformer_block_field(
                block_key, self, 'merged_group_access')
            if transformer_block_field is None:
                continue

            access_denying_partition_ids = transformer_block_field.get_access_denying_partitions(
                user_groups)
            access_denying_reasons = []
            access_denying_messages = []
            for partition_id in access_denying_partition_ids:
                access_denying_partition = get_partition_from_id(
                    user_partitions, partition_id)

                if access_denying_partition:
                    user_group = user_groups.get(access_denying_partition.id)
                    allowed_groups = transformer_block_field.get_allowed_groups(
                    )[access_denying_partition.id]
                    access_denied_message = access_denying_partition.access_denied_message(
                        block_key, user, user_group, allowed_groups)
                    access_denying_reasons.append(
                        access_denying_partition.name)
                    if access_denied_message:
                        access_denying_messages.append(access_denied_message)

            if access_denying_reasons and not access_denying_messages:
                block_structure.remove_block(block_key, keep_descendants=False)
            else:
                if access_denying_reasons:
                    block_structure.override_xblock_field(
                        block_key, 'authorization_denial_reason',
                        access_denying_reasons[0])
                if access_denying_messages:
                    block_structure.override_xblock_field(
                        block_key, 'authorization_denial_message',
                        access_denying_messages[0])
예제 #3
0
 def load_data(self):
     """
     Pull track groups for this course and which group the user is in.
     """
     user_partition = create_enrollment_track_partition_with_course_id(
         self.course_key)
     self.enrollment_track_groups = get_user_partition_groups(
         self.course_key, [user_partition],
         self.user,
         partition_dict_key='id')
     self.user_group = self.enrollment_track_groups.get(
         ENROLLMENT_TRACK_PARTITION_ID)
예제 #4
0
def get_experiment_user_metadata_context(course, user):
    """
    Return a context dictionary with the keys used by the user_metadata.html.
    """
    if DEPRECATED_METADATA.is_enabled():
        return get_deprecated_experiment_user_metadata_context(course, user)

    enrollment = None
    # TODO: clean up as part of REVO-28 (START)
    user_enrollments = None
    audit_enrollments = None
    has_non_audit_enrollments = False
    try:
        user_enrollments = CourseEnrollment.objects.select_related(
            'course').filter(user_id=user.id)
        audit_enrollments = user_enrollments.filter(mode='audit')
        has_non_audit_enrollments = (len(audit_enrollments) !=
                                     len(user_enrollments))
        # TODO: clean up as part of REVO-28 (END)
        enrollment = CourseEnrollment.objects.select_related('course').get(
            user_id=user.id, course_id=course.id)
    except CourseEnrollment.DoesNotExist:
        pass  # Not enrolled, use the default values

    context = get_base_experiment_metadata_context(course, user, enrollment,
                                                   user_enrollments,
                                                   audit_enrollments)
    has_staff_access = has_staff_access_to_preview_mode(user, course.id)
    forum_roles = []
    if user.is_authenticated:
        forum_roles = list(
            Role.objects.filter(
                users=user,
                course_id=course.id).values_list('name').distinct())

    # get user partition data
    if user.is_authenticated():
        partition_groups = get_all_partitions_for_course(course)
        user_partitions = get_user_partition_groups(course.id,
                                                    partition_groups, user,
                                                    'name')
    else:
        user_partitions = {}

    # TODO: clean up as part of REVO-28 (START)
    context['has_non_audit_enrollments'] = has_non_audit_enrollments
    # TODO: clean up as part of REVO-28 (END)
    context['has_staff_access'] = has_staff_access
    context['forum_roles'] = forum_roles
    context['partition_groups'] = user_partitions
    return context
예제 #5
0
def get_experiment_user_metadata_context(course, user):
    """
    Return a context dictionary with the keys used by the user_metadata.html.
    """
    enrollment = None
    # TODO: clean up as part of REVO-28 (START)
    user_enrollments = None
    audit_enrollments = None
    has_non_audit_enrollments = False
    try:
        user_enrollments = CourseEnrollment.objects.select_related(
            'course', 'schedule').filter(user_id=user.id)
        has_non_audit_enrollments = user_enrollments.exclude(
            mode__in=CourseMode.UPSELL_TO_VERIFIED_MODES).exists()
        # TODO: clean up as part of REVO-28 (END)
        enrollment = CourseEnrollment.objects.select_related(
            'course', 'schedule').get(user_id=user.id, course_id=course.id)
    except CourseEnrollment.DoesNotExist:
        pass  # Not enrolled, use the default values

    has_entitlements = False
    if user.is_authenticated():
        has_entitlements = CourseEntitlement.objects.filter(user=user).exists()

    context = get_base_experiment_metadata_context(course, user, enrollment,
                                                   user_enrollments)
    has_staff_access = has_staff_access_to_preview_mode(user, course.id)
    forum_roles = []
    if user.is_authenticated:
        forum_roles = list(
            Role.objects.filter(
                users=user,
                course_id=course.id).values_list('name').distinct())

    # get user partition data
    if user.is_authenticated():
        partition_groups = get_all_partitions_for_course(course)
        user_partitions = get_user_partition_groups(course.id,
                                                    partition_groups, user,
                                                    'name')
    else:
        user_partitions = {}

    # TODO: clean up as part of REVO-28 (START)
    context[
        'has_non_audit_enrollments'] = has_non_audit_enrollments or has_entitlements
    # TODO: clean up as part of REVO-28 (END)
    context['has_staff_access'] = has_staff_access
    context['forum_roles'] = forum_roles
    context['partition_groups'] = user_partitions
    return context
예제 #6
0
    def transform_block_filters(self, usage_info, block_structure):
        user = usage_info.user
        result_list = SplitTestTransformer().transform_block_filters(
            usage_info, block_structure)
        staff_access = has_access(user, 'staff', usage_info.course_key)

        # If you have staff access, you are allowed access to the entire result list
        if staff_access:
            return result_list

        user_partitions = block_structure.get_transformer_data(
            self, 'user_partitions')
        if not user_partitions:
            return [block_structure.create_universal_filter()]

        user_groups = get_user_partition_groups(usage_info.course_key,
                                                user_partitions, user, 'id')

        for block_key in block_structure.topological_traversal():
            transformer_block_field = block_structure.get_transformer_block_field(
                block_key, self, 'merged_group_access')
            access_denying_partition_id = transformer_block_field.get_access_denying_partition(
                user_groups)
            access_denying_partition = get_partition_from_id(
                user_partitions, access_denying_partition_id)

            if access_denying_partition:
                user_group = user_groups.get(access_denying_partition.id)
                allowed_groups = transformer_block_field.get_allowed_groups()[
                    access_denying_partition.id]
                access_denied_message = access_denying_partition.access_denied_message(
                    block_key, user, user_group, allowed_groups)
                block_structure.override_xblock_field(
                    block_key, 'authorization_denial_reason',
                    access_denying_partition.name)
                block_structure.override_xblock_field(
                    block_key, 'authorization_denial_message',
                    access_denied_message)

        group_access_filter = block_structure.create_removal_filter(
            lambda block_key: (block_structure.get_transformer_block_field(
                block_key, self, 'merged_group_access'
            ).get_access_denying_partition(
                user_groups) is not None and block_structure.get_xblock_field(
                    block_key, 'authorization_denial_message') is None))
        result_list.append(group_access_filter)

        return result_list
예제 #7
0
파일: utils.py 프로젝트: edx/edx-platform
def get_experiment_user_metadata_context(course, user):
    """
    Return a context dictionary with the keys used by the user_metadata.html.
    """
    if DEPRECATED_METADATA.is_enabled():
        return get_deprecated_experiment_user_metadata_context(course, user)

    enrollment = None
    # TODO: clean up as part of REVO-28 (START)
    user_enrollments = None
    audit_enrollments = None
    has_non_audit_enrollments = False
    try:
        user_enrollments = CourseEnrollment.objects.select_related('course').filter(user_id=user.id)
        audit_enrollments = user_enrollments.filter(mode='audit')
        has_non_audit_enrollments = (len(audit_enrollments) != len(user_enrollments))
        # TODO: clean up as part of REVO-28 (END)
        enrollment = CourseEnrollment.objects.select_related(
            'course'
        ).get(user_id=user.id, course_id=course.id)
    except CourseEnrollment.DoesNotExist:
        pass  # Not enrolled, use the default values

    context = get_base_experiment_metadata_context(course, user, enrollment, user_enrollments, audit_enrollments)
    has_staff_access = has_staff_access_to_preview_mode(user, course.id)
    forum_roles = []
    if user.is_authenticated:
        forum_roles = list(Role.objects.filter(users=user, course_id=course.id).values_list('name').distinct())

    # get user partition data
    if user.is_authenticated():
        partition_groups = get_all_partitions_for_course(course)
        user_partitions = get_user_partition_groups(course.id, partition_groups, user, 'name')
    else:
        user_partitions = {}

    # TODO: clean up as part of REVO-28 (START)
    context['has_non_audit_enrollments'] = has_non_audit_enrollments
    # TODO: clean up as part of REVO-28 (END)
    context['has_staff_access'] = has_staff_access
    context['forum_roles'] = forum_roles
    context['partition_groups'] = user_partitions
    return context
예제 #8
0
    def transform_block_filters(self, usage_info, block_structure):
        user = usage_info.user
        result_list = SplitTestTransformer().transform_block_filters(usage_info, block_structure)

        user_partitions = block_structure.get_transformer_data(self, 'user_partitions')
        if not user_partitions:
            return [block_structure.create_universal_filter()]

        user_groups = get_user_partition_groups(usage_info.course_key, user_partitions, user, 'id')

        for block_key in block_structure.topological_traversal():
            transformer_block_field = block_structure.get_transformer_block_field(
                block_key, self, 'merged_group_access'
            )
            access_denying_partition_id = transformer_block_field.get_access_denying_partition(
                user_groups
            )
            access_denying_partition = get_partition_from_id(user_partitions, access_denying_partition_id)

            if not has_access(user, 'staff', block_key) and access_denying_partition:
                user_group = user_groups.get(access_denying_partition.id)
                allowed_groups = transformer_block_field.get_allowed_groups()[access_denying_partition.id]
                access_denied_message = access_denying_partition.access_denied_message(
                    block_key, user, user_group, allowed_groups
                )
                block_structure.override_xblock_field(
                    block_key, 'authorization_denial_reason', access_denying_partition.name
                )
                block_structure.override_xblock_field(
                    block_key, 'authorization_denial_message', access_denied_message
                )

        group_access_filter = block_structure.create_removal_filter(
            lambda block_key: (
                not has_access(user, 'staff', block_key) and
                block_structure.get_transformer_block_field(
                    block_key, self, 'merged_group_access'
                ).get_access_denying_partition(user_groups) is not None and
                block_structure.get_xblock_field(block_key, 'authorization_denial_message') is None
            )
        )
        result_list.append(group_access_filter)
        return result_list
예제 #9
0
    def test_user_randomly_assigned(self):
        # user was randomly assigned to one of the groups
        user_groups = get_user_partition_groups(
            self.course.id, [self.split_test_user_partition], self.user, 'id')
        assert len(user_groups) == 1

        # calling twice should result in the same block set
        block_structure1 = get_course_blocks(
            self.user,
            self.course.location,
            self.transformers,
        )
        with check_mongo_calls(0):
            block_structure2 = get_course_blocks(
                self.user,
                self.course.location,
                self.transformers,
            )
        assert set(block_structure1.get_block_keys()) == set(
            block_structure2.get_block_keys())
예제 #10
0
    def transform_block_filters(self, usage_info, block_structure):
        user = usage_info.user
        result_list = SplitTestTransformer().transform_block_filters(
            usage_info, block_structure)

        user_partitions = block_structure.get_transformer_data(
            self, 'user_partitions')
        if not user_partitions:
            return [block_structure.create_universal_filter()]

        user_groups = get_user_partition_groups(usage_info.course_key,
                                                user_partitions, user, 'id')
        group_access_filter = block_structure.create_removal_filter(
            lambda block_key: not (has_access(user, 'staff', block_key) or
                                   block_structure.get_transformer_block_field(
                                       block_key, self, 'merged_group_access'
                                   ).check_group_access(user_groups)))

        result_list.append(group_access_filter)
        return result_list
예제 #11
0
    def transform_block_filters(self, usage_info, block_structure):
        user = usage_info.user
        result_list = SplitTestTransformer().transform_block_filters(usage_info, block_structure)

        user_partitions = block_structure.get_transformer_data(self, 'user_partitions')
        if not user_partitions:
            return [block_structure.create_universal_filter()]

        user_groups = get_user_partition_groups(usage_info.course_key, user_partitions, user, 'id')
        group_access_filter = block_structure.create_removal_filter(
            lambda block_key: not (
                has_access(user, 'staff', block_key) or
                block_structure.get_transformer_block_field(block_key, self, 'merged_group_access').check_group_access(
                    user_groups
                )
            )
        )

        result_list.append(group_access_filter)
        return result_list
예제 #12
0
def get_experiment_user_metadata_context(course, user):
    """
    Return a context dictionary with the keys used by the user_metadata.html.
    """
    enrollment_mode = None
    enrollment_time = None
    enrollment = None
    try:
        enrollment = CourseEnrollment.objects.select_related(
            'course'
        ).get(user_id=user.id, course_id=course.id)
        if enrollment.is_active:
            enrollment_mode = enrollment.mode
            enrollment_time = enrollment.created
    except CourseEnrollment.DoesNotExist:
        pass  # Not enrolled, used the default None values

    upgrade_link, upgrade_date = check_and_get_upgrade_link_and_date(user, enrollment, course)
    has_staff_access = has_staff_access_to_preview_mode(user, course.id)
    forum_roles = []
    if user.is_authenticated:
        forum_roles = list(Role.objects.filter(users=user, course_id=course.id).values_list('name').distinct())

    # get user partition data
    partition_groups = get_all_partitions_for_course(course)
    user_partitions = get_user_partition_groups(course.id, partition_groups, user, 'name')

    return {
        'upgrade_link': upgrade_link,
        'upgrade_price': unicode(get_cosmetic_verified_display_price(course)),
        'enrollment_mode': enrollment_mode,
        'enrollment_time': enrollment_time,
        'pacing_type': 'self_paced' if course.self_paced else 'instructor_paced',
        'upgrade_deadline': upgrade_date,
        'course_key': course.id,
        'course_start': course.start,
        'course_end': course.end,
        'has_staff_access': has_staff_access,
        'forum_roles': forum_roles,
        'partition_groups': user_partitions,
    }
예제 #13
0
    def test_user_randomly_assigned(self):
        # user was randomly assigned to one of the groups
        user_groups = get_user_partition_groups(
            self.course.id, [self.split_test_user_partition], self.user, 'id'
        )
        self.assertEquals(len(user_groups), 1)

        # calling twice should result in the same block set
        block_structure1 = get_course_blocks(
            self.user,
            self.course.location,
            self.transformers,
        )
        with check_mongo_calls(0):
            block_structure2 = get_course_blocks(
                self.user,
                self.course.location,
                self.transformers,
            )
        self.assertEqual(
            set(block_structure1.get_block_keys()),
            set(block_structure2.get_block_keys()),
        )
예제 #14
0
def get_experiment_user_metadata_context(course, user):
    """
    Return a context dictionary with the keys used by the user_metadata.html.
    """
    enrollment_mode = None
    enrollment_time = None
    enrollment = None
    # TODO: clean up as part of REVO-28 (START)
    has_non_audit_enrollments = None
    # TODO: clean up as part of REVO-28 (END)
    # TODO: clean up as part of REVEM-106 (START)
    program_key = None
    # TODO: clean up as part of REVEM-106 (END)
    try:
        # TODO: clean up as part of REVO-28 (START)
        user_enrollments = CourseEnrollment.objects.select_related(
            'course').filter(user_id=user.id)
        audit_enrollments = user_enrollments.filter(mode='audit')
        has_non_audit_enrollments = (len(audit_enrollments) !=
                                     len(user_enrollments))
        # TODO: clean up as part of REVO-28 (END)
        enrollment = CourseEnrollment.objects.select_related('course').get(
            user_id=user.id, course_id=course.id)
        if enrollment.is_active:
            enrollment_mode = enrollment.mode
            enrollment_time = enrollment.created

            # TODO: clean up as part of REVEM-106 (START)
            # get program data for this course
            request = get_current_request()
            if request:
                enrollment_list = [enrollment]
                meter = ProgramProgressMeter(request.site,
                                             user,
                                             enrollments=enrollment_list)
                if meter.engaged_programs and meter.engaged_programs[0]:
                    org_name = None
                    courses_not_started = 0
                    courses_in_progress = 0
                    courses_completed = 0
                    program_data = meter.engaged_programs[0]
                    program_data = ProgramDataExtender(
                        program_data, user, mobile_only=False).extend()
                    program_orgs = program_data.get(
                        'credit_backing_organizations')
                    if program_orgs and program_orgs[0]:
                        org = program_orgs[0]
                        org_name = org.get('name')
                    if meter.progress() and meter.progress()[0]:
                        progress = meter.progress()[0]
                        courses_not_started = progress.get('not_started')
                        courses_in_progress = progress.get('in_progress')
                        courses_completed = progress.get('completed')
                    program_key = {
                        'uuid': program_data.get('uuid'),
                        'title': program_data.get('title'),
                        'marketing_url': program_data.get('marketing_url'),
                        'org_name': org_name,
                        'courses_not_started': courses_not_started,
                        'courses_in_progress': courses_in_progress,
                        'courses_completed': courses_completed,
                    }
            # TODO: clean up as part of REVEM-106 (END)
    except CourseEnrollment.DoesNotExist:
        pass  # Not enrolled, used the default None values

    # upgrade_link and upgrade_date should be None if user has passed their dynamic pacing deadline.
    upgrade_link, upgrade_date = check_and_get_upgrade_link_and_date(
        user, enrollment, course)
    has_staff_access = has_staff_access_to_preview_mode(user, course.id)
    forum_roles = []
    if user.is_authenticated:
        forum_roles = list(
            Role.objects.filter(
                users=user,
                course_id=course.id).values_list('name').distinct())

    # get user partition data
    if user.is_authenticated():
        partition_groups = get_all_partitions_for_course(course)
        user_partitions = get_user_partition_groups(course.id,
                                                    partition_groups, user,
                                                    'name')
    else:
        user_partitions = {}

    return {
        'upgrade_link': upgrade_link,
        'upgrade_price': unicode(get_cosmetic_verified_display_price(course)),
        'enrollment_mode': enrollment_mode,
        'enrollment_time': enrollment_time,
        'pacing_type':
        'self_paced' if course.self_paced else 'instructor_paced',
        'upgrade_deadline': upgrade_date,
        'course_key': course.id,
        'course_start': course.start,
        'course_end': course.end,
        'has_staff_access': has_staff_access,
        'forum_roles': forum_roles,
        'partition_groups': user_partitions,
        # TODO: clean up as part of REVO-28 (START)
        'has_non_audit_enrollments': has_non_audit_enrollments,
        # TODO: clean up as part of REVO-28 (END)
        # TODO: clean up as part of REVEM-106 (START)
        'program_key_fields': program_key,
        # TODO: clean up as part of REVEM-106 (END)
    }
예제 #15
0
def get_experiment_user_metadata_context(course, user):
    """
    Return a context dictionary with the keys used for Optimizely experiments, exposed via user_metadata.html:
    view from the DOM in those calling views using: JSON.parse($("#user-metadata").text());
    Most views call this function with both parameters, but student dashboard has only a user
    """
    enrollment = None
    # TODO: clean up as part of REVO-28 (START)
    user_enrollments = None
    audit_enrollments = None
    has_non_audit_enrollments = False
    context = {}
    if course is not None:
        try:
            user_enrollments = CourseEnrollment.objects.select_related('course', 'schedule').filter(user_id=user.id)
            has_non_audit_enrollments = user_enrollments.exclude(mode__in=CourseMode.UPSELL_TO_VERIFIED_MODES).exists()
            # TODO: clean up as part of REVO-28 (END)
            enrollment = CourseEnrollment.objects.select_related(
                'course', 'schedule'
            ).get(user_id=user.id, course_id=course.id)
        except CourseEnrollment.DoesNotExist:
            pass  # Not enrolled, use the default values

        has_entitlements = False
        if user.is_authenticated:
            has_entitlements = CourseEntitlement.objects.filter(user=user).exists()

        context = get_base_experiment_metadata_context(course, user, enrollment, user_enrollments)
        has_staff_access = has_staff_access_to_preview_mode(user, course.id)
        forum_roles = []
        if user.is_authenticated:
            forum_roles = list(Role.objects.filter(users=user, course_id=course.id).values_list('name').distinct())

        # get user partition data
        if user.is_authenticated:
            partition_groups = get_all_partitions_for_course(course)
            user_partitions = get_user_partition_groups(course.id, partition_groups, user, 'name')
        else:
            user_partitions = {}

        # TODO: clean up as part of REVO-28 (START)
        context['has_non_audit_enrollments'] = has_non_audit_enrollments or has_entitlements
        # TODO: clean up as part of REVO-28 (END)
        context['has_staff_access'] = has_staff_access
        context['forum_roles'] = forum_roles
        context['partition_groups'] = user_partitions

    user_metadata = {
        key: context.get(key)
        for key in (
            'username',
            'user_id',
            'course_id',
            'course_display_name',
            'enrollment_mode',
            'upgrade_link',
            'upgrade_price',
            'audit_access_deadline',
            'course_duration',
            'pacing_type',
            'has_staff_access',
            'forum_roles',
            'partition_groups',
            # TODO: clean up as part of REVO-28 (START)
            'has_non_audit_enrollments',
            # TODO: clean up as part of REVO-28 (END)
            # TODO: clean up as part of REVEM-199 (START)
            'program_key_fields',
            # TODO: clean up as part of REVEM-199 (END)
        )
    }

    if user:
        user_metadata['username'] = user.username
        user_metadata['user_id'] = user.id
        if hasattr(user, 'email'):
            user_metadata['email'] = user.email

    for datekey in (
            'schedule_start',
            'enrollment_time',
            'course_start',
            'course_end',
            'dynamic_upgrade_deadline',
            'course_upgrade_deadline',
            'audit_access_deadline',
    ):
        user_metadata[datekey] = (
            context.get(datekey).isoformat() if context.get(datekey) else None
        )

    for timedeltakey in (
        'course_duration',
    ):
        user_metadata[timedeltakey] = (
            context.get(timedeltakey).total_seconds() if context.get(timedeltakey) else None
        )

    course_key = context.get('course_key')
    if course and not course_key:
        course_key = course.id

    if course_key:
        if isinstance(course_key, CourseKey):
            user_metadata['course_key_fields'] = {
                'org': course_key.org,
                'course': course_key.course,
                'run': course_key.run,
            }

            if not context.get('course_id'):
                user_metadata['course_id'] = six.text_type(course_key)
        elif isinstance(course_key, six.string_types):
            user_metadata['course_id'] = course_key

    context['user_metadata'] = user_metadata
    return context
예제 #16
0
def get_deprecated_experiment_user_metadata_context(course, user):
    """
    Return a context dictionary with the keys used by the user_metadata.html. This is deprecated and will be removed
    once we have confirmed that its replacement functions as intended.
    """
    enrollment_mode = None
    enrollment_time = None
    enrollment = None
    # TODO: clean up as part of REVO-28 (START)
    has_non_audit_enrollments = None
    # TODO: clean up as part of REVO-28 (END)
    # TODO: clean up as part of REVEM-199 (START)
    program_key = None
    # TODO: clean up as part of REVEM-199 (END)
    try:
        # TODO: clean up as part of REVO-28 (START)
        user_enrollments = CourseEnrollment.objects.select_related('course').filter(user_id=user.id)
        audit_enrollments = user_enrollments.filter(mode='audit')
        has_non_audit_enrollments = (len(audit_enrollments) != len(user_enrollments))
        # TODO: clean up as part of REVO-28 (END)
        # TODO: clean up as part of REVEM-199 (START)
        if PROGRAM_INFO_FLAG.is_enabled():
            programs = get_programs(course=course.id)
            if programs:
                # A course can be in multiple programs, but we're just grabbing the first one
                program = programs[0]
                complete_enrollment = False
                has_courses_left_to_purchase = False
                total_courses = None
                courses = program.get('courses')
                courses_left_to_purchase_price = None
                courses_left_to_purchase_url = None
                program_uuid = program.get('uuid')
                status = None
                is_eligible_for_one_click_purchase = None
                if courses is not None:
                    total_courses = len(courses)
                    complete_enrollment = is_enrolled_in_all_courses(courses, user_enrollments)
                    status = program.get('status')
                    is_eligible_for_one_click_purchase = program.get('is_program_eligible_for_one_click_purchase')
                    # Get the price and purchase URL of the program courses the user has yet to purchase. Say a
                    # program has 3 courses (A, B and C), and the user previously purchased a certificate for A.
                    # The user is enrolled in audit mode for B. The "left to purchase price" should be the price of
                    # B+C.
                    non_audit_enrollments = [en for en in user_enrollments if en not in
                                             audit_enrollments]
                    courses_left_to_purchase = get_unenrolled_courses(courses, non_audit_enrollments)
                    if courses_left_to_purchase:
                        has_courses_left_to_purchase = True
                        if is_eligible_for_one_click_purchase:
                            courses_left_to_purchase_price, courses_left_to_purchase_skus = \
                                get_program_price_and_skus(courses_left_to_purchase)
                            if courses_left_to_purchase_skus:
                                courses_left_to_purchase_url = EcommerceService().get_checkout_page_url(
                                    *courses_left_to_purchase_skus, program_uuid=program_uuid)

                program_key = {
                    'uuid': program_uuid,
                    'title': program.get('title'),
                    'marketing_url': program.get('marketing_url'),
                    'status': status,
                    'is_eligible_for_one_click_purchase': is_eligible_for_one_click_purchase,
                    'total_courses': total_courses,
                    'complete_enrollment': complete_enrollment,
                    'has_courses_left_to_purchase': has_courses_left_to_purchase,
                    'courses_left_to_purchase_price': courses_left_to_purchase_price,
                    'courses_left_to_purchase_url': courses_left_to_purchase_url,
                }
        # TODO: clean up as part of REVEM-199 (END)
        enrollment = CourseEnrollment.objects.select_related(
            'course'
        ).get(user_id=user.id, course_id=course.id)
        if enrollment.is_active:
            enrollment_mode = enrollment.mode
            enrollment_time = enrollment.created
    except CourseEnrollment.DoesNotExist:
        pass  # Not enrolled, used the default None values

    # upgrade_link and upgrade_date should be None if user has passed their dynamic pacing deadline.
    upgrade_link, upgrade_date = check_and_get_upgrade_link_and_date(user, enrollment, course)
    has_staff_access = has_staff_access_to_preview_mode(user, course.id)
    forum_roles = []
    if user.is_authenticated:
        forum_roles = list(Role.objects.filter(users=user, course_id=course.id).values_list('name').distinct())

    # get user partition data
    if user.is_authenticated():
        partition_groups = get_all_partitions_for_course(course)
        user_partitions = get_user_partition_groups(course.id, partition_groups, user, 'name')
    else:
        user_partitions = {}

    return {
        'upgrade_link': upgrade_link,
        'upgrade_price': six.text_type(get_cosmetic_verified_display_price(course)),
        'enrollment_mode': enrollment_mode,
        'enrollment_time': enrollment_time,
        'pacing_type': 'self_paced' if course.self_paced else 'instructor_paced',
        'upgrade_deadline': upgrade_date,
        'audit_access_deadline': get_audit_access_expiration(user, course),
        'course_key': course.id,
        'course_start': course.start,
        'course_end': course.end,
        'has_staff_access': has_staff_access,
        'forum_roles': forum_roles,
        'partition_groups': user_partitions,
        # TODO: clean up as part of REVO-28 (START)
        'has_non_audit_enrollments': has_non_audit_enrollments,
        # TODO: clean up as part of REVO-28 (END)
        # TODO: clean up as part of REVEM-199 (START)
        'program_key_fields': program_key,
        # TODO: clean up as part of REVEM-199 (END)
    }
예제 #17
0
def get_experiment_user_metadata_context(course, user):
    """
    Return a context dictionary with the keys used by the user_metadata.html.
    """
    enrollment_mode = None
    enrollment_time = None
    enrollment = None
    # TODO: clean up as part of REVO-28 (START)
    has_non_audit_enrollments = None
    # TODO: clean up as part of REVO-28 (END)

    # TODO: clean up as part of REVEM-199 (START)
    program_key = None
    # TODO: clean up as part of REVEM-199 (END)
    try:
        # TODO: clean up as part of REVO-28 (START)
        user_enrollments = CourseEnrollment.objects.select_related(
            'course').filter(user_id=user.id)
        audit_enrollments = user_enrollments.filter(mode='audit')
        has_non_audit_enrollments = (len(audit_enrollments) !=
                                     len(user_enrollments))
        # TODO: clean up as part of REVO-28 (END)
        enrollment = CourseEnrollment.objects.select_related('course').get(
            user_id=user.id, course_id=course.id)
        if enrollment.is_active:
            enrollment_mode = enrollment.mode
            enrollment_time = enrollment.created

            # TODO: clean up as part of REVEM-199 (START)
            if PROGRAM_INFO_FLAG.is_enabled():
                programs = get_programs(course=course.id)
                if programs:
                    # A course can be in multiple programs, but we're just grabbing the first one
                    program = programs[0]
                    complete_enrollment = False
                    total_courses = None
                    courses = program.get('courses')
                    if courses is not None:
                        total_courses = len(courses)
                        complete_enrollment = is_enrolled_in_all_courses_in_program(
                            courses, user_enrollments)

                    program_key = {
                        'uuid': program.get('uuid'),
                        'title': program.get('title'),
                        'marketing_url': program.get('marketing_url'),
                        'total_courses': total_courses,
                        'complete_enrollment': complete_enrollment,
                    }
            # TODO: clean up as part of REVEM-199 (END)
    except CourseEnrollment.DoesNotExist:
        pass  # Not enrolled, used the default None values

    # upgrade_link and upgrade_date should be None if user has passed their dynamic pacing deadline.
    upgrade_link, upgrade_date = check_and_get_upgrade_link_and_date(
        user, enrollment, course)
    has_staff_access = has_staff_access_to_preview_mode(user, course.id)
    forum_roles = []
    if user.is_authenticated:
        forum_roles = list(
            Role.objects.filter(
                users=user,
                course_id=course.id).values_list('name').distinct())

    # get user partition data
    if user.is_authenticated():
        partition_groups = get_all_partitions_for_course(course)
        user_partitions = get_user_partition_groups(course.id,
                                                    partition_groups, user,
                                                    'name')
    else:
        user_partitions = {}

    return {
        'upgrade_link': upgrade_link,
        'upgrade_price': unicode(get_cosmetic_verified_display_price(course)),
        'enrollment_mode': enrollment_mode,
        'enrollment_time': enrollment_time,
        'pacing_type':
        'self_paced' if course.self_paced else 'instructor_paced',
        'upgrade_deadline': upgrade_date,
        'course_key': course.id,
        'course_start': course.start,
        'course_end': course.end,
        'has_staff_access': has_staff_access,
        'forum_roles': forum_roles,
        'partition_groups': user_partitions,
        # TODO: clean up as part of REVO-28 (START)
        'has_non_audit_enrollments': has_non_audit_enrollments,
        # TODO: clean up as part of REVO-28 (END)
        # TODO: clean up as part of REVEM-199 (START)
        'program_key_fields': program_key,
        # TODO: clean up as part of REVEM-199 (END)
    }