Beispiel #1
0
    def get_context_data(self, **kwargs):
        context = super(CourseIndex, self).get_context_data(**kwargs)
        user = self.request.user
        courses = permissions.get_user_course_permissions(user)
        if not courses and not (user.is_superuser or user.is_staff):
            # 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
    def get_data(self):
        user = self.request.user
        courses = permissions.get_user_course_permissions(user)
        if not courses and not (user.is_superuser or user.is_staff):
            # 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(analytics_client=self.analytics_client)
        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(analytics_client=self.analytics_client)
            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
Beispiel #3
0
 def test_no_summaries(self):
     cache.clear()  # previous test has course_ids=None case cached
     presenter = CourseSummariesPresenter()
     with mock.patch(
             'analyticsclient.course_summaries.CourseSummaries.course_summaries',
             mock.Mock(return_value=[])):
         summaries, last_updated = presenter.get_course_summaries()
         self.assertListEqual(summaries, [])
         self.assertIsNone(last_updated)
Beispiel #4
0
 def test_get_course_summary_metrics(self):
     presenter = CourseSummariesPresenter(Client('base_url'))
     metrics = presenter.get_course_summary_metrics(
         self._PRESENTER_SUMMARIES.values())
     expected = {
         'total_enrollment': 5111,
         'current_enrollment': 3888,
         'enrollment_change_7_days': 4,
         'verified_enrollment': 13,
         'masters_enrollment': 1111,
     }
     self.assertEqual(metrics, expected)
Beispiel #5
0
    def test_get_summaries(self, input_course_ids, ouptut_course_ids):
        presenter = CourseSummariesPresenter()
        if input_course_ids:
            mock_api_response = [
                self._API_SUMMARIES[course_id]
                for course_id in input_course_ids
            ]
        else:
            mock_api_response = list(self._API_SUMMARIES.values())
        expected_summaries = [
            self._PRESENTER_SUMMARIES[course_id]
            for course_id in ouptut_course_ids
        ]

        with mock.patch(
                'analyticsclient.course_summaries.CourseSummaries.course_summaries',
                mock.Mock(return_value=mock_api_response)):
            actual_summaries, last_updated = presenter.get_course_summaries(
                course_ids=input_course_ids)
            for actual, expected in zip(actual_summaries, expected_summaries):
                self.assertCountEqual(actual, expected)
            self.assertEqual(last_updated, utils.CREATED_DATETIME)