def setup(self, db): self.course_overview = CourseOverviewFactory() self.users = [UserFactory(), UserFactory()] self.course_access_roles = [ CourseAccessRoleFactory(user=self.users[0], course_id=self.course_overview.id, role='staff'), CourseAccessRoleFactory(user=self.users[1], course_id=self.course_overview.id, role='administrator'), ] self.serializer = CourseDetailsSerializer( instance=self.course_overview) self.expected_fields = [ 'course_id', 'course_name', 'course_code', 'org', 'start_date', 'end_date', 'self_paced', 'staff', 'average_progress', 'learners_enrolled', 'average_days_to_complete', 'users_completed', ]
def test_get_staff_with_no_course(self): '''Create a serializer for a course with a different ID than for the data we set up. This simulates when there are no staff members for the given course, which will have a different ID than the one we created in the setup method ''' assert CourseDetailsSerializer().get_staff( CourseOverviewFactory()) == []
def retrieve(self, request, *args, **kwargs): # NOTE: Duplicating code in GeneralCourseDataViewSet. Candidate to dry up # Make it a decorator course_id_str = kwargs.get('pk', '') course_key = CourseKey.from_string(course_id_str.replace(' ', '+')) site = django.contrib.sites.shortcuts.get_current_site(request) if figures.helpers.is_multisite(): if site != figures.sites.get_site_for_course(course_key): # Raising NotFound instead of PermissionDenied raise NotFound() course_overview = get_object_or_404(CourseOverview, pk=course_key) return Response(CourseDetailsSerializer(course_overview).data)