audioform_mostrecent.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. from pytimeparse.timeparse import timeparse
  2. from flask_wtf import Form
  3. from wtforms import StringField, validators, SubmitField, ValidationError
  4. class AudioForm(Form):
  5. nick = StringField('Audio nick', validators=[validators.required()],
  6. description='A simple name to recognize this audio')
  7. path = StringField('Path', validators=[validators.required()],
  8. description='Directory to pick file from')
  9. maxage = StringField('Max age',
  10. validators=[validators.required()],
  11. description='in seconds, or human-readable '
  12. '(like 9w3d12h)')
  13. submit = SubmitField('Submit')
  14. def validate_maxage(form, field):
  15. try:
  16. int(field.data)
  17. except ValueError:
  18. if timeparse(field.data) is None:
  19. raise ValidationError("maxage must either be a number "
  20. "(in seconds) or a human-readable "
  21. "string like '1h2m' or '1d12h'")
  22. def audio_receive(form):
  23. return {
  24. 'kind': 'mostrecent',
  25. 'nick': form.nick.data,
  26. 'path': form.path.data,
  27. 'maxage': form.maxage.data,
  28. 'howmany': 1
  29. }