diff --git a/larigira/dbadmin/__init__.py b/larigira/dbadmin/__init__.py index e1b5c8c..e3e111e 100644 --- a/larigira/dbadmin/__init__.py +++ b/larigira/dbadmin/__init__.py @@ -13,6 +13,10 @@ from larigira import forms db = Blueprint('db', __name__, url_prefix='/db', template_folder='templates') +def get_model(): + return current_app.larigira.controller.monitor.model + + @db.route('/') def home(): return render_template('dbadmin_base.html') @@ -33,17 +37,37 @@ def addtime(): return render_template('add_time.html', kinds=kinds) +@db.route('/edit/time/', methods=['GET', 'POST']) +def edit_time(alarmid): + model = get_model() + timespec = model.get_alarm_by_id(alarmid) + kind = timespec['kind'] + Form, receiver = tuple(forms.get_timeform(kind)) + form = Form() + if request.method == 'GET': + form.populate_from_timespec(timespec) + if request.method == 'POST' and form.validate(): + data = receiver(form) + model.update_alarm(alarmid, data) + model.reload() + return redirect(url_for('db.list', _anchor='event-%d' % alarmid)) + return render_template('add_time_kind.html', + form=form, + kind=kind, + mode='edit', + ) + + @db.route('/add/time/', methods=['GET', 'POST']) def addtime_kind(kind): Form, receiver = tuple(forms.get_timeform(kind)) form = Form() if request.method == 'POST' and form.validate(): data = receiver(form) - model = current_app.larigira.controller.monitor.model - eid = model.add_alarm(data) + eid = get_model().add_alarm(data) return redirect(url_for('db.edit_event', alarmid=eid)) - return render_template('add_time_kind.html', form=form, kind=kind) + return render_template('add_time_kind.html', form=form, kind=kind, mode='add') @db.route('/add/audio') diff --git a/larigira/dbadmin/templates/add_time_kind.html b/larigira/dbadmin/templates/add_time_kind.html index 6e10a0f..ce38eb8 100644 --- a/larigira/dbadmin/templates/add_time_kind.html +++ b/larigira/dbadmin/templates/add_time_kind.html @@ -10,6 +10,11 @@ resubmit {% endif %} +{% if mode == 'edit' %} +
+ Note: You can change parameters, but you cannot change the kind of this alarm (yet) +
+{% endif %}
{{wtf.quick_form(form)}} diff --git a/larigira/dbadmin/templates/list.html b/larigira/dbadmin/templates/list.html index 99c561d..b68b856 100644 --- a/larigira/dbadmin/templates/list.html +++ b/larigira/dbadmin/templates/list.html @@ -17,17 +17,19 @@ $(function() { {% block content %}
{% for e, actions in events %} -
+

Event {{e.eid}} {{e.nick if 'nick'}}

Time

+ + Change time for this event {{dict_table(e, ['nick', 'actions'])}}

Audio

- Change audio for this event + Change audios for this event
    {% for a in actions %}
  1. diff --git a/larigira/timeform_frequency.py b/larigira/timeform_frequency.py index d9859b2..d36f099 100644 --- a/larigira/timeform_frequency.py +++ b/larigira/timeform_frequency.py @@ -1,4 +1,4 @@ -from __future__ import print_function +from datetime import datetime from pytimeparse.timeparse import timeparse from flask_wtf import Form @@ -23,6 +23,15 @@ class FrequencyAlarmForm(Form): '(like 9w3d12h)') submit = SubmitField(u'Submit') + def populate_from_timespec(self, timespec): + if 'nick' in timespec: + self.nick.data = timespec['nick'] + if 'start' in timespec: + self.start.data = datetime.fromtimestamp(timespec['start']) + if 'end' in timespec: + self.end.data = datetime.fromtimestamp(timespec['end']) + self.interval.data = timespec['interval'] + def validate_interval(form, field): try: int(field.data) diff --git a/larigira/timeform_single.py b/larigira/timeform_single.py index 37e5f56..572ed96 100644 --- a/larigira/timeform_single.py +++ b/larigira/timeform_single.py @@ -1,4 +1,4 @@ -from __future__ import print_function +from datetime import datetime from flask_wtf import Form from wtforms import StringField, DateTimeField, validators, SubmitField @@ -12,6 +12,12 @@ class SingleAlarmForm(Form): 'YYYY-MM-DD HH:MM:SS') submit = SubmitField(u'Submit') + def populate_from_timespec(self, timespec): + if 'nick' in timespec: + self.nick.data = timespec['nick'] + if 'timestamp' in timespec: + self.dt.data = datetime.fromtimestamp(timespec['timestamp']) + def singlealarm_receive(form): return {