DBAdmin can add alarms of kind 'single' to db
The entrypoint infrastructure is ready.
This commit is contained in:
parent
30eda39a7c
commit
9e13105bba
9 changed files with 102 additions and 11 deletions
|
@ -16,6 +16,7 @@ def get_conf(prefix='LARIGIRA_'):
|
|||
conf['CACHING_TIME'] = 10
|
||||
conf['DB_URI'] = 'larigira.db'
|
||||
conf['BOOTSTRAP_SERVE_LOCAL'] = True
|
||||
conf['SECRET_KEY'] = 'Please replace me!'
|
||||
conf.update(from_envvars(prefix=prefix))
|
||||
return conf
|
||||
|
||||
|
|
|
@ -5,14 +5,42 @@ Templates are self-contained in this directory
|
|||
'''
|
||||
from __future__ import print_function
|
||||
|
||||
from flask import current_app, Blueprint, render_template
|
||||
from flask import current_app, Blueprint, render_template, jsonify, abort
|
||||
|
||||
from larigira.entrypoints_utils import get_avail_entrypoints
|
||||
from larigira import forms
|
||||
db = Blueprint('db', __name__, url_prefix='/db', template_folder='templates')
|
||||
|
||||
|
||||
@db.route('/list')
|
||||
def db_list():
|
||||
def list():
|
||||
model = current_app.larigira.monitor.source.model
|
||||
alarms = tuple(model.get_all_alarms())
|
||||
events = [(alarm, model.get_actions_by_alarm(alarm))
|
||||
for alarm in alarms]
|
||||
return render_template('list.html', events=events)
|
||||
|
||||
|
||||
@db.route('/add/time')
|
||||
def addtime():
|
||||
kinds = get_avail_entrypoints('larigira.timeform_create')
|
||||
return render_template('add_time.html', kinds=kinds)
|
||||
|
||||
|
||||
@db.route('/add/time/<kind>')
|
||||
def addtime_kind(kind):
|
||||
Form = next(forms.get_timeform(kind))
|
||||
return render_template('add_time_kind.html', form=Form(), kind=kind)
|
||||
|
||||
|
||||
@db.route('/add/time/<kind>', methods=['POST'])
|
||||
def addtime_kind_post(kind):
|
||||
Form, receiver = tuple(forms.get_timeform(kind))
|
||||
form = Form()
|
||||
del Form
|
||||
if not form.validate_on_submit():
|
||||
abort(400)
|
||||
data = receiver(form)
|
||||
model = current_app.larigira.monitor.source.model
|
||||
eid = model.add_event(data, [])
|
||||
return jsonify(dict(inserted=eid, data=data))
|
||||
|
|
15
larigira/dbadmin/templates/add_time.html
Normal file
15
larigira/dbadmin/templates/add_time.html
Normal file
|
@ -0,0 +1,15 @@
|
|||
{% extends "dbadmin_base.html" %}
|
||||
{% block title %}Larigira - DB add time {%endblock%}
|
||||
|
||||
{% block content %}
|
||||
<div class="container-fluid">
|
||||
<h2>Add time specification</h2>
|
||||
Available kinds:
|
||||
<ul>
|
||||
{% for kind in kinds %}
|
||||
<li><a href="{{url_for('db.addtime_kind', kind=kind)}}">{{kind}}
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</div>
|
||||
{% endblock content %}
|
||||
{# vim: set ts=2 sw=2 noet: #}
|
11
larigira/dbadmin/templates/add_time_kind.html
Normal file
11
larigira/dbadmin/templates/add_time_kind.html
Normal file
|
@ -0,0 +1,11 @@
|
|||
{% extends "dbadmin_base.html" %}
|
||||
{% import "bootstrap/wtf.html" as wtf %}
|
||||
|
||||
{% block title %}Larigira - DB add time "{{kind}}" {%endblock%}
|
||||
|
||||
{% block content %}
|
||||
<div class="container-fluid">
|
||||
{{wtf.quick_form(form)}}
|
||||
</div>
|
||||
{% endblock content %}
|
||||
{# vim: set ts=2 sw=2 noet: #}
|
|
@ -13,18 +13,13 @@
|
|||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="{{url_for('db.db_list')}}">Larigira</a>
|
||||
<a class="navbar-brand" href="{{url_for('db.list')}}">Larigira</a>
|
||||
</div>{# navbar-header #}
|
||||
|
||||
<div class="collapse navbar-collapse" id="bs-navbar-collapse-1">
|
||||
<ul class="nav navbar-nav">
|
||||
<li><a href="{{url_for('db.db_list')}}">List</a></li>
|
||||
<li><a href="{{url_for('db.db_list')}}">List</a></li>
|
||||
<li><a href="{{url_for('db.db_list')}}">List</a></li>
|
||||
<li><a href="{{url_for('db.db_list')}}">List</a></li>
|
||||
<li><a href="{{url_for('db.db_list')}}">List</a></li>
|
||||
<li><a href="{{url_for('db.db_list')}}">List</a></li>
|
||||
<li><a href="{{url_for('db.db_list')}}">List</a></li>
|
||||
<li><a href="{{url_for('db.list')}}">List</a></li>
|
||||
<li><a href="{{url_for('db.addtime')}}">Add time</a></li>
|
||||
</ul>
|
||||
</div>{# collapse #}
|
||||
|
||||
|
|
|
@ -11,3 +11,7 @@ def get_one_entrypoint(group, kind):
|
|||
if len(points) > 1:
|
||||
log.warning("Found more than one timeform for %s:%s" % (group, kind))
|
||||
return points[0].load()
|
||||
|
||||
|
||||
def get_avail_entrypoints(group):
|
||||
return [e.name for e in iter_entry_points(group=group)]
|
||||
|
|
9
larigira/forms.py
Normal file
9
larigira/forms.py
Normal file
|
@ -0,0 +1,9 @@
|
|||
from logging import getLogger
|
||||
log = getLogger('timeform')
|
||||
from entrypoints_utils import get_one_entrypoint
|
||||
|
||||
|
||||
def get_timeform(kind):
|
||||
'''Messes with entrypoints to return a TimeForm'''
|
||||
for group in ('larigira.timeform_create', 'larigira.timeform_receive'):
|
||||
yield get_one_entrypoint(group, kind)
|
20
larigira/timeform_single.py
Normal file
20
larigira/timeform_single.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
from __future__ import print_function
|
||||
|
||||
from flask_wtf import Form
|
||||
from wtforms import StringField, DateTimeField, validators, SubmitField
|
||||
|
||||
|
||||
class SingleAlarmForm(Form):
|
||||
nick = StringField(u'Alarm nick', validators=[validators.required()],
|
||||
description='A simple name to recognize this alarm')
|
||||
dt = DateTimeField(u'Date and time', validators=[validators.required()],
|
||||
description='Date to ring on, expressed as '
|
||||
'YYYY-MM-DD HH:MM:SS')
|
||||
submit = SubmitField(u'Submit')
|
||||
|
||||
|
||||
def singlealarm_receive(form):
|
||||
return {
|
||||
'nick': form.nick.data,
|
||||
'timestamp': form.dt.data.strftime('%s')
|
||||
}
|
10
setup.py
10
setup.py
|
@ -41,6 +41,8 @@ setup(name='larigira',
|
|||
'flask',
|
||||
'flask-bootstrap',
|
||||
'python-mpd2',
|
||||
'wtforms',
|
||||
'Flask-WTF',
|
||||
'tinydb'
|
||||
],
|
||||
tests_require=['pytest', 'pytest-timeout'],
|
||||
|
@ -59,6 +61,12 @@ setup(name='larigira',
|
|||
'larigira.timegenerators': [
|
||||
'frequency = larigira.timegen_every:FrequencyAlarm',
|
||||
'single = larigira.timegen_every:SingleAlarm',
|
||||
]
|
||||
],
|
||||
'larigira.timeform_create': [
|
||||
'single = larigira.timeform_single:SingleAlarmForm',
|
||||
],
|
||||
'larigira.timeform_receive': [
|
||||
'single = larigira.timeform_single:singlealarm_receive',
|
||||
],
|
||||
}
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue