From 9e13105bba141da3acad97207afe0d8ec19f15cf Mon Sep 17 00:00:00 2001 From: boyska Date: Sun, 1 Feb 2015 21:47:12 +0100 Subject: [PATCH] DBAdmin can add alarms of kind 'single' to db The entrypoint infrastructure is ready. --- larigira/config.py | 1 + larigira/dbadmin/__init__.py | 32 +++++++++++++++++-- larigira/dbadmin/templates/add_time.html | 15 +++++++++ larigira/dbadmin/templates/add_time_kind.html | 11 +++++++ larigira/dbadmin/templates/dbadmin_base.html | 11 ++----- larigira/entrypoints_utils.py | 4 +++ larigira/forms.py | 9 ++++++ larigira/timeform_single.py | 20 ++++++++++++ setup.py | 10 +++++- 9 files changed, 102 insertions(+), 11 deletions(-) create mode 100644 larigira/dbadmin/templates/add_time.html create mode 100644 larigira/dbadmin/templates/add_time_kind.html create mode 100644 larigira/forms.py create mode 100644 larigira/timeform_single.py diff --git a/larigira/config.py b/larigira/config.py index 3ae9425..d5d5bbe 100644 --- a/larigira/config.py +++ b/larigira/config.py @@ -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 diff --git a/larigira/dbadmin/__init__.py b/larigira/dbadmin/__init__.py index d179942..ea22916 100644 --- a/larigira/dbadmin/__init__.py +++ b/larigira/dbadmin/__init__.py @@ -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/') +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/', 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)) diff --git a/larigira/dbadmin/templates/add_time.html b/larigira/dbadmin/templates/add_time.html new file mode 100644 index 0000000..4d66c3a --- /dev/null +++ b/larigira/dbadmin/templates/add_time.html @@ -0,0 +1,15 @@ +{% extends "dbadmin_base.html" %} +{% block title %}Larigira - DB add time {%endblock%} + +{% block content %} +
+

Add time specification

+ Available kinds: + +
+{% endblock content %} +{# vim: set ts=2 sw=2 noet: #} diff --git a/larigira/dbadmin/templates/add_time_kind.html b/larigira/dbadmin/templates/add_time_kind.html new file mode 100644 index 0000000..806c37c --- /dev/null +++ b/larigira/dbadmin/templates/add_time_kind.html @@ -0,0 +1,11 @@ +{% extends "dbadmin_base.html" %} +{% import "bootstrap/wtf.html" as wtf %} + +{% block title %}Larigira - DB add time "{{kind}}" {%endblock%} + +{% block content %} +
+ {{wtf.quick_form(form)}} +
+{% endblock content %} +{# vim: set ts=2 sw=2 noet: #} diff --git a/larigira/dbadmin/templates/dbadmin_base.html b/larigira/dbadmin/templates/dbadmin_base.html index 41a3c51..10604cd 100644 --- a/larigira/dbadmin/templates/dbadmin_base.html +++ b/larigira/dbadmin/templates/dbadmin_base.html @@ -13,18 +13,13 @@ -
Larigira + Larigira {# navbar-header #} {# collapse #} diff --git a/larigira/entrypoints_utils.py b/larigira/entrypoints_utils.py index 1f556cf..11e61d8 100644 --- a/larigira/entrypoints_utils.py +++ b/larigira/entrypoints_utils.py @@ -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)] diff --git a/larigira/forms.py b/larigira/forms.py new file mode 100644 index 0000000..a8ca4c0 --- /dev/null +++ b/larigira/forms.py @@ -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) diff --git a/larigira/timeform_single.py b/larigira/timeform_single.py new file mode 100644 index 0000000..a9951be --- /dev/null +++ b/larigira/timeform_single.py @@ -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') + } diff --git a/setup.py b/setup.py index b55c36f..21395ae 100644 --- a/setup.py +++ b/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', + ], } )