app

Overview

This app provides app-related features, such as base model and view, a registry and a base class for kii apps, autoregistration of kii apps urls, and menu management.

Core

This module provides two core components for kii:

  • apps, an instance of AppManager, used especialy for

    automatic URL inclusion of kii apps.

  • App, a class that extends django regular django.apps.AppConfig and you should use for building your own kii apps.

class kii.app.core.App(app_name, app_module)[source]

A base class for all kii apps

api_urls = None

See urls for more details: a string containing the path to an URLconf that contains API urls (if any). This URLconf will be included under the kii:api namespace.

get_url_prefix()[source]

Return a prefix for used for URLconf inclusion, such as r'^myapp/'. It must be a raw string, starting with ^ and ending with a trailing slash.

By default, uses the label as the prefix.

index[source]
Returns:A URL pointing to the app index
installed[source]
Returns:a boolean that indicates if the app is marked as installed by django
kii_app = True

wether the app should be considered as a kii app or not.

menu = None

If you attach a MenuNode instance here, the corresponding menu will be automatically built and included in templates.

App.ready() is a good place for registering your menu.

public_models()[source]

return a list of models from this app that can be created by any user TODO: is this used ?

urlconf(target)[source]
Parameters:target (str) – The targeted path
Returns:The full path to the targeted URLconf, for further inclusion in a URL pattern
urls = None

A string containing the path to the app URLconf (if any). This URLconf will then be automatically registered under the kii namespace with AppManager.get_apps_urls().

You can use an absolute path, such as your_app.subpackage.urls or a relative path, like .urls. In the last case, a full path will be built using name.

class kii.app.core.AppManager[source]

Provide a cleaner API to django.apps.apps

all()[source]
Returns:An iterable containing all the registered app configs
filter(**kwargs)[source]
Returns:an iterable of installed apps that match given filters
get(app_label)[source]
Parameters:app_label (str) –
Returns:A django.apps.AppConfig instance corresponding to the given app_abel.
get_apps_urls(urlconf='urls')[source]

Gather all URLs for kii apps so they can easily be included in a URLconf.

Returns:A list of django url patterns
kii_apps()[source]
Returns:An iterable containing all registered django apps marked that are also kii apps

Menu

class kii.app.menu.MenuNode(**kwargs)[source]

Describe a menu element, and may be attached to an App instance (via the menu attribute) for automatic inclusion in templates.

__init__(**kwargs)[source]
Parameters:
  • route (str) – Either a relative URL, absolute URL or a django URL name, such as kii:myapp:index. Defaults to #.
  • reverse (bool) – Wether the given route should be reversed using django’s reverse() or returned ‘as is’. Defaults to True.
  • parent – TODO, seems useless
  • weight (int) – Indicate the importance of the node. Higher is more important, default to 0.
  • require_authentication (bool) – Used to determine if the node should be shown to unauthenticated users. Defaults to True.
  • reverse_kwargs (list) – A list of strings that the route will accept when reversing. Defaults to []
  • children (list) – A list of children MenuNode instances that will be considered as submenus of this instance. Defaults to [].
  • icon – TODO, seems useless.
__weakref__

list of weak references to the object (if defined)

add(item)[source]

Add a new node to the instance children and sort them by weight.

Parameters:item – A menu node instance
url(**kwargs)[source]
Parameters:kwargs – a dictionary of values that will be used for reversing, if the corresponding key is present in self.reverse_kwargs
Returns:The target URL of the node, after reversing (if needed)

Models

class kii.app.models.AppModel(*args, **kwargs)[source]

A base class for all apps related models. It implements URL reversing for model instances, so one can do:

instance = MyModel.objects.get(pk=42)
assert instance.reverse_update() == "/kii/myapp/mymodel/42/update"
classmethod class_reverse(suffix)[source]

Same as reverse but callable from class instead of instances. :return: a reversed URL for the model

get_absolute_url()[source]
Returns:The absolute URL of the instance, which is equal to self.reverse_detail() by default
public_model = False

: If True, any authenticated user will be able to create isntances of this model TODO : is it useful ?

reverse(suffix, **kwargs)[source]

Get a model-instance relative URL, such as a detail, delete or update URL. You can override per-suffix URLs by defining reverse_<suffix> methods on the model class.

Parameters:
  • suffix (str) – a string that will be used to find the corresponding reverse method on the model class (if any)
  • kwargs (dict) – optional URL kwargs that will be passed to the reverse function
Returns:

a reversed URL

reverse_delete(**kwargs)[source]
Returns:The delete URL of the instance
reverse_detail(**kwargs)[source]
Returns:The detail URL of the instance
reverse_update(**kwargs)[source]
Returns:The update URL of the instance
url_namespace(**kwargs)[source]
Parameters:user_area (bool) – whether the URL namespace should include the username part
Returns:a string representing the URL namespace of the model, such as kii:myapp:mymodel:

Template tags

kii.app.templatetags.app_tags.model_url(model, suffix, **kwargs)[source]

Return a model-related URL. Usage:

{% load app_tags %}

{% model_url my_object "delete" as url %}
{% if url %}
    {{ url }}
{% endif %}

Will ouptut something like /myapp/mymodel/12/delete if the URL exists.

Model can be an instance or a Model subclass.

This tag will fail silently if the URL is not foun and return an empty string.

kii.app.templatetags.app_tags.node_url(node, **kwargs)[source]

Typical usage (look at kii/glue/templates/default/glue/menu_node.html for a real example):

{% load app_tags %}

{% with node=app.menu %}

    {% node_url node as url %}
    <a href="{{ url }}">My app root menu node</a>

    {% for child_node in node.children %}

        {% node_url child_node as child_url %}
        <a href="{{ child_url }}">A child node</a>

    {% endfor %}
{% endwith %}
Parameters:
  • node – a kii.app.menu.MenuNode instance
  • kwargs – kwargs that will be passed to the reverse() function
Returns:

the target URL of the menu node

Views

class kii.app.views.AppMixin[source]

Add extra method and context to all apps views

dispatch(request, *args, **kwargs)[source]

Set up some hooks before calling the actual dispatch method

get_breadcrumbs()[source]
Returns:a list of breadcrumb elements , such as (('Delete' '/delete'), ('My model', None), ('My app', None)) for use in templates. The first item of each tuple is the title of the element, the second is the URL. URL can be None.
get_context_data(**kwargs)[source]

Add the current app, the page title, the breadcrumbs and the root URL of kii to context

get_page_title()[source]

Override this method if you want to return a custom title for the page. :return: A page title, as a string

page_title = ''

a page title that will be display in templates and <title> tags

pre_dispatch(request, *args, **kwargs)[source]

Called just after setup(). If you return anything but None, the view will stop and return the returned value.

This method is convenient for checking a permission, for example.

Returns:None
setup(request, *args, **kwargs)[source]

Called at the beginning of dispatch(), you can extend this method if you need to set some variables for later use