Dedicated administration pages

Purpose

The dedicated administration pages are available in Domogik administration on a client page in the Advanced menu.

How it works

On startup, Domogik administration will look for each installed client if there is an admin/ folder. If so, the content of this folder will be handled by Flask to be available from the administration.

So, you just need to fill an admin/ folder in the root of your package repository.

Create a simple page (diskfree example)

First, create the files :

mkdir admin/
mkdir admin/templates/
touch admin/__init__.py
touch admin/templates/plugin_diskfree.html

Of course, change plugin_diskfree with the appropriate package id.

admin/__init__.py file

The __init__.py file will NOT be empty. It will contain the Flask related python code needed by Domogik administration to use your dedicated pages.

Example code (plugin diskfree) :

# -*- coding: utf-8 -*-

### common imports
from flask import Blueprint, abort
from domogik.common.utils import get_packages_directory
from domogik.admin.application import render_template
from domogik.admin.views.clients import get_client_detail
from jinja2 import TemplateNotFound

### package specific imports
import subprocess



### package specific functions
def get_df():
    df = subprocess.Popen(["df", "-h"], stdout=subprocess.PIPE)
    output = df.communicate()[0]
    #device, size, used, available, percent, mountpoint = output.split("\n")[1].split()
    if isinstance(output, str):
        output = unicode(output, 'utf-8')
    return output

### common tasks
package = "plugin_diskfree"
template_dir = "{0}/{1}/admin/templates".format(get_packages_directory(), package)
static_dir = "{0}/{1}/admin/static".format(get_packages_directory(), package)

plugin_diskfree_adm = Blueprint(package, __name__,
                        template_folder = template_dir,
                        static_folder = static_dir)

@plugin_diskfree_adm.route('/<client_id>')
def index(client_id):
    detail = get_client_detail(client_id)
    try:
        return render_template('plugin_diskfree.html',
            clientid = client_id,
            client_detail = detail,
            mactive="clients",
            active = 'advanced',
            df = get_df())

    except TemplateNotFound:
        abort(404)

First, there are all the imports. You should customize only the package specific imports part. Then, the function get_df() is specific to this plugin, you can, like here define as many functions as needed.

The ### common tasks part is important : you must customize the package variable. Then, you have to create a Blueprint object named like this : <package type>_<package name>_adm, in our example : plugin_diskfree_adm.

After, you will have to create one function for each administration page you need. Each function will at least contains the same actions as the example.

admin/templates/*.html

You must at least have one template file. This mandatory file must be named <package type>_<package name>.html, for example: plugin_diskfree.html. Each file you will have to create must start by the prefix <package type>_<package name>. This allow avoiding collisions between the packages dedicated pages.

Here is an example for the plugin diskfree :

{% extends theme("base/base.html") %}
{% import theme("base/wtf.html") as wtf %}

{% block content %}
{% include theme("client_menu.html") %}
<div class="container">
<h2>{% trans %}Result of the 'df -h' command{% endtrans %}</H2>
<p>{% trans %}You only need to create devices for the mounted pathes. The <em>df -h</em> result will show you all the mounted pathes that may be monitored.{% endtrans %}</p>
<p>{% trans %}Please notice that the plugin may not give exactly the same values! This is related to the way the size are calculated. {% endtrans %}</p>
<pre>{{ df }}</pre>
</div>
{% endblock %}

You are free to put anything you want/need in the container div. But you need to keep all the others lines as given.

Use Domogik Message Queue (MQ)

If you need to use the MQ from your admin page, you can by using WebSockets in Javascript : a gateway is available in the admin.

Todo

Examples