def get_data(self):
        courses = permissions.get_user_course_permissions(self.request.user)
        if not courses:
            # The user is probably not a course administrator and should not be using this application.
            raise PermissionDenied

        enable_course_filters = switch_is_active('enable_course_filters')

        presenter = CourseSummariesPresenter()
        summaries, _ = presenter.get_course_summaries(courses)

        if not summaries:
            # Instead of returning a useless blank CSV, return a 404 error
            raise Http404

        # Exclude specified fields from each summary entry
        summaries = [remove_keys(summary, self.exclude_fields) for summary in summaries]

        if enable_course_filters:
            # Add list of associated program IDs to each summary entry
            programs_presenter = ProgramsPresenter()
            programs = programs_presenter.get_programs(course_ids=courses)
            for summary in summaries:
                summary_programs = [program for program in programs if summary['course_id'] in program['course_ids']]
                summary['program_ids'] = ' | '.join([program['program_id'] for program in summary_programs])
                summary['program_titles'] = ' | '.join([program['program_title'] for program in summary_programs])

        summaries_csv = self.renderer.render(summaries)
        return summaries_csv
Пример #2
0
    def get_context_data(self, **kwargs):
        context = super(CourseIndex, self).get_context_data(**kwargs)
        courses = permissions.get_user_course_permissions(self.request.user)
        if not courses:
            # The user is probably not a course administrator and should not be using this application.
            raise PermissionDenied

        summaries_presenter = CourseSummariesPresenter()
        summaries, last_updated = summaries_presenter.get_course_summaries(
            courses)

        context.update(
            {'update_message': self.get_last_updated_message(last_updated)})

        enable_course_filters = switch_is_active('enable_course_filters')
        data = {
            'course_list_json': summaries,
            'enable_course_filters': enable_course_filters,
            'enable_passing_users': switch_is_active('enable_course_passing'),
            'course_list_download_url': reverse('courses:index_csv'),
        }

        if enable_course_filters:
            programs_presenter = ProgramsPresenter()
            programs = programs_presenter.get_programs(course_ids=courses)
            data['programs_json'] = programs

        context['js_data']['course'] = data
        context['page_data'] = self.get_page_data(context)
        context['summary'] = summaries_presenter.get_course_summary_metrics(
            summaries)

        return context
Пример #3
0
    def test_get_programs(self, program_ids, course_ids):
        ''''Test programs filtered from API response.'''
        presenter = ProgramsPresenter()

        with mock.patch('analyticsclient.programs.Programs.programs',
                        mock.Mock(return_value=self.mock_api_response)):
            actual_programs = presenter.get_programs(program_ids=program_ids, course_ids=course_ids)
            self.assertListEqual(actual_programs, self.get_expected_programs(program_ids=program_ids,
                                                                             course_ids=course_ids))