Wiki

Clone wiki

gnocchi-tools / Home

Gnocchi Tool

In the development of my Gnocchi CMS, many tools emerged as common between the various modules.

As I break Gnocchi into its component apps, I'm moving their common requirements into this app.

Attributes

Firstly, there's the common "attribute" model. This provides a typeless "attribute" scheme using generic relations.

To make accessing them simple, the attr.AttrHelper mixin makes them accessible through dict style access on the model. The mixin uses a _load_cache(self, force=False) hook to load all attribute on the first access, for efficiency.

Also included is admin.InlineAttributeAdmin to make editing attributes on models easier.

Templates

In gnocchi_tools.template is the following function:

def parse_args(parser, token, as_name=False):

It returns args, kwargs, and as_name.

It helps with making non-trivial template tags. It will check each arg to the tag to see if it's a kwarg (has an '=') or arg, and wrap them in a parser filter for resolving on tag render.

If as_name is passed, it will check if the second last arg is 'as', in which case it will take the last arg as the name to be used as a target for this tag.

It use would be along the lines of the following code, from gnocchi-blog:

@register.tag
def get_recent_posts(parser, token):
    args, kwargs, as_name = tools.template.parse_args(parser, token, 'posts')
    count = int(args.pop(0))
    return RecentPostsNode(count, as_name, *args, **kwargs)

This allows you to have a tag like:

{% get_recent_posts username=user.username as posts %}

Updated