Commit 9fd33fcf authored by Pascal's avatar Pascal
Browse files

ini file

parent 03434cb3
# admin-extra
Add facilities for the development server.
## Help pages
By default, a ``?`` help button is added to all served pages,
which links to a main help page.
If the lektor project contains a ``content/admin-pages`` folder,
the help button will redirect there. This page should have system
fields ``_hidden: yes`` and ``_discoverable: no``.
If ``content/admin-pages`` is not found, the default
``template/help.html`` of this plugin is served instead.
When writing ``admin-pages``, consider extending the default
``template/help.html``.
Add extra buttons and messages to lektor admin panel.
## Adding custom buttons
Besides the default ``?`` help button, the plugin makes it possible
to register other links. For example, the ``lektor-login``
plugin registers a logout link.
Use ``add_serve_button(url, tooltip, html)`` to add a button
to all served pages, and ``add_dash_button`` for dash.
Example
```
class MyPlugin(Plugin):
def on_setup_env(self, *args, **extra):
admin_extra = get_plugin('admin-extra', self.env)
admin_extra.add_button( '/secret/url', 'hidden diary', ':-/' )
```
Trick: in order to use ``url_for``, the code must run after Flask app has
started. An option is to register under ``before_app_first_request``
```
from lektor.admin.modules import serve
@serve.bp.before_app_first_request
def register_button():
admin_extra.add_button( '/secret/url', 'hidden diary', ':-/' )
```
## Adding help pages
All
# -*- coding: utf-8 -*-
from lektor.pluginsystem import Plugin
from flask import Blueprint, \
current_app, \
url_for, \
render_template
from lektor.pluginsystem import Plugin
from lektor.context import get_ctx
from lektor.admin.modules import serve, dash
utilsbp = Blueprint('utils', __name__,
......@@ -12,10 +13,6 @@ utilsbp = Blueprint('utils', __name__,
template_folder='templates'
)
@utilsbp.route('/help')
def help():
return render_template('help.html')
def add_content(contents, extra_routes=[]):
"""
insert buttons in html pages
......@@ -42,15 +39,28 @@ class AdminExtraPlugin(Plugin):
name = 'admin-extra'
description = u'Add buttons on lektor admin pages.'
right_buttons = { 'serve' : [], 'dash': [] }
help_data = {
'index': []
}
bp = utilsbp
def emit(self, event, **kwargs):
""" lektor bug, now fixed #859 """
return self.env.plugin_controller.emit(self.id + "-" + event, **kwargs)
def on_setup_env(self, *args, **extra):
config = self.get_config()
help_dir = config.get('help_pages', None)
@serve.bp.before_app_first_request
def setup_blueprint():
app = current_app
app.register_blueprint(utilsbp)
self.add_button( url_for('utils.help'), 'help', '?' )
if help_dir is not None:
self.add_button(help_dir, 'help', '?', index=0 )
else:
self.add_button( url_for('utils.help'), 'help', '?', index=0 )
@serve.bp.after_request
#pylint: disable=unused-variable
......@@ -68,15 +78,25 @@ class AdminExtraPlugin(Plugin):
response.set_data(add_content(response.get_data(),self.buttons('dash')))
return response
@utilsbp.route('/help')
def help():
pad = self.env.new_pad()
return render_template('help.html', this=self.help_data, site=pad)
#return self.env.render_template('help.html', this=self.help_data)
def buttons(self, bp, **kwargs):
return [ b for b,f in self.right_buttons[bp] if f is None or f(**kwargs) ]
def add_button(self, route, title, html_entity, bp=['serve','dash'], ignore = None):
def add_button(self, route, title, html_entity, bp=['serve','dash'], ignore = None, index=None):
print("register button %s -> %s"%(title, route))
for b in bp:
self.right_buttons[b].append( ((route, title, html_entity), ignore) )
if index is None or index > len(self.right_buttons[b]):
self.right_buttons[b].append( ((route, title, html_entity), ignore) )
else:
self.right_buttons[b].insert(index, ((route, title, html_entity), ignore) )
def add_serve_button(self, *args, **kwargs):
self.add_button(*args, bp=['serve'], **kwargs)
def add_dash_button(self, *args, **kwargs):
self.add_button(*args, bp=['dash'], **kwargs)
def add_help_page(self, url, item):
self.help_data['index'].append( (url, item) )
<div class="auth-button-div">
{% if session.get('token') %}
<a href="{{ url_for('auth.logout') }}" title="logout"><i class="fa fa-power-off"></i></a>
{#
<a href="{{ url_for('auth.logout') }}" title="logout">&#x23FB;</a>
<a href="{{ url_for('auth.logout') }}" title="logout">\uf011</a>
#}
{% endif %}
{% for (url, title, entity) in buttons %}
<a href="{{ url }}" title="{{ title }}">{{ entity }}</a>
{% endfor %}
</div>
{# and remove buttons inside preview iframe... #}
{# FIXME: try to remove buttons inside preview iframe... #}
<script>
document.querySelector("iframe").addEventListener( "load", function(e) {
return;
......
<!DOCTYPE HTML>
<html lang="fr">
<head>
<link rel="stylesheet" href="{{url_for('utils.static', filename='login.css')}}">
</head>
<body>
<div>
<h1>Aide</h1>
</div>
{% extends "layout.html" %}
{% block box %}
<h1>Aide</h1>
<div class="container">
Pour modifier ces pages, vous devez être enregistré
comme éditeur.
<h2>Rubriques</h2>
<ul>
{% for url, item in this.index %}
<li><a href="{{ url }}">{{ item }}</a></li>
{% endfor %}
</ul>
{% if site.get('/admin-pages') %}
<h2>Documentation</h2>
<ul>
{% with prefix = '/admin-pages' %}
{% for page in site.get(prefix).children.include_undiscoverable(true) %}
{{ page }}
<li><a href="{{ page.path }}">{{ page.title }}</a></li>
<li><a href="../{{ page.path[1:] }}">{{ page.title }}</a></li>
<li><a href="{{ page.path[13:] }}">{{ page.title }}</a></li>
<li><a href="./{{ page.path[13:] }}">{{ page.title }}</a></li>
<li><a href="/{{ page.path[1:] }}">{{ page.title }}</a></li>
{% with url = page.path[page.path.startswith(prefix) and prefix|length:] %}
<li><a href="{{ url }}">{{ page.title }}</a></li>
{% endwith %}
{% endfor %}
{% endwith %}
</ul>
{% endif %}
</div>
<body>
{% endblock box %}
......@@ -92,13 +92,6 @@ def server(lektorproject, port):
print("[HALT LEKTOR SERVER]")
server.kill()
#def login(name='test',**kwargs):
# session = BaseUrlSession(base_url='http://localhost:%d'%PORT)
# rv = session.post('/auth/login',
# data = dict( username=name, password=name,**kwargs))
# yield session
# session.close()
def login(client, name, **kwargs):
password = kwargs.pop('password',name)
url = kwargs.pop('url', '/admin/root/edit')
......@@ -131,7 +124,7 @@ def blog(server):
@pytest.fixture(scope='function')
def test(server):
session = BaseUrlSession(base_url='http://localhost:%d'%PORT)
session = BaseUrlSession(base_url=server.base_url)
login(session, 'test')
yield session
session.close()
......
help_pages: /admin-pages
......@@ -4,6 +4,7 @@ import os
def test_project(lektorproject):
print(lektorproject)
for (dirpath, dirnames, files) in os.walk(lektorproject):
#dirnames[:] = [ d for d in dirnames if d[0] != '.' ]
dirnames[:] = [ d for d in dirnames if d[0] != '.' ]
print(dirnames, files)
assert os.path.exists(os.path.join(lektorproject,'packages/lektor-admin-utils'))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment