audioform_podcast.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from flask_wtf import Form
  2. from wtforms import (BooleanField, IntegerField, SelectField, StringField,
  3. SubmitField, validators)
  4. from wtforms.fields.html5 import URLField
  5. class AudioForm(Form):
  6. nick = StringField(
  7. "Audio nick",
  8. validators=[validators.required()],
  9. description="A simple name to recognize this audio",
  10. )
  11. url = URLField(
  12. "URL",
  13. validators=[validators.required()],
  14. description="URL of the podcast; it must be valid xml",
  15. )
  16. # TODO: group by filters/sort/select
  17. min_len = StringField(
  18. "Accetta solo audio lunghi almeno:",
  19. description="Leaving this empty will disable this filter",
  20. )
  21. max_len = StringField(
  22. "Accetta solo audio lunghi al massimo:",
  23. description="Leaving this empty will disable this filter",
  24. )
  25. sort_by = SelectField(
  26. "Sort episodes",
  27. choices=[
  28. ("none", "Don't sort"),
  29. ("random", "Random"),
  30. ("duration", "Duration"),
  31. ("date", "date"),
  32. ],
  33. )
  34. start = IntegerField(
  35. "Play from episode number",
  36. description="Episodes count from 0; 0 is a sane default",
  37. )
  38. reverse = BooleanField("Reverse sort (descending)")
  39. submit = SubmitField("Submit")
  40. def populate_from_audiospec(self, audiospec):
  41. for key in ("nick", "url", "sort_by", "reverse", "min_len", "max_len"):
  42. if key in audiospec:
  43. getattr(self, key).data = audiospec[key]
  44. self.start.data = int(audiospec.get("start", 0))
  45. def audio_receive(form):
  46. d = {"kind": "podcast"}
  47. for key in (
  48. "nick",
  49. "url",
  50. "sort_by",
  51. "reverse",
  52. "min_len",
  53. "max_len",
  54. "start",
  55. ):
  56. d[key] = getattr(form, key).data
  57. return d