def get_single_enrollment_overview( user, program, course_overview, certificate_info, relative_resume_url, request, ): """ Get an overview of a user's enrollment in a course run within a program. Arguments: user (User) program (Program) course_overview (CourseOverview) certificate_info (dict): Info about a user's certificate in this course run. relative_resume_url (str): URL to resume course. Relative to LMS root. request (HttpRequest): Source HTTP request. Needed for URL generation. Returns: dict """ course_key = course_overview.id course_run_status = get_course_run_status(course_overview, certificate_info) due_dates = ( get_due_dates(request, course_key, user) if course_run_status == CourseRunProgressStatuses.IN_PROGRESS else [] ) result = { 'course_run_id': str(course_key), 'display_name': course_overview.display_name_with_default, 'course_run_status': course_run_status, 'course_run_url': get_course_run_url(request, course_key), 'start_date': course_overview.start, 'end_date': course_overview.end, 'due_dates': due_dates, } emails_enabled = get_emails_enabled(user, course_key) if emails_enabled is not None: result['emails_enabled'] = emails_enabled download_url = certificate_info.get('download_url') if download_url: result['certificate_download_url'] = request.build_absolute_uri( certificate_info['download_url'] ) if program['type'] == 'MicroMasters': result['micromasters_title'] = program['title'] if relative_resume_url: result['resume_course_run_url'] = request.build_absolute_uri(relative_resume_url) return result
def to_representation(self, instance): representation = super(EnterpriseCourseEnrollmentSerializer, self).to_representation(instance) request = self.context['request'] course_run_id = instance.course_id user = request.user # Course Overview course_overview = self._get_course_overview(course_run_id) # Certificate certificate_info = get_certificate_for_user(user.username, course_run_id) or {} representation['certificate_download_url'] = certificate_info.get( 'download_url') # Email enabled emails_enabled = get_emails_enabled(user, course_run_id) if emails_enabled is not None: representation['emails_enabled'] = emails_enabled representation['course_run_id'] = course_run_id representation['course_run_status'] = get_course_run_status( course_overview, certificate_info, instance) representation['start_date'] = course_overview['start'] representation['end_date'] = course_overview['end'] representation['display_name'] = course_overview[ 'display_name_with_default'] representation['course_run_url'] = get_course_run_url( request, course_run_id) representation['due_dates'] = [] representation['pacing'] = course_overview['pacing'] representation['org_name'] = course_overview[ 'display_org_with_default'] representation[ 'is_revoked'] = instance.license.is_revoked if instance.license else False return representation
def get(self, request, program_uuid=None): """ Defines the GET endpoint for overviews of course enrollments for a user as part of a program. """ user = request.user self._check_program_enrollment_exists(user, program_uuid) course_run_keys = [ CourseKey.from_string(key) for key in course_run_keys_for_program(self.program) ] course_enrollments = CourseEnrollment.objects.filter( user=user, course_id__in=course_run_keys, mode__in=[CourseMode.VERIFIED, CourseMode.MASTERS], is_active=True, ) overviews = CourseOverview.get_from_ids(course_run_keys) course_run_resume_urls = get_resume_urls_for_enrollments( user, course_enrollments) course_runs = [] for enrollment in course_enrollments: overview = overviews[enrollment.course_id] certificate_info = get_certificate_for_user( user.username, enrollment.course_id) or {} course_run_dict = { 'course_run_id': enrollment.course_id, 'display_name': overview.display_name_with_default, 'course_run_status': get_course_run_status(overview, certificate_info), 'course_run_url': get_course_run_url(request, enrollment.course_id), 'start_date': overview.start, 'end_date': overview.end, 'due_dates': get_due_dates(request, enrollment.course_id, user), } emails_enabled = get_emails_enabled(user, enrollment.course_id) if emails_enabled is not None: course_run_dict['emails_enabled'] = emails_enabled if certificate_info.get('download_url'): course_run_dict[ 'certificate_download_url'] = request.build_absolute_uri( certificate_info['download_url']) if self.program['type'] == 'MicroMasters': course_run_dict['micromasters_title'] = self.program['title'] if course_run_resume_urls.get(enrollment.course_id): relative_resume_course_run_url = course_run_resume_urls.get( enrollment.course_id) course_run_dict[ 'resume_course_run_url'] = request.build_absolute_uri( relative_resume_course_run_url) course_runs.append(course_run_dict) serializer = CourseRunOverviewListSerializer( {'course_runs': course_runs}) return Response(serializer.data)