def test_purge_an_instance(self, logger, debug_mock, settings):
     purge_an_instance("http://NinaFox")
     self.assertTrue('CacheDebug: Delete cache keys related to pattern' in str(debug_mock.call_args_list))
     self.assertEqual(retrieve(u"_@@_@@http://Charles@@xubiru##instance"), {})
     self.assertEqual(retrieve(u"_@@_@@http://NinaFox@@class_uri=http://dog##instance"), None)
     self.assertEqual(retrieve(u"_@@_@@http://NinaFox@@a=1&b=2##instance"), None)
     self.assertEqual(retrieve(u"_@@_@@http://NinaFox@@abc##instance"), None)
Exemple #2
0
    def delete(self, context_name, class_name, instance_id):
        valid_params = INSTANCE_PARAMS
        with safe_params(valid_params):
            self.query_params = ParamDict(self,
                                          context_name=context_name,
                                          class_name=class_name,
                                          instance_id=instance_id,
                                          **valid_params)
        del context_name
        del class_name
        del instance_id

        deleted = delete_instance(self.query_params)
        if deleted:
            response = 204
            if settings.NOTIFY_BUS:
                self._notify_bus(action="DELETE")
            cache.purge_an_instance(self.query_params['instance_uri'])
        else:
            msg = _(u"Instance ({0}) of class ({1}) in graph ({2}) was not found.")
            error_message = msg.format(self.query_params["instance_uri"],
                                       self.query_params["class_uri"],
                                       self.query_params["graph_uri"])
            raise HTTPError(404, log_message=error_message)
        self.finalize(response)
Exemple #3
0
    def delete(self, context_name, class_name, instance_id):
        valid_params = INSTANCE_PARAMS
        with safe_params(valid_params):
            self.query_params = ParamDict(self,
                                          context_name=context_name,
                                          class_name=class_name,
                                          instance_id=instance_id,
                                          **valid_params)
        del context_name
        del class_name
        del instance_id

        deleted = delete_instance(self.query_params)
        if deleted:
            response = 204
            if settings.NOTIFY_BUS:
                self._notify_bus(action="DELETE")
            cache.purge_an_instance(self.query_params['instance_uri'])
        else:
            msg = _(u"Instance ({0}) of class ({1}) in graph ({2}) was not found.")
            error_message = msg.format(self.query_params["instance_uri"],
                                       self.query_params["class_uri"],
                                       self.query_params["graph_uri"])
            raise HTTPError(404, log_message=error_message)
        self.finalize(response)
Exemple #4
0
    def put(self, context_name, class_name, instance_id):
        valid_params = INSTANCE_PARAMS
        with safe_params(valid_params):
            self.query_params = ParamDict(self,
                                          context_name=context_name,
                                          class_name=class_name,
                                          instance_id=instance_id,
                                          **valid_params)
        del context_name
        del class_name
        del instance_id

        instance_data = get_json_request_as_dict(self.request.body)
        instance_data = normalize_all_uris_recursively(instance_data)

        rdf_type_error = is_rdf_type_invalid(self.query_params, instance_data)
        if rdf_type_error:
            raise HTTPError(400, log_message=rdf_type_error)

        try:
            if not instance_exists(self.query_params):
                try:
                    schema = schema_resource.get_cached_schema(
                        self.query_params)
                except SchemaNotFound:
                    schema = None
                if schema is None:
                    msg = _(u"Class {0} doesn't exist in graph {1}.")
                    raise HTTPError(404,
                                    log_message=msg.format(
                                        self.query_params["class_uri"],
                                        self.query_params["graph_uri"]))
                instance_uri, instance_id = create_instance(
                    self.query_params, instance_data,
                    self.query_params["instance_uri"])
                resource_url = self.request.full_url()
                status = 201
                self.set_header("location", resource_url)
                self.set_header("X-Brainiak-Resource-URI", instance_uri)
            else:
                edit_instance(self.query_params, instance_data)
                status = 200
        except InstanceError as ex:
            raise HTTPError(400, log_message=str(ex))
        except SchemaNotFound as ex:
            raise HTTPError(404, log_message=str(ex))

        cache.purge_an_instance(self.query_params['instance_uri'])

        self.query_params["expand_object_properties"] = "1"
        instance_data = get_instance(self.query_params)

        if instance_data and settings.NOTIFY_BUS:
            self.query_params["instance_uri"] = instance_data["@id"]
            self._notify_bus(action="PUT", instance_data=instance_data)

        self.finalize(status)
Exemple #5
0
    def patch(self, context_name, class_name, instance_id):
        valid_params = INSTANCE_PARAMS
        with safe_params(valid_params):
            self.query_params = ParamDict(self,
                                          context_name=context_name,
                                          class_name=class_name,
                                          instance_id=instance_id,
                                          **valid_params)
        del context_name
        del class_name
        del instance_id

        patch_list = get_json_request_as_dict(self.request.body)

        # Retrieve original data
        instance_data = memoize(self.query_params,
                                get_instance,
                                key=build_instance_key(self.query_params),
                                function_arguments=self.query_params)

        if instance_data is not None:
            # Editing an instance
            instance_data = instance_data['body']
            instance_data.pop(
                'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', None)

            # compute patch
            changed_data = apply_patch(instance_data, patch_list)

            # Try to put
            edit_instance(self.query_params, changed_data)
            status = 200

            # Clear cache
            cache.purge_an_instance(self.query_params['instance_uri'])

            self.finalize(status)
        else:
            # Creating a new instance from patch list
            instance_data = get_instance_data_from_patch_list(patch_list)
            instance_data = normalize_all_uris_recursively(instance_data)

            rdf_type_error = is_rdf_type_invalid(self.query_params,
                                                 instance_data)
            if rdf_type_error:
                raise HTTPError(400, log_message=rdf_type_error)

            instance_uri, instance_id = create_instance(
                self.query_params, instance_data,
                self.query_params["instance_uri"])
            resource_url = self.request.full_url()
            status = 201
            self.set_header("location", resource_url)
            self.set_header("X-Brainiak-Resource-URI", instance_uri)

            self.finalize(status)
 def test_purge_an_instance(self, logger, debug_mock, settings):
     purge_an_instance("http://NinaFox")
     self.assertTrue('CacheDebug: Delete cache keys related to pattern' in
                     str(debug_mock.call_args_list))
     self.assertEqual(retrieve(u"_@@_@@http://Charles@@xubiru##instance"),
                      {})
     self.assertEqual(
         retrieve(u"_@@_@@http://NinaFox@@class_uri=http://dog##instance"),
         None)
     self.assertEqual(retrieve(u"_@@_@@http://NinaFox@@a=1&b=2##instance"),
                      None)
     self.assertEqual(retrieve(u"_@@_@@http://NinaFox@@abc##instance"),
                      None)
Exemple #7
0
    def patch(self, context_name, class_name, instance_id):
        valid_params = INSTANCE_PARAMS
        with safe_params(valid_params):
            self.query_params = ParamDict(self,
                                          context_name=context_name,
                                          class_name=class_name,
                                          instance_id=instance_id,
                                          **valid_params)
        del context_name
        del class_name
        del instance_id

        try:
            patch_list = json.loads(self.request.body)
        except ValueError:
            raise HTTPError(400,
                            log_message=_("No JSON object could be decoded"))

        # Retrieve original data
        instance_data = memoize(self.query_params,
                                get_instance,
                                key=build_instance_key(self.query_params),
                                function_arguments=self.query_params)
        try:
            instance_data = instance_data['body']
        except TypeError:
            raise HTTPError(404, log_message=_("Inexistent instance"))

        instance_data.pop('http://www.w3.org/1999/02/22-rdf-syntax-ns#type',
                          None)

        # compute patch
        changed_data = apply_patch(instance_data, patch_list)

        # Try to put
        edit_instance(self.query_params, changed_data)
        status = 200

        # Clear cache
        cache.purge_an_instance(self.query_params['instance_uri'])

        self.finalize(status)
Exemple #8
0
    def patch(self, context_name, class_name, instance_id):
        valid_params = INSTANCE_PARAMS
        with safe_params(valid_params):
            self.query_params = ParamDict(self,
                                          context_name=context_name,
                                          class_name=class_name,
                                          instance_id=instance_id,
                                          **valid_params)
        del context_name
        del class_name
        del instance_id

        try:
            patch_list = json.loads(self.request.body)
        except ValueError:
            raise HTTPError(400, log_message=_("No JSON object could be decoded"))

        # Retrieve original data
        instance_data = memoize(self.query_params,
                           get_instance,
                           key=build_instance_key(self.query_params),
                           function_arguments=self.query_params)
        try:
            instance_data = instance_data['body']
        except TypeError:
            raise HTTPError(404, log_message=_("Inexistent instance"))

        instance_data.pop('http://www.w3.org/1999/02/22-rdf-syntax-ns#type', None)

        # compute patch
        changed_data = apply_patch(instance_data, patch_list)

        # Try to put
        edit_instance(self.query_params, changed_data)
        status = 200

        # Clear cache
        cache.purge_an_instance(self.query_params['instance_uri'])

        self.finalize(status)
Exemple #9
0
    def put(self, context_name, class_name, instance_id):
        valid_params = INSTANCE_PARAMS
        with safe_params(valid_params):
            self.query_params = ParamDict(self,
                                          context_name=context_name,
                                          class_name=class_name,
                                          instance_id=instance_id,
                                          **valid_params)
        del context_name
        del class_name
        del instance_id

        try:
            instance_data = json.loads(self.request.body)
        except ValueError:
            raise HTTPError(400,
                            log_message=_("No JSON object could be decoded"))

        instance_data = normalize_all_uris_recursively(instance_data)

        RDFS_TYPE = "http://www.w3.org/2000/01/rdf-schema#type"
        rdfs_type = instance_data.get(RDFS_TYPE)

        if rdfs_type:
            class_uri = self.query_params["class_uri"]
            if (rdfs_type == class_uri):
                instance_data.pop(RDFS_TYPE)
            else:
                msg = u"Incompatible values for rdfs:type <{0}> and class URI <{1}>"
                msg = msg.format(rdfs_type, class_uri)
                raise HTTPError(400, log_message=msg)

        try:
            if not instance_exists(self.query_params):
                try:
                    schema = schema_resource.get_cached_schema(
                        self.query_params)
                except SchemaNotFound:
                    schema = None
                if schema is None:
                    msg = _(u"Class {0} doesn't exist in graph {1}.")
                    raise HTTPError(404,
                                    log_message=msg.format(
                                        self.query_params["class_uri"],
                                        self.query_params["graph_uri"]))
                instance_uri, instance_id = create_instance(
                    self.query_params, instance_data,
                    self.query_params["instance_uri"])
                resource_url = self.request.full_url()
                status = 201
                self.set_header("location", resource_url)
                self.set_header("X-Brainiak-Resource-URI", instance_uri)
            else:
                edit_instance(self.query_params, instance_data)
                status = 200
        except InstanceError as ex:
            raise HTTPError(400, log_message=unicode(ex))
        except SchemaNotFound as ex:
            raise HTTPError(404, log_message=unicode(ex))

        cache.purge_an_instance(self.query_params['instance_uri'])

        self.query_params["expand_object_properties"] = "1"
        instance_data = get_instance(self.query_params)

        if instance_data and settings.NOTIFY_BUS:
            self.query_params["instance_uri"] = instance_data["@id"]
            self._notify_bus(action="PUT", instance_data=instance_data)

        self.finalize(status)
Exemple #10
0
    def put(self, context_name, class_name, instance_id):
        valid_params = INSTANCE_PARAMS
        with safe_params(valid_params):
            self.query_params = ParamDict(self,
                                          context_name=context_name,
                                          class_name=class_name,
                                          instance_id=instance_id,
                                          **valid_params)
        del context_name
        del class_name
        del instance_id

        try:
            instance_data = json.loads(self.request.body)
        except ValueError:
            raise HTTPError(400, log_message=_("No JSON object could be decoded"))

        instance_data = normalize_all_uris_recursively(instance_data)

        RDFS_TYPE = "http://www.w3.org/2000/01/rdf-schema#type"
        rdfs_type = instance_data.get(RDFS_TYPE)

        if rdfs_type:
            class_uri = self.query_params["class_uri"]
            if (rdfs_type == class_uri):
                instance_data.pop(RDFS_TYPE)
            else:
                msg = u"Incompatible values for rdfs:type <{0}> and class URI <{1}>"
                msg = msg.format(rdfs_type, class_uri)
                raise HTTPError(400, log_message=msg)

        try:
            if not instance_exists(self.query_params):
                try:
                    schema = schema_resource.get_cached_schema(self.query_params)
                except SchemaNotFound:
                    schema = None
                if schema is None:
                    msg = _(u"Class {0} doesn't exist in graph {1}.")
                    raise HTTPError(404, log_message=msg.format(self.query_params["class_uri"],
                                                                self.query_params["graph_uri"]))
                instance_uri, instance_id = create_instance(self.query_params, instance_data, self.query_params["instance_uri"])
                resource_url = self.request.full_url()
                status = 201
                self.set_header("location", resource_url)
                self.set_header("X-Brainiak-Resource-URI", instance_uri)
            else:
                edit_instance(self.query_params, instance_data)
                status = 200
        except InstanceError as ex:
            raise HTTPError(400, log_message=unicode(ex))
        except SchemaNotFound as ex:
            raise HTTPError(404, log_message=unicode(ex))

        cache.purge_an_instance(self.query_params['instance_uri'])

        self.query_params["expand_object_properties"] = "1"
        instance_data = get_instance(self.query_params)

        if instance_data and settings.NOTIFY_BUS:
            self.query_params["instance_uri"] = instance_data["@id"]
            self._notify_bus(action="PUT", instance_data=instance_data)

        self.finalize(status)