Folder and query API?

18 views
Skip to first unread message

Andreas Jung

unread,
Jan 21, 2013, 5:39:59 AM1/21/13
to ko...@googlegroups.com
Are there higher-level APIs in Kotti for something like

- give me all content objects inside a folder?
- give me the path of an object with ID x (there could be multiple objects with the same ID)?
- give me all objects for type X inside the site or a subtree?

Or do I have to implement something myself on the Sqlalchemy level?

Andreas

Andreas Kaiser

unread,
Jan 21, 2013, 6:07:01 AM1/21/13
to ko...@googlegroups.com
Hi Andreas,


On 21.01.2013, at 11:39, Andreas Jung <zopyx...@gmail.com> wrote:

> Are there higher-level APIs in Kotti for something like
>
> - give me all content objects inside a folder?

folder.children
(see https://github.com/Kotti/Kotti/blob/master/kotti/resources.py#L128)

or

folder.children_with_permission(request)
(see https://github.com/Kotti/Kotti/blob/master/kotti/resources.py#L133)

> - give me the path of an object with ID x (there could be multiple objects with the same ID)?

The id attribute is guaranteed to be unique, it is the primary key for
all Node (and its subclasses) objects.

This is untested, but should be what you want:

from pyramid.traversal import resource_path
resource_path(session.query(Content).filter(content.id==id))

> - give me all objects for type X inside the site

Depending on what you want to achieve you could do either

session.query(Document).all()

or

session.query(Content).filter(Content.type == 'document')

to get all objects of type document within the site's tree.

> or a subtree?

I'm not aware of an existing API for that.

> Or do I have to implement something myself on the Sqlalchemy level?

Kotti doesn't provide a high level API for things that can be done
rather simply with SQLAlchemy. Regarding your last question: as this
is definitively not a really simple thing to do with SQLAlchemy, we
should consider to add it to Kotti's API.


HTH,

Andreas

Marco Scheidhuber

unread,
Jan 21, 2013, 6:23:36 AM1/21/13
to ko...@googlegroups.com
Hi Andreas,

On 01/21/2013 12:07 PM, Andreas Kaiser wrote:
> On 21.01.2013, at 11:39, Andreas Jung <zopyx...@gmail.com> wrote:
>> or a subtree?
actually there is some code. The function nodes_tree is used to generate
the navigation
(https://github.com/Kotti/Kotti/blob/master/kotti/views/util.py#L347),
but was extended to get all children in a subtree. See
https://github.com/Kotti/Kotti/blob/master/kotti/views/edit/actions.py#L49
with a
current usage. But this is not in the official API yet.

> Kotti doesn't provide a high level API for things that can be done
> rather simply with SQLAlchemy. Regarding your last question: as this
> is definitively not a really simple thing to do with SQLAlchemy, we
> should consider to add it to Kotti's API.
+1

Cheers,
Marco

Daniel Nouri

unread,
Jan 21, 2013, 10:51:48 AM1/21/13
to ko...@googlegroups.com, Marco Scheidhuber
+1

I needed this in a project and wrote the following code for it. I'm
not sure how well this would scale to more levels (how deep down it
goes, which is limited) and number of objects in the tree, but it
works okay for what I'm doing.

Example usage:

query_children(
context,
more_conditions={'type': 'mytype'},
).order_by(MyType.name)


Here's the code:

def children_ids(context, levels=3, more_conditions=None):
selects = []

for level in range(levels):
alias = Node.__table__
conditions = [Node.__table__.c.id == context.id]
for depth in range(level + 1):
alias, old_alias = Node.__table__.alias(), alias
conditions.append(alias.c.parent_id == old_alias.c.id)
if more_conditions:
for key, value in more_conditions.items():
conditions.append(getattr(alias.c, key) == value)
selects.append(select([alias.c.id], and_(*conditions)))

sel_union = selects.pop(0).alias().select()
for sel in selects:
sel_union = sel_union.union(sel).alias().select()

return sel_union

def query_children(context, levels=3, more_conditions=None):
return DBSession.query(Node).filter(
Node.id.in_(children_ids(context, levels, more_conditions)))



Daniel

Daniel Nouri

unread,
Jan 21, 2013, 10:56:10 AM1/21/13
to ko...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 01/21/2013 12:07 PM, Andreas Kaiser wrote:
> Hi Andreas,
>
>
> On 21.01.2013, at 11:39, Andreas Jung <zopyx...@gmail.com>
> wrote:
>
>> Are there higher-level APIs in Kotti for something like
>>
>> - give me all content objects inside a folder?
>
> folder.children (see
> https://github.com/Kotti/Kotti/blob/master/kotti/resources.py#L128)

Folders
>
are dict-like so you can also do `container.values()` to get a
list of items it contains. (Similarly `container.keys()` to get an
ordered list of names.)

There's more about the Node API here:


http://kotti.readthedocs.org/en/0.7.2/developer-manual.html#working-with-content-objects


Daniel
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)

iEYEARECAAYFAlD9ZRoACgkQi2kx5isbD8wDsgCfSlhXwNvSUNEgcg0+APl6l0U0
Y1cAoIR6BAs6S6JFAxDEgWRIbWpuQPV8
=rRoy
-----END PGP SIGNATURE-----
Reply all
Reply to author
Forward
0 new messages