Пример #1
0
    def get_queryset(self):
        """ List one course run
        ---
        parameters:
            - name: include_deleted_programs
              description: Will include deleted programs in the associated programs array
              required: false
              type: integer
              paramType: query
              multiple: false
        """
        q = self.request.query_params.get('q')
        partner = self.request.site.partner
        edit_mode = get_query_param(self.request, 'editable') or self.request.method not in SAFE_METHODS

        if edit_mode and q:
            raise EditableAndQUnsupported()

        if edit_mode and (not self.request.user.is_staff and not is_publisher_user(self.request.user)):
            raise PermissionDenied

        if edit_mode:
            queryset = CourseRun.objects.filter_drafts()
            queryset = CourseEditor.editable_course_runs(self.request.user, queryset)
        else:
            queryset = self.queryset

        if q:
            qs = SearchQuerySetWrapper(CourseRun.search(q).filter(partner=partner.short_code))
            # This is necessary to avoid issues with the filter backend.
            qs.model = self.queryset.model
            return qs

        queryset = queryset.filter(course__partner=partner)
        return self.get_serializer_class().prefetch_queryset(queryset=queryset)
Пример #2
0
    def get_queryset(self):
        """ List one course run
        ---
        parameters:
            - name: include_deleted_programs
              description: Will include deleted programs in the associated programs array
              required: false
              type: integer
              paramType: query
              multiple: false
        """
        q = self.request.query_params.get('q')
        partner = self.request.site.partner

        if q:
            qs = SearchQuerySetWrapper(
                CourseRun.search(q).filter(partner=partner.short_code))
            # This is necessary to avoid issues with the filter backend.
            qs.model = self.queryset.model
            return qs
        else:
            queryset = super(
                CourseRunViewSet,
                self).get_queryset().filter(course__partner=partner)
            return self.get_serializer_class().prefetch_queryset(
                queryset=queryset)
Пример #3
0
    def get_queryset(self):
        """ List one course run
        ---
        parameters:
            - name: include_deleted_programs
              description: Will include deleted programs in the associated programs array
              required: false
              type: integer
              paramType: query
              multiple: false
        """
        q = self.request.query_params.get('q')
        include_all_partners = get_query_param(self.request,
                                               'include_all_partners')
        partners = Partner.objects.all(
        ) if include_all_partners else Partner.objects.filter(
            site=self.request.site)
        edx_org_short_name = self.request.query_params.get('org')

        if q:
            qs = SearchQuerySetWrapper(
                CourseRun.search(q).filter(
                    partner__in=partners.values_list('short_code', flat=True)))
            # This is necessary to avoid issues with the filter backend.
            qs.model = self.queryset.model
            return qs
        else:
            queryset = super(
                CourseRunViewSet,
                self).get_queryset().filter(course__partner__in=partners)
            return self.get_serializer_class().prefetch_queryset(
                queryset=queryset, edx_org_short_name=edx_org_short_name)
Пример #4
0
    def contains_course_runs(self, course_run_ids):
        """
        Determines if the given course runs are contained in this catalog.

        Arguments:
            course_run_ids (str[]): List of course run IDs

        Returns:
            dict: Mapping of course IDs to booleans indicating if course run is
                  contained in this catalog.
        """
        contains = {course_run_id: False for course_run_id in course_run_ids}
        course_runs = CourseRun.search(self.query).filter(
            'terms', **{
                'key.raw': course_run_ids
            }).source(['key'])
        try:
            course_runs_keys = [i.key for i in course_runs]
        except RequestError:
            course_runs_keys = []
        contains.update({
            course_run_id: course_run_id in course_runs_keys
            for course_run_id in course_run_ids
        })

        return contains
Пример #5
0
    def contains(self, request):
        """
        Determine if course runs are found in the query results.

        A dictionary mapping course run keys to booleans,
        indicating course run presence, will be returned.
        ---
        serializer: serializers.ContainedCourseRunsSerializer
        parameters:
            - name: query
              description: Elasticsearch querystring query
              required: true
              type: string
              paramType: query
              multiple: false
            - name: course_run_ids
              description: Comma-separated list of course run IDs
              required: true
              type: string
              paramType: query
              multiple: true
        """
        query = request.GET.get('query')
        course_run_ids = request.GET.get('course_run_ids')

        if query and course_run_ids:
            course_run_ids = course_run_ids.split(',')
            course_runs = CourseRun.search(query).filter(key__in=course_run_ids).values_list('key', flat=True)
            contains = {course_run_id: course_run_id in course_runs for course_run_id in course_run_ids}

            instance = {'course_runs': contains}
            serializer = serializers.ContainedCourseRunsSerializer(instance)
            return Response(serializer.data)
        return Response(status=status.HTTP_400_BAD_REQUEST)
Пример #6
0
    def get_queryset(self):
        """ List one course run
        ---
        parameters:
            - name: include_deleted_programs
              description: Will include deleted programs in the associated programs array
              required: false
              type: integer
              paramType: query
              multiple: false
        """
        q = self.request.query_params.get('q', None)
        partner = self.get_partner()

        if q:
            qs = SearchQuerySetWrapper(
                CourseRun.search(q).filter(partner=partner.short_code))
            # This is necessary to avoid issues with the filter backend.
            qs.model = self.queryset.model
            return qs
        else:
            queryset = super(
                CourseRunViewSet,
                self).get_queryset().filter(course__partner=partner)
            queryset = queryset.select_related(
                *serializers.SELECT_RELATED_FIELDS['course_run'])
            queryset = queryset.prefetch_related(
                *serializers.PREFETCH_FIELDS['course_run'])
            return queryset
Пример #7
0
    def get_queryset(self):
        """ List one course run
        ---
        parameters:
            - name: include_deleted_programs
              description: Will include deleted programs in the associated programs array
              required: false
              type: integer
              paramType: query
              multiple: false
        """
        q = self.request.query_params.get('q')
        partner = self.request.site.partner
        edit_mode = get_query_param(self.request, 'editable') or self.request.method not in SAFE_METHODS

        if edit_mode and q:
            raise EditableAndQUnsupported()

        if edit_mode:
            queryset = CourseRun.objects.filter_drafts()
            queryset = CourseEditor.editable_course_runs(self.request.user, queryset)
        else:
            queryset = self.queryset

        if q:
            queryset = CourseRun.search(q, queryset=queryset)

        queryset = queryset.filter(course__partner=partner)
        return self.get_serializer_class().prefetch_queryset(queryset=queryset)
 def test_search(self):
     """ Verify the method returns a filtered queryset of course runs. """
     title = 'Some random title'
     course_runs = factories.CourseRunFactory.create_batch(3, title=title)
     query = 'title:' + title
     actual_sorted = sorted(SearchQuerySetWrapper(CourseRun.search(query)), key=lambda course_run: course_run.key)
     expected_sorted = sorted(course_runs, key=lambda course_run: course_run.key)
     self.assertEqual(actual_sorted, expected_sorted)
Пример #9
0
 def get_queryset(self):
     q = self.request.query_params.get('q', None)
     if q:
         qs = SearchQuerySetWrapper(CourseRun.search(q))
         # This is necessary to avoid issues with the filter backend.
         qs.model = self.queryset.model
         return qs
     else:
         return super(CourseRunViewSet, self).get_queryset()
Пример #10
0
 def test_search(self):
     """ Verify the method returns a filtered queryset of course runs. """
     title = 'Some random title'
     course_runs = factories.CourseRunFactory.create_batch(3, title=title)
     query = 'title:' + title
     actual_sorted = sorted(SearchQuerySetWrapper(CourseRun.search(query)),
                            key=lambda course_run: course_run.key)
     expected_sorted = sorted(course_runs,
                              key=lambda course_run: course_run.key)
     self.assertEqual(actual_sorted, expected_sorted)
Пример #11
0
    def contains_course_runs(self, course_run_ids):  # pylint: disable=unused-argument
        """
        Determines if the given course runs are contained in this catalog.

        Arguments:
            course_run_ids (str[]): List of course run IDs

        Returns:
            dict: Mapping of course IDs to booleans indicating if course run is
                  contained in this catalog.
        """
        contains = {course_run_id: False for course_run_id in course_run_ids}
        course_runs = CourseRun.search(self.query).filter(key__in=course_run_ids).values_list('key', flat=True)
        contains.update({course_run_id: course_run_id in course_runs for course_run_id in course_run_ids})

        return contains
Пример #12
0
    def contains(self, request):
        """
        Determine if course runs are found in the query results.

        A dictionary mapping course run keys to booleans,
        indicating course run presence, will be returned.
        ---
        serializer: serializers.ContainedCourseRunsSerializer
        parameters:
            - name: query
              description: Elasticsearch querystring query
              required: true
              type: string
              paramType: query
              multiple: false
            - name: course_run_ids
              description: Comma-separated list of course run IDs
              required: true
              type: string
              paramType: query
              multiple: true
            - name: partner
              description: Filter by partner
              required: false
              type: string
              paramType: query
              multiple: false
        """
        query = request.GET.get('query')
        course_run_ids = request.GET.get('course_run_ids')
        partner = self.request.site.partner

        if query and course_run_ids:
            course_run_ids = course_run_ids.split(',')
            course_runs = (CourseRun.search(query).filter(
                ESDSLQ('term', partner=partner.short_code)
                & ESDSLQ('terms', **{'key.raw': course_run_ids})).source(
                    ['key']))
            course_runs_keys = [i.key for i in course_runs]
            contains = {
                course_run_id: course_run_id in course_runs_keys
                for course_run_id in course_run_ids
            }
            instance = {'course_runs': contains}
            serializer = serializers.ContainedCourseRunsSerializer(instance)
            return Response(serializer.data)
        return Response(status=status.HTTP_400_BAD_REQUEST)
Пример #13
0
    def contains(self, request):
        """
        Determine if course runs are found in the query results.

        A dictionary mapping course run keys to booleans,
        indicating course run presence, will be returned.
        ---
        serializer: serializers.ContainedCourseRunsSerializer
        parameters:
            - name: query
              description: Elasticsearch querystring query
              required: true
              type: string
              paramType: query
              multiple: false
            - name: course_run_ids
              description: Comma-separated list of course run IDs
              required: true
              type: string
              paramType: query
              multiple: true
            - name: partner
              description: Filter by partner
              required: false
              type: string
              paramType: query
              multiple: false
        """
        query = request.GET.get('query')
        course_run_ids = request.GET.get('course_run_ids')
        partner = self.get_partner()

        if query and course_run_ids:
            course_run_ids = course_run_ids.split(',')
            course_runs = CourseRun.search(query).filter(partner=partner.short_code).filter(key__in=course_run_ids). \
                values_list('key', flat=True)
            contains = {
                course_run_id: course_run_id in course_runs
                for course_run_id in course_run_ids
            }

            instance = {'course_runs': contains}
            serializer = serializers.ContainedCourseRunsSerializer(instance)
            return Response(serializer.data)
        return Response(status=status.HTTP_400_BAD_REQUEST)
Пример #14
0
    def get(self, request):
        """
        Determine if a set of courses and/or course runs is found in the query results.

        Returns
            dict:  mapping of course and run indentifiers included in the request to boolean values
                indicating whether or not the associated course or run is contained in the queryset
                described by the query found in the request.
        """
        query = request.GET.get('query')
        course_run_ids = request.GET.get('course_run_ids', None)
        course_uuids = request.GET.get('course_uuids', None)
        partner = self.request.site.partner

        if query and (course_run_ids or course_uuids):
            identified_course_ids = set()
            specified_course_ids = []
            if course_run_ids:
                course_run_ids = course_run_ids.split(',')
                specified_course_ids = course_run_ids
                identified_course_ids.update(
                    i.key for i in CourseRun.search(query).filter(
                        ESDSLQ('term', partner=partner.short_code)
                        | ESDSLQ('terms', **{'key.raw': course_run_ids})).
                    source(['key']))
            if course_uuids:
                course_uuids = [
                    UUID(course_uuid)
                    for course_uuid in course_uuids.split(',')
                ]
                specified_course_ids += course_uuids
                identified_course_ids.update(
                    Course.search(query).filter(
                        partner=partner,
                        uuid__in=course_uuids).values_list('uuid', flat=True))

            contains = {
                str(identifier): identifier in identified_course_ids
                for identifier in specified_course_ids
            }
            return Response(contains)
        return Response(
            'CatalogQueryContains endpoint requires query and identifiers list(s)',
            status=status.HTTP_400_BAD_REQUEST)
Пример #15
0
    def get_queryset(self):
        """ List one course run
        ---
        parameters:
            - name: include_deleted_programs
              description: Will include deleted programs in the associated programs array
              required: false
              type: integer
              paramType: query
              multiple: false
        """
        q = self.request.query_params.get("q", None)
        partner = self.get_partner()

        if q:
            qs = SearchQuerySetWrapper(CourseRun.search(q).filter(partner=partner.short_code))
            # This is necessary to avoid issues with the filter backend.
            qs.model = self.queryset.model
            return qs
        else:
            queryset = super(CourseRunViewSet, self).get_queryset().filter(course__partner=partner)
            queryset = queryset.select_related(*serializers.SELECT_RELATED_FIELDS["course_run"])
            queryset = queryset.prefetch_related(*serializers.PREFETCH_FIELDS["course_run"])
            return queryset
Пример #16
0
    def contains(self, request):
        """
        Determine if course runs are found in the query results.

        A dictionary mapping course run keys to booleans,
        indicating course run presence, will be returned.
        ---
        serializer: serializers.ContainedCourseRunsSerializer
        parameters:
            - name: query
              description: Elasticsearch querystring query
              required: true
              type: string
              paramType: query
              multiple: false
            - name: course_run_ids
              description: Comma-separated list of course run IDs
              required: true
              type: string
              paramType: query
              multiple: true
            - name: partner
              description: Filter by partner
              required: false
              type: string
              paramType: query
              multiple: false
            - name: org
              description: Filter results on edx organization's short name.
              required: false
              type: string
              paramType: query
              multiple: false
            - name: include_all_partners
              description: Include courses for all partners.
              required: false
              type: integer
              paramType: query
              multiple: false
        """
        query = request.GET.get('query')
        course_run_ids = request.GET.get('course_run_ids')
        include_all_partners = get_query_param(self.request,
                                               'include_all_partners')
        partners = Partner.objects.all(
        ) if include_all_partners else Partner.objects.filter(
            site=self.request.site)
        edx_org_short_name = request.GET.get('org')

        if query and course_run_ids:
            course_run_ids = course_run_ids.split(',')
            course_runs = CourseRun.search(query).filter(
                partner__in=partners.values_list(
                    'short_code', flat=True)).filter(key__in=course_run_ids)
            # update "course_runs" with edx organization filter
            course_runs = course_runs.filter(
                course__authoring_organizations__key=edx_org_short_name
            ).values_list('key', flat=True)
            contains = {
                course_run_id: course_run_id in course_runs
                for course_run_id in course_run_ids
            }

            instance = {'course_runs': contains}
            serializer = serializers.ContainedCourseRunsSerializer(instance)
            return Response(serializer.data)
        return Response(status=status.HTTP_400_BAD_REQUEST)