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)
def post(self, context_name, class_name): valid_params = CLASS_PARAMS with safe_params(valid_params): self.query_params = ParamDict(self, context_name=context_name, class_name=class_name, **valid_params) del context_name del class_name try: schema = schema_resource.get_cached_schema(self.query_params) except SchemaNotFound: schema = None if schema is None: class_uri = self.query_params["class_uri"] graph_uri = self.query_params["graph_uri"] raise HTTPError( 404, log_message=_( u"Class {0} doesn't exist in context {1}.").format( class_uri, graph_uri)) try: instance_data = json.loads(self.request.body) except ValueError: raise HTTPError(400, log_message=_(u"No JSON object could be decoded")) instance_data = normalize_all_uris_recursively(instance_data) try: (instance_uri, instance_id) = create_instance(self.query_params, instance_data) except InstanceError as ex: raise HTTPError(500, log_message=unicode(ex)) instance_url = self.build_resource_url(instance_id) self.set_header("location", instance_url) self.set_header("X-Brainiak-Resource-URI", instance_uri) self.query_params["instance_uri"] = instance_uri self.query_params["instance_id"] = instance_id self.query_params["expand_object_properties"] = "1" instance_data = get_instance(self.query_params) if settings.NOTIFY_BUS: self._notify_bus(action="POST", instance_data=instance_data) self.finalize(201)
def post(self, context_name, class_name): valid_params = CLASS_PARAMS with safe_params(valid_params): self.query_params = ParamDict(self, context_name=context_name, class_name=class_name, **valid_params) del context_name del class_name try: schema = schema_resource.get_cached_schema(self.query_params) except SchemaNotFound: schema = None if schema is None: class_uri = self.query_params["class_uri"] graph_uri = self.query_params["graph_uri"] raise HTTPError(404, log_message=_(u"Class {0} doesn't exist in context {1}.").format(class_uri, graph_uri)) try: instance_data = json.loads(self.request.body) except ValueError: raise HTTPError(400, log_message=_(u"No JSON object could be decoded")) instance_data = normalize_all_uris_recursively(instance_data) try: (instance_uri, instance_id) = create_instance(self.query_params, instance_data) except InstanceError as ex: raise HTTPError(500, log_message=unicode(ex)) instance_url = self.build_resource_url(instance_id) self.set_header("location", instance_url) self.set_header("X-Brainiak-Resource-URI", instance_uri) self.query_params["instance_uri"] = instance_uri self.query_params["instance_id"] = instance_id self.query_params["expand_object_properties"] = "1" instance_data = get_instance(self.query_params) if settings.NOTIFY_BUS: self._notify_bus(action="POST", instance_data=instance_data) self.finalize(201)
def test_get_instance_without_result(self, get_cached_schema, assemble_instance_json, query_all_properties_and_objects): query_params = {'request': MockRequest(instance="instance"), 'class_name': 'Country', 'class_uri': 'http://www.onto.sample/place/Country', 'context_name': 'place', 'expand_uri': '0', 'graph_uri': 'http://www.onto.sample/place', 'instance_id': 'Brazil', 'instance_uri': settings.URI_PREFIX + 'place/Country/Brazil', 'lang': 'pt'} response = get_instance.get_instance(query_params) self.assertEqual(response, None) self.assertFalse(assemble_instance_json.called) self.assertFalse(get_cached_schema.called) self.assertTrue(query_all_properties_and_objects.called)
def test_get_instance_without_result(self, get_cached_schema, assemble_instance_json, query_all_properties_and_objects): query_params = { 'request': MockRequest(instance="instance"), 'class_name': 'Country', 'class_uri': 'http://www.onto.sample/place/Country', 'context_name': 'place', 'expand_uri': '0', 'graph_uri': 'http://www.onto.sample/place', 'instance_id': 'Brazil', 'instance_uri': settings.URI_PREFIX + 'place/Country/Brazil', 'lang': 'pt' } response = get_instance.get_instance(query_params) self.assertEqual(response, None) self.assertFalse(assemble_instance_json.called) self.assertFalse(get_cached_schema.called) self.assertTrue(query_all_properties_and_objects.called)
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)
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)