コード例 #1
0
    def put(self, request, fileuuid):
        """
        Requires a file UUID, and document body must be a JSON-encoded list.
        Replaces the list of tags in the record with the provided list.
        Returns {"success": true} on success.
        Returns 400 if no JSON document is provided in the request body, if the body can't be decoded, or if the body is any JSON object other than a list.
        """
        try:
            tags = json.load(request)
        except ValueError:
            response = {
                "success": False,
                "message": "No JSON document could be decoded from the request.",
            }
            return helpers.json_response(response, status_code=400)
        if not isinstance(tags, list):
            response = {
                "success": False,
                "message": "The request body must be an array.",
            }
            return helpers.json_response(response, status_code=400)

        try:
            es_client = elasticSearchFunctions.get_client()
            elasticSearchFunctions.set_file_tags(es_client, fileuuid, tags)
        except elasticSearchFunctions.ElasticsearchError as e:
            response = {"success": False, "message": str(e)}
            if isinstance(e, elasticSearchFunctions.EmptySearchResultError):
                status_code = 404
            else:
                status_code = 400
            return helpers.json_response(response, status_code=status_code)
        return helpers.json_response({"success": True})
コード例 #2
0
 def delete(self, request, fileuuid):
     """
     Request a file UUID.
     Deletes all tags for the given file.
     """
     try:
         es_client = elasticSearchFunctions.get_client()
         elasticSearchFunctions.set_file_tags(es_client, fileuuid, [])
     except elasticSearchFunctions.ElasticsearchError as e:
         response = {"success": False, "message": str(e)}
         if isinstance(e, elasticSearchFunctions.EmptySearchResultError):
             status_code = 404
         else:
             status_code = 400
         return helpers.json_response(response, status_code=status_code)
     return helpers.json_response({"success": True})
コード例 #3
0
 def test_set_tags_fails_when_file_cant_be_found(self):
     with pytest.raises(elasticSearchFunctions.EmptySearchResultError):
         elasticSearchFunctions.set_file_tags(self.client, 'no_such_file',
                                              [])
コード例 #4
0
 def test_set_get_tags(self):
     elasticSearchFunctions.set_file_tags(self.client, self.file_uuid,
                                          ['test'])
     assert elasticSearchFunctions.get_file_tags(
         self.client, self.file_uuid) == ['test']
コード例 #5
0
 def test_set_tags(self):
     elasticSearchFunctions.set_file_tags(
         self.client, '2101fa74-bc27-405b-8e29-614ebd9d5a89', ['test'])
     assert elasticSearchFunctions.get_file_tags(
         self.client, '2101fa74-bc27-405b-8e29-614ebd9d5a89') == ['test']