DBAdmin can add alarms of kind 'single' to db

The entrypoint infrastructure is ready.
This commit is contained in:
boyska 2015-02-01 21:47:12 +01:00
parent 30eda39a7c
commit 9e13105bba
No known key found for this signature in database
GPG key ID: 7395DCAE58289CA9
9 changed files with 102 additions and 11 deletions

View file

@ -16,6 +16,7 @@ def get_conf(prefix='LARIGIRA_'):
conf['CACHING_TIME'] = 10 conf['CACHING_TIME'] = 10
conf['DB_URI'] = 'larigira.db' conf['DB_URI'] = 'larigira.db'
conf['BOOTSTRAP_SERVE_LOCAL'] = True conf['BOOTSTRAP_SERVE_LOCAL'] = True
conf['SECRET_KEY'] = 'Please replace me!'
conf.update(from_envvars(prefix=prefix)) conf.update(from_envvars(prefix=prefix))
return conf return conf

View file

@ -5,14 +5,42 @@ Templates are self-contained in this directory
''' '''
from __future__ import print_function 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 = Blueprint('db', __name__, url_prefix='/db', template_folder='templates')
@db.route('/list') @db.route('/list')
def db_list(): def list():
model = current_app.larigira.monitor.source.model model = current_app.larigira.monitor.source.model
alarms = tuple(model.get_all_alarms()) alarms = tuple(model.get_all_alarms())
events = [(alarm, model.get_actions_by_alarm(alarm)) events = [(alarm, model.get_actions_by_alarm(alarm))
for alarm in alarms] for alarm in alarms]
return render_template('list.html', events=events) 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))

View 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: #}

View 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: #}

View file

@ -13,18 +13,13 @@
<span class="icon-bar"></span> <span class="icon-bar"></span>
<span class="icon-bar"></span> <span class="icon-bar"></span>
</button> </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>{# navbar-header #}
<div class="collapse navbar-collapse" id="bs-navbar-collapse-1"> <div class="collapse navbar-collapse" id="bs-navbar-collapse-1">
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
<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.db_list')}}">List</a></li> <li><a href="{{url_for('db.addtime')}}">Add time</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>
</ul> </ul>
</div>{# collapse #} </div>{# collapse #}

View file

@ -11,3 +11,7 @@ def get_one_entrypoint(group, kind):
if len(points) > 1: if len(points) > 1:
log.warning("Found more than one timeform for %s:%s" % (group, kind)) log.warning("Found more than one timeform for %s:%s" % (group, kind))
return points[0].load() 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
View 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)

View 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')
}

View file

@ -41,6 +41,8 @@ setup(name='larigira',
'flask', 'flask',
'flask-bootstrap', 'flask-bootstrap',
'python-mpd2', 'python-mpd2',
'wtforms',
'Flask-WTF',
'tinydb' 'tinydb'
], ],
tests_require=['pytest', 'pytest-timeout'], tests_require=['pytest', 'pytest-timeout'],
@ -59,6 +61,12 @@ setup(name='larigira',
'larigira.timegenerators': [ 'larigira.timegenerators': [
'frequency = larigira.timegen_every:FrequencyAlarm', 'frequency = larigira.timegen_every:FrequencyAlarm',
'single = larigira.timegen_every:SingleAlarm', 'single = larigira.timegen_every:SingleAlarm',
] ],
'larigira.timeform_create': [
'single = larigira.timeform_single:SingleAlarmForm',
],
'larigira.timeform_receive': [
'single = larigira.timeform_single:singlealarm_receive',
],
} }
) )