frequency form is aware of human intervals

refs #20
This commit is contained in:
boyska 2016-09-09 10:41:10 +02:00
parent 6977608c7e
commit 676224dbeb
No known key found for this signature in database
GPG key ID: 7395DCAE58289CA9

View file

@ -1,8 +1,9 @@
from __future__ import print_function
from pytimeparse.timeparse import timeparse
from flask_wtf import Form
from wtforms import StringField, DateTimeField, IntegerField, validators, \
SubmitField
from wtforms import StringField, DateTimeField, validators, \
SubmitField, ValidationError
class FrequencyAlarmForm(Form):
@ -16,11 +17,21 @@ class FrequencyAlarmForm(Form):
validators=[validators.optional()],
description='Date after which no alarm will ring, '
'expressed as YYYY-MM-DD HH:MM:SS')
interval = IntegerField(u'Frequency',
interval = StringField(u'Frequency',
validators=[validators.required()],
description='in seconds')
description='in seconds, or human-readable '
'(like 9w3d12h)')
submit = SubmitField(u'Submit')
def validate_interval(form, field):
try:
int(field.data)
except ValueError:
if timeparse(field.data) is None:
raise ValidationError("interval must either be a number "
"(in seconds) or a human-readable "
"string like '1h2m' or '1d12h'")
def frequencyalarm_receive(form):
obj = {
@ -32,4 +43,3 @@ def frequencyalarm_receive(form):
if form.end.data:
obj['end'] = int(form.end.data.strftime('%s'))
return obj