Пример #1
0
    def save(self):
        """
            Creates this index in the collection if it hasn't been already created
        """

        api = Client.instance().api

        index_details = {
            'type': self.index_type_obj.type_name
        }

        extra_index_attributes = self.index_type_obj.get_extra_attributes()

        for extra_attribute_key in extra_index_attributes:
            extra_attribute_value = extra_index_attributes[extra_attribute_key]
            index_details[extra_attribute_key] = extra_attribute_value

        query_parameters = {
            'collection': self.collection.name,
        }

        result = api.index.post(data=index_details, **query_parameters)

        self.index_type_obj.is_new = result['isNewlyCreated']
        self.index_type_obj.id = result['id']
Пример #2
0
    def create(cls, url, databases):
        """
            If databases is an empty list, all databases present in the server will become accessible via the endpoint,
            with the _system database being the default database.

            If databases is non-empty, only the specified databases will become available via the endpoint.
            The first database name in the databases list will also become the default database for the endpoint.
            The default database will always be used if a request coming in on the endpoint does not specify
            the database name explicitly.

            *Note*: adding or reconfiguring endpoints is allowed in the system database only.
            Calling this action in any other database will make the server return an error.

            Adding SSL endpoints at runtime is only supported if the server was started with SSL
            properly configured (e.g. --server.keyfile must have been set).

            :param url the endpoint specification, e.g. tcp://127.0.0.1:8530
            :param databases a list of database names the endpoint is responsible for.
        """

        api = Client.instance().api

        result = api.endpoint.post(data={
            'endpoint': url,
            'databases': databases,
        })

        return result
Пример #3
0
    def _construct_query(cls, name, collection, multiple=False, result=True, **kwargs):
        """
        """

        query = {
            'collection': collection.name,
        }

        for arg_name in kwargs:
            query[arg_name] = kwargs[arg_name]

        client = Client.instance()
        client.set_database(collection.database)
        api = client.api
        result_dict = api.simple(name).put(data=query)

        if not result:
            return result_dict

        if result_dict['count'] == 0:
            return None

        if multiple is True:

            docs = []

            for result_dict_obj in result_dict['result']:
                doc = create_document_from_result_dict(result_dict_obj, api)
                docs.append(doc)

            return docs

        else:
            return create_document_from_result_dict(result_dict['result'][0], api)
Пример #4
0
    def _send_follow(cls, request_data):
        """
        """

        related_docs = []

        api = Client.instance().api
        result_dict = api.traversal.post(data=request_data)
        results = result_dict['result']['visited']

        vertices = results['vertices']
        vertices.remove(vertices[0])

        for vertice in vertices:
            collection_name = vertice['_id'].split('/')[0]

            doc = Document(
                id=vertice['_id'],
                key=vertice['_key'],
                collection=collection_name,
                api=api,
            )

            del vertice['_id']
            del vertice['_key']
            del vertice['_rev']

            doc.data = vertice

            related_docs.append(doc)

        return related_docs
Пример #5
0
    def create(cls, url, databases):
        """
            If databases is an empty list, all databases present in the server will become accessible via the endpoint,
            with the _system database being the default database.

            If databases is non-empty, only the specified databases will become available via the endpoint.
            The first database name in the databases list will also become the default database for the endpoint.
            The default database will always be used if a request coming in on the endpoint does not specify
            the database name explicitly.

            *Note*: adding or reconfiguring endpoints is allowed in the system database only.
            Calling this action in any other database will make the server return an error.

            Adding SSL endpoints at runtime is only supported if the server was started with SSL
            properly configured (e.g. --server.keyfile must have been set).

            :param url the endpoint specification, e.g. tcp://127.0.0.1:8530
            :param databases a list of database names the endpoint is responsible for.
        """

        api = Client.instance().api

        result = api.endpoint.post(data={
            'endpoint': url,
            'databases': databases,
        })

        return result
Пример #6
0
    def _send_follow(cls, request_data):
        """
        """

        related_docs = []

        api = Client.instance().api
        result_dict = api.traversal.post(data=request_data)
        results = result_dict['result']['visited']

        vertices = results['vertices']
        vertices.remove(vertices[0])

        for vertice in vertices:
            collection_name = vertice['_id'].split('/')[0]

            doc = Document(
                id=vertice['_id'],
                key=vertice['_key'],
                collection=collection_name,
                api=api,
            )

            del vertice['_id']
            del vertice['_key']
            del vertice['_rev']

            doc.data = vertice

            related_docs.append(doc)

        return related_docs
Пример #7
0
    def remove(cls, name):
        """
        """

        api = Client.instance().api

        api.user(name).delete()
Пример #8
0
    def remove(cls, id):
        """
            Deletes an index with id

            :param id string/document-handle
        """

        api = Client.instance().api
        api.index(id).delete()
Пример #9
0
    def remove(cls, id):
        """
            Deletes an index with id

            :param id string/document-handle
        """

        api = Client.instance().api
        api.index(id).delete()
Пример #10
0
    def destroy(cls, url):
        """
            This operation deletes an existing endpoint from the list of all endpoints,
            and makes the server stop listening on the endpoint.

            *Note*: deleting and disconnecting an endpoint is allowed in the system database only.
            Calling this action in any other database will make the server return an error.

            Futhermore, the last remaining endpoint cannot be deleted as this would make the server kaput.

            :param url The endpoint to delete, e.g. tcp://127.0.0.1:8529.
        """

        api = Client.instance().api

        api.endpoint(url).delete()
Пример #11
0
    def get(cls, name):
        """
        """

        api = Client.instance().api

        user = api.user(name).get()

        user_name = user['user']
        change_password = user['changePassword']
        active = user['active']
        extra = user['extra']

        user_obj = cls(name=user_name, change_password=change_password, active=active, extra=extra, api=api)

        return user_obj
Пример #12
0
    def destroy(cls, url):
        """
            This operation deletes an existing endpoint from the list of all endpoints,
            and makes the server stop listening on the endpoint.

            *Note*: deleting and disconnecting an endpoint is allowed in the system database only.
            Calling this action in any other database will make the server return an error.

            Futhermore, the last remaining endpoint cannot be deleted as this would make the server kaput.

            :param url The endpoint to delete, e.g. tcp://127.0.0.1:8529.
        """

        api = Client.instance().api

        api.endpoint(url).delete()
Пример #13
0
    def create(cls, name, password='', active=True, extra=None, change_password=False):
        """
        """

        api = Client.instance().api

        api.user.post({
            'user': name,
            'passwd': password,
            'active': active,
            'exta': extra,
            'changePassword': change_password,
        })

        user_obj = cls(name=name, change_password=change_password, active=active, extra=extra, api=api)

        return user_obj
Пример #14
0
    def execute_raw(cls, query_string):
        """
        """

        logger.debug(query_string)

        post_data = {
            'query': query_string
        }

        api = Client.instance().api

        result = []

        try:
            start_time = time()
            post_result = api.cursor.post(data=post_data)
            end_time = time()

            calculated_time = (end_time - start_time) * 1000
            time_result = '%s ms' % calculated_time
            logger_output = 'Query took %s' % time_result
            logger.debug(logger_output)

            result_dict_list = post_result['result']

            # Create documents
            for result_list in result_dict_list:

                # Look if it is a list which needs to be iterated
                if isinstance(result_list, list):
                    for result_dict in result_list:
                        doc = create_document_from_result_dict(result_dict, api)
                        result.append(doc)
                # Otherwise just create a result document
                else:
                    result_dict = result_list
                    doc = create_document_from_result_dict(result_dict, api)
                    result.append(doc)

        except Exception as err:
            raise err

        return result
Пример #15
0
    def execute_raw(cls, query_string):
        """
        """

        logger.debug(query_string)

        post_data = {'query': query_string}

        api = Client.instance().api

        result = []

        try:
            start_time = time()
            post_result = api.cursor.post(data=post_data)
            end_time = time()

            calculated_time = (end_time - start_time) * 1000
            time_result = '%s ms' % calculated_time
            logger_output = 'Query took %s' % time_result
            logger.debug(logger_output)

            result_dict_list = post_result['result']

            # Create documents
            for result_list in result_dict_list:

                # Look if it is a list which needs to be iterated
                if isinstance(result_list, list):
                    for result_dict in result_list:
                        doc = create_document_from_result_dict(
                            result_dict, api)
                        result.append(doc)
                # Otherwise just create a result document
                else:
                    result_dict = result_list
                    doc = create_document_from_result_dict(result_dict, api)
                    result.append(doc)

        except Exception as err:
            raise err

        return result
Пример #16
0
    def start(self, transaction):
        """
        """

        statements = transaction.compile()
        client = Client.instance()
        api = client.api

        query = {
            'collections': transaction.collections,
            'action': statements,
        }

        logger.debug(query)

        try:
            val = api.transaction.post(data=query)
            return val
        except HttpClientError as err:
            raise TransactionController.InvalidTransactionException(err.content)
Пример #17
0
    def start(self, transaction):
        """
        """

        statements = transaction.compile()
        client = Client.instance()
        api = client.api

        query = {
            'collections': transaction.collections,
            'action': statements,
        }

        logger.debug(query)

        try:
            val = api.transaction.post(data=query)
            return val
        except HttpClientError as err:
            raise TransactionController.InvalidTransactionException(
                err.content)
Пример #18
0
    def _construct_query(cls,
                         name,
                         collection,
                         multiple=False,
                         result=True,
                         **kwargs):
        """
        """

        query = {
            'collection': collection.name,
        }

        for arg_name in kwargs:
            query[arg_name] = kwargs[arg_name]

        client = Client.instance()
        client.set_database(collection.database)
        api = client.api
        result_dict = api.simple(name).put(data=query)

        if not result:
            return result_dict

        if result_dict['count'] == 0:
            return None

        if multiple is True:

            docs = []

            for result_dict_obj in result_dict['result']:
                doc = create_document_from_result_dict(result_dict_obj, api)
                docs.append(doc)

            return docs

        else:
            return create_document_from_result_dict(result_dict['result'][0],
                                                    api)
Пример #19
0
    def follow(cls, start_vertex, edge_collection, direction):
        """
        """

        related_docs = []

        request_data = {
            'startVertex': start_vertex,
            'edgeCollection': edge_collection,
            'direction': direction,
        }

        api = Client.instance().api
        result_dict = api.traversal.post(data=request_data)
        results = result_dict['result']['visited']

        vertices = results['vertices']
        vertices.remove(vertices[0])

        for vertice in vertices:
            collection_name = vertice['_id'].split('/')[0]

            doc = Document(
                id=vertice['_id'],
                key=vertice['_key'],
                collection=collection_name,
                api=api,
            )

            del vertice['_id']
            del vertice['_key']
            del vertice['_rev']

            doc.data = vertice

            related_docs.append(doc)

        return related_docs
Пример #20
0
    def all(cls):
        """
            Returns a list of all configured endpoints the server is listening on. For each endpoint,
            the list of allowed databases is returned too if set.

            The result is a JSON hash which has the endpoints as keys, and the list of
            mapped database names as values for each endpoint.

            If a list of mapped databases is empty, it means that all databases can be accessed via the endpoint.
            If a list of mapped databases contains more than one database name, this means that any of the
            databases might be accessed via the endpoint, and the first database in the list will be treated
            as the default database for the endpoint. The default database will be used when an incoming request
            does not specify a database name in the request explicitly.

            *Note*: retrieving the list of all endpoints is allowed in the system database only.
            Calling this action in any other database will make the server return an error.
        """

        api = Client.instance().api

        endpoint_list = api.endpoint.get()

        return endpoint_list
Пример #21
0
    def save(self):
        """
            Creates this index in the collection if it hasn't been already created
        """

        api = Client.instance().api

        index_details = {'type': self.index_type_obj.type_name}

        extra_index_attributes = self.index_type_obj.get_extra_attributes()

        for extra_attribute_key in extra_index_attributes:
            extra_attribute_value = extra_index_attributes[extra_attribute_key]
            index_details[extra_attribute_key] = extra_attribute_value

        query_parameters = {
            'collection': self.collection.name,
        }

        result = api.index.post(data=index_details, **query_parameters)

        self.index_type_obj.is_new = result['isNewlyCreated']
        self.index_type_obj.id = result['id']
Пример #22
0
    def all(cls):
        """
            Returns a list of all configured endpoints the server is listening on. For each endpoint,
            the list of allowed databases is returned too if set.

            The result is a JSON hash which has the endpoints as keys, and the list of
            mapped database names as values for each endpoint.

            If a list of mapped databases is empty, it means that all databases can be accessed via the endpoint.
            If a list of mapped databases contains more than one database name, this means that any of the
            databases might be accessed via the endpoint, and the first database in the list will be treated
            as the default database for the endpoint. The default database will be used when an incoming request
            does not specify a database name in the request explicitly.

            *Note*: retrieving the list of all endpoints is allowed in the system database only.
            Calling this action in any other database will make the server return an error.
        """

        api = Client.instance().api

        endpoint_list = api.endpoint.get()

        return endpoint_list
Пример #23
0
    def on_init(self, model_class):
        """
        """

        if not self.related_name is None:
            relation_name = self._get_relation_collection_name(model_class)

            try:
                self.relation_collection = Collection.create(name=relation_name, database=Client.instance().database, type=3)
            except:
                self.relation_collection = Collection.get_loaded_collection(name=relation_name)

            fields = self.relation_class._model_meta_data._fields
            otherside_field = ManyToManyField(to=model_class, related_name=None)
            fields[self.related_name] = otherside_field

            # Configure other side field
            otherside_field.related_queryset = self.relation_class.objects.all()
            otherside_field.relation_collection = self.relation_collection

            self.related_queryset = self.relation_class.objects.all()
Пример #24
0
    def remove(cls, id):
        """
        """

        api = Client.instance().api
        api.index(id).delete()
Пример #25
0
    def on_init(self, model_class, attribute_name):
        """
        """

        super(ManyToManyField, self).on_init(model_class=model_class, attribute_name=attribute_name)

        if not self.related_name is None:
            relation_name = self._get_relation_collection_name(model_class)

            try:
                self.relation_collection = Collection.create(name=relation_name, database=Client.instance().database, type=3)
            except:
                self.relation_collection = Collection.get_loaded_collection(name=relation_name)

            fields = self.relation_class._model_meta_data._fields
            otherside_field = ManyToManyField(to=model_class, related_name=None)
            fields[self.related_name] = otherside_field

            # Configure other side field
            otherside_field.related_queryset = self.relation_class.objects.all()
            otherside_field.relation_collection = self.relation_collection

            self.related_queryset = self.relation_class.objects.all()
Пример #26
0
    def on_init(self, model_class):
        """
        """

        if not self.related_name is None:
            relation_name = self._get_relation_collection_name(model_class)

            try:
                self.relation_collection = Collection.create(name=relation_name, database=Client.instance().database, type=3)
            except:
                self.relation_collection = Collection.get_loaded_collection(name=relation_name)

            fields = self.relation_class._model_meta_data._fields
            fields[self.related_name] = ManyToManyField(to=model_class, related_name=None)