Esempio n. 1
0
    def test_limit_key_parsing(self):
        from sqlalchemy_traversal import parse_key

        keys = ["/messages.limit(0,20)", "/messages.order_by.limit(2,20)"]

        for key in keys:
            result = parse_key(key)
            import pdb
            pdb.set_trace()
Esempio n. 2
0
    def __getitem__(self, key):
        """
        This is used in traversal to get the correct item. If the path ends
        with a tablename such as "user" then we will return a list of all the
        rows in table wrapped in a ModelCollection.

        If the request is a PUT or POST we will assume that you want to create
        or update the object and will just return an instance of the model.

        If we are in a GET request and don't end with a tablename then we will
        root a SQLAlchemyFactory that will keep track of which node we are 
        currently at.
        """
        filters = parse_key(key)

        cls = self.tables[filters['table']]

        to_return = None

        # Do we have the table registered as a traversal object?
        if cls == None:
            raise KeyError

        # This is used to shortcircuit the traversal, if we are ending
        # on a model, for instance /api/user then we should either be creating
        # a new instance or querying the table
        path = urllib.parse.unquote(self.request.path)

        if path.endswith(key):
            if self.request.method == 'GET':
                #query = filter_query_by_qs(self.session, cls,
                #        self.request.GET
                #)
                query = self.session.query(cls)
                query = filter_query(filters, query, cls)

                try:
                    to_return = ModelCollection(
                        [x for x in query.all()]
                        , request=self.request
                    )
                except ProgrammingError:
                    raise KeyError

            elif self.request.method == 'POST' or self.request.method == 'PUT':
                to_return = cls()

        # If we haven't found something to return in the traversal tree yet,
        # it means we want to continue traversing the SQLAlchemy objects,
        # so lets return an SQLAlchemyRoot
        if not to_return:
            to_return = SQLAlchemyRoot(self.request, cls)

        to_return.__parent__ = self

        return to_return
    def test_limit_key_parsing(self):
        from sqlalchemy_traversal import parse_key

        keys = [
            "/messages.limit(0,20)"
            , "/messages.order_by.limit(2,20)"
        ]

        for key in keys:
            result = parse_key(key)
            import pdb; pdb.set_trace()
Esempio n. 4
0
    def __getitem__(self, key):
        """
        This is used in traversal to get the correct item. If the path ends
        with a tablename such as "user" then we will return a list of all the
        rows in table wrapped in a ModelCollection.

        If the request is a PUT or POST we will assume that you want to create
        or update the object and will just return an instance of the model.

        If we are in a GET request and don't end with a tablename then we will
        root a SQLAlchemyFactory that will keep track of which node we are 
        currently at.
        """
        filters = parse_key(key)

        cls = self.tables[filters['table']]

        to_return = None

        # Do we have the table registered as a traversal object?
        if cls == None:
            raise KeyError

        # This is used to shortcircuit the traversal, if we are ending
        # on a model, for instance /api/user then we should either be creating
        # a new instance or querying the table
        path = urllib.parse.unquote(self.request.path)

        if path.endswith(key):
            if self.request.method == 'GET':
                #query = filter_query_by_qs(self.session, cls,
                #        self.request.GET
                #)
                query = self.session.query(cls)
                query = filter_query(filters, query, cls)

                try:
                    to_return = ModelCollection([x for x in query.all()],
                                                request=self.request)
                except ProgrammingError:
                    raise KeyError

            elif self.request.method == 'POST' or self.request.method == 'PUT':
                to_return = cls()

        # If we haven't found something to return in the traversal tree yet,
        # it means we want to continue traversing the SQLAlchemy objects,
        # so lets return an SQLAlchemyRoot
        if not to_return:
            to_return = SQLAlchemyRoot(self.request, cls)

        to_return.__parent__ = self

        return to_return