示例#1
0
    def create(self, request, *args, **kwargs):  # noqa pylint: disable=unused-argument
        try:
            lr_id = int(request.data['id'])
        except ValueError:
            raise ValidationError("LearningResource id must be a number")
        repo_slug = self.kwargs['repo_slug']

        learning_resource = get_resource(lr_id, self.request.user.id)
        if learning_resource.course.repository.slug != repo_slug:
            raise DjangoPermissionDenied()

        if EXPORTS_KEY not in self.request.session:
            self.request.session[EXPORTS_KEY] = {}

        if repo_slug not in self.request.session[EXPORTS_KEY]:
            self.request.session[EXPORTS_KEY][repo_slug] = []

        exports = self.request.session[EXPORTS_KEY][repo_slug]
        if lr_id not in exports:
            exports.append(lr_id)
            self.request.session.modified = True

        url = reverse('learning-resource-export-detail',
                      kwargs={'repo_slug': self.kwargs['repo_slug'],
                              'username': self.kwargs['username'],
                              'lr_id': lr_id})
        headers = {"Location": url}
        return Response(
            {"id": lr_id}, status=status.HTTP_201_CREATED, headers=headers)
示例#2
0
 def test_get_resource(self):
     """Get a resource"""
     resource_id = LearningResource.objects.all()[0].id
     self.assertTrue(
         isinstance(
             api.get_resource(resource_id, self.user.id),
             LearningResource
         )
     )
示例#3
0
 def test_get_no_permission_resource(self):
     """Get a resource"""
     resource_id = LearningResource.objects.all()[0].id
     with self.assertRaises(api.PermissionDenied):
         api.get_resource(resource_id, self.user_norepo.id)
示例#4
0
 def test_get_missing_resource(self):
     """Get a resource"""
     resource_id = LearningResource.objects.all().order_by("-id")[0].id + 1
     with self.assertRaises(api.NotFound):
         api.get_resource(resource_id, self.user.id)
    def test_learning_resource_filter(self):
        """Test for filtering LearningResources by ids."""

        def get_filtered(ids=None, vocab_slug=None, types=None,
                         expected_status=HTTP_200_OK):
            """Return list of LearningResources in shopping cart."""
            url_base = "{repo_base}{repo_slug}/learning_resources/".format(
                repo_base=REPO_BASE,
                repo_slug=self.repo.slug,
            )

            params = []
            if ids is not None:
                params.append("id={ids}".format(
                    ids=",".join([str(s) for s in ids])
                ))

            if vocab_slug is not None:
                params.append("vocab_slug={slug}".format(
                    slug=vocab_slug
                ))

            if types is not None:
                for type_name in types:
                    params.append("type_name={name}".format(name=type_name))

            params_line = ""
            if params:
                params_line = "?" + "&".join(params)
            resp = self.client.get("{url_base}{params_line}".format(
                url_base=url_base,
                params_line=params_line
            ))
            self.assertEqual(expected_status, resp.status_code)
            if expected_status == HTTP_200_OK:
                return sorted([x['id'] for x in as_json(resp)['results']])

        # Filter by id
        self.import_course_tarball(self.repo)
        self.assertEqual([], get_filtered(ids=[]))
        get_filtered(ids=["not-a-number"],
                     expected_status=HTTP_400_BAD_REQUEST)
        self.assertEqual([], get_filtered(ids=[-1]))

        all_ids = list(get_resources(
            self.repo.id).values_list('id', flat=True))
        self.assertEqual(sorted(all_ids), get_filtered(ids=all_ids))
        self.assertEqual(
            sorted(all_ids[:5]), get_filtered(ids=all_ids[:5]))

        # Assign term to resource
        vocab1_slug = self.create_vocabulary(self.repo.slug)['slug']
        term1_slug = self.create_term(self.repo.slug, vocab1_slug)['slug']

        vocab2_dict = dict(self.DEFAULT_VOCAB_DICT)
        vocab2_dict['name'] = 'two'
        vocab2_slug = self.create_vocabulary(
            self.repo.slug, vocab2_dict)['slug']
        term2_slug = self.create_term(
            self.repo.slug, vocab2_slug)['slug']

        resource1 = get_resource(all_ids[0], self.user.id)
        resource2 = get_resource(all_ids[1], self.user.id)

        # Verify no vocabularies come up in filter
        self.assertEqual([], get_filtered(vocab_slug=vocab1_slug))
        self.assertEqual([], get_filtered(vocab_slug=vocab2_slug))

        # Assign terms
        resource1.terms.add(Term.objects.get(slug=term1_slug))
        resource2.terms.add(Term.objects.get(slug=term2_slug))

        # Filter by vocabulary
        self.assertEqual(
            [resource1.id], get_filtered(vocab_slug=vocab1_slug)
        )
        self.assertEqual(
            [resource1.id], get_filtered(
                ids=[resource1.id], vocab_slug=vocab1_slug
            )
        )
        self.assertEqual(
            [], get_filtered(
                ids=[resource2.id], vocab_slug=vocab1_slug
            )
        )
        self.assertEqual(
            [], get_filtered(
                ids=[resource1.id], vocab_slug=vocab2_slug
            )
        )

        # Filter by type
        self.assertNotEqual(
            resource1.learning_resource_type.name,
            resource2.learning_resource_type.name
        )
        self.assertEqual(
            sorted([lr.id for lr in LearningResource.objects.filter(
                learning_resource_type=resource1.learning_resource_type
            )]), get_filtered(
                types=[resource1.learning_resource_type.name]
            )
        )
        self.assertEqual(
            sorted([lr.id for lr in LearningResource.objects.filter(
                learning_resource_type=resource2.learning_resource_type
            )]), get_filtered(
                types=[resource2.learning_resource_type.name]
            )
        )
        self.assertEqual(
            [], get_filtered(
                types=["missing"]
            )
        )
示例#6
0
    def test_learning_resource_filter(self):
        """Test for filtering LearningResources by ids."""

        def get_filtered(ids=None, vocab_slug=None, types=None,
                         expected_status=HTTP_200_OK):
            """Return list of LearningResources in shopping cart."""
            url_base = "{repo_base}{repo_slug}/learning_resources/".format(
                repo_base=REPO_BASE,
                repo_slug=self.repo.slug,
            )

            params = []
            if ids is not None:
                params.append("id={ids}".format(
                    ids=",".join([str(s) for s in ids])
                ))

            if vocab_slug is not None:
                params.append("vocab_slug={slug}".format(
                    slug=vocab_slug
                ))

            if types is not None:
                for type_name in types:
                    params.append("type_name={name}".format(name=type_name))

            params_line = ""
            if params:
                params_line = "?" + "&".join(params)
            resp = self.client.get("{url_base}{params_line}".format(
                url_base=url_base,
                params_line=params_line
            ))
            self.assertEqual(expected_status, resp.status_code)
            if expected_status == HTTP_200_OK:
                return sorted([x['id'] for x in as_json(resp)['results']])

        # Filter by id
        self.import_course_tarball(self.repo)
        self.assertEqual([], get_filtered(ids=[]))
        get_filtered(ids=["not-a-number"],
                     expected_status=HTTP_400_BAD_REQUEST)
        self.assertEqual([], get_filtered(ids=[-1]))

        all_ids = list(get_resources(
            self.repo.id).values_list('id', flat=True))
        self.assertEqual(sorted(all_ids), get_filtered(ids=all_ids))
        self.assertEqual(
            sorted(all_ids[:5]), get_filtered(ids=all_ids[:5]))

        # Assign term to resource
        vocab1_slug = self.create_vocabulary(self.repo.slug)['slug']
        term1_slug = self.create_term(self.repo.slug, vocab1_slug)['slug']

        vocab2_dict = dict(self.DEFAULT_VOCAB_DICT)
        vocab2_dict['name'] = 'two'
        vocab2_slug = self.create_vocabulary(
            self.repo.slug, vocab2_dict)['slug']
        term2_slug = self.create_term(
            self.repo.slug, vocab2_slug)['slug']

        resource1 = get_resource(all_ids[0], self.user.id)
        resource2 = get_resource(all_ids[1], self.user.id)

        # Verify no vocabularies come up in filter
        self.assertEqual([], get_filtered(vocab_slug=vocab1_slug))
        self.assertEqual([], get_filtered(vocab_slug=vocab2_slug))

        # Assign terms
        resource1.terms.add(Term.objects.get(slug=term1_slug))
        resource2.terms.add(Term.objects.get(slug=term2_slug))

        # Filter by vocabulary
        self.assertEqual(
            [resource1.id], get_filtered(vocab_slug=vocab1_slug)
        )
        self.assertEqual(
            [resource1.id], get_filtered(
                ids=[resource1.id], vocab_slug=vocab1_slug
            )
        )
        self.assertEqual(
            [], get_filtered(
                ids=[resource2.id], vocab_slug=vocab1_slug
            )
        )
        self.assertEqual(
            [], get_filtered(
                ids=[resource1.id], vocab_slug=vocab2_slug
            )
        )

        # Filter by type
        self.assertNotEqual(
            resource1.learning_resource_type.name,
            resource2.learning_resource_type.name
        )
        self.assertEqual(
            sorted([lr.id for lr in LearningResource.objects.filter(
                learning_resource_type=resource1.learning_resource_type
            )]), get_filtered(
                types=[resource1.learning_resource_type.name]
            )
        )
        self.assertEqual(
            sorted([lr.id for lr in LearningResource.objects.filter(
                learning_resource_type=resource2.learning_resource_type
            )]), get_filtered(
                types=[resource2.learning_resource_type.name]
            )
        )
        self.assertEqual(
            [], get_filtered(
                types=["missing"]
            )
        )