def test_build_key_for_instance(self):
     url_params = dict(graph_uri="graph", class_uri="Class", instance_uri="instance")
     handler = MockHandler(**url_params)
     params = ParamDict(handler, **url_params)
     computed = build_instance_key(params)
     expected = "_@@_@@instance@@expand_uri=1&instance_uri=instance&lang=pt##instance"
     self.assertEqual(computed, expected)
Example #2
0
    def get(self, context_name, class_name, instance_id):
        optional_params = INSTANCE_PARAMS
        with safe_params(optional_params):
            self.query_params = ParamDict(self,
                                          context_name=context_name,
                                          class_name=class_name,
                                          instance_id=instance_id,
                                          **optional_params)

        response = memoize(self.query_params,
                           get_instance,
                           key=build_instance_key(self.query_params),
                           function_arguments=self.query_params)

        if response is None:
            error_message = u"Instance ({0}) of class ({1}) in graph ({2}) was not found.".format(
                self.query_params['instance_uri'],
                self.query_params['class_uri'],
                self.query_params['graph_uri'])
            raise HTTPError(404, log_message=error_message)

        response_meta = response['meta']
        response = response['body']

        if self.query_params["expand_uri"] == "0":
            response = normalize_all_uris_recursively(response, mode=SHORTEN)

        self.add_cache_headers(response_meta)
        self.finalize(response)
 def test_build_key_for_instance(self):
     url_params = dict(graph_uri="graph", class_uri="Class", instance_uri="instance")
     handler = MockHandler(**url_params)
     params = ParamDict(handler, **url_params)
     computed = build_instance_key(params)
     expected = "_@@_@@instance@@expand_uri=0&instance_uri=instance&lang=pt##instance"
     self.assertEqual(computed, expected)
Example #4
0
    def get(self, context_name, class_name, instance_id):
        optional_params = INSTANCE_PARAMS
        with safe_params(optional_params):
            self.query_params = ParamDict(self,
                                          context_name=context_name,
                                          class_name=class_name,
                                          instance_id=instance_id,
                                          **optional_params)

        response = memoize(self.query_params,
                           get_instance,
                           key=build_instance_key(self.query_params),
                           function_arguments=self.query_params)

        if response is None:
            error_message = u"Instance ({0}) of class ({1}) in graph ({2}) was not found.".format(
                self.query_params['instance_uri'],
                self.query_params['class_uri'],
                self.query_params['graph_uri'])
            raise HTTPError(404, log_message=error_message)

        response_meta = response['meta']
        response = response['body']

        if self.query_params["expand_uri"] == "0":
            response = normalize_all_uris_recursively(response, mode=SHORTEN)

        self.add_cache_headers(response_meta)
        self.finalize(response)
Example #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)
Example #6
0
 def purge(self, context_name, class_name, instance_id):
     if settings.ENABLE_CACHE:
         optional_params = INSTANCE_PARAMS
         with safe_params(optional_params):
             self.query_params = ParamDict(self,
                                           context_name=context_name,
                                           class_name=class_name,
                                           instance_id=instance_id,
                                           **optional_params)
         path = build_instance_key(self.query_params)
         cache.purge_by_path(path, False)
     else:
         raise HTTPError(405, log_message=_("Cache is disabled (Brainaik's settings.ENABLE_CACHE is set to False)"))
Example #7
0
 def purge(self, context_name, class_name, instance_id):
     if settings.ENABLE_CACHE:
         optional_params = INSTANCE_PARAMS
         with safe_params(optional_params):
             self.query_params = ParamDict(self,
                                           context_name=context_name,
                                           class_name=class_name,
                                           instance_id=instance_id,
                                           **optional_params)
         path = build_instance_key(self.query_params)
         cache.purge_by_path(path, False)
     else:
         raise HTTPError(405, log_message=_("Cache is disabled (Brainaik's settings.ENABLE_CACHE is set to False)"))
Example #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)
Example #9
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)