Something something like soundcloud but not like soundcloud.
Log in, upload records, done.
Simple, easy, KISS.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

forms.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. from flask_security import RegisterForm, current_user
  2. from flask_uploads import UploadSet, AUDIO
  3. from flask_wtf import FlaskForm as Form
  4. from flask_wtf.file import FileField, FileAllowed, FileRequired
  5. from wtforms import SubmitField, SelectField, BooleanField, TextAreaField
  6. from wtforms import widgets
  7. from wtforms.fields.core import StringField
  8. from wtforms.validators import DataRequired, ValidationError, Length, Regexp
  9. from wtforms_alchemy import model_form_factory
  10. from flask_babelex import gettext
  11. from wtforms.ext.sqlalchemy.fields import QuerySelectField
  12. from models import db, User, Album, licences
  13. BaseModelForm = model_form_factory(Form)
  14. sounds = UploadSet("sounds", AUDIO)
  15. class PasswordFieldNotHidden(StringField):
  16. """
  17. Original source: https://github.com/wtforms/wtforms/blob/2.0.2/wtforms/fields/simple.py#L35-L42 # noqa: E501
  18. A StringField, except renders an ``<input type="password">``.
  19. Also, whatever value is accepted by this field is not rendered back
  20. to the browser like normal fields.
  21. """
  22. widget = widgets.PasswordInput(hide_value=False)
  23. class ModelForm(BaseModelForm):
  24. @classmethod
  25. def get_session(cls):
  26. return db.session
  27. class ExtendedRegisterForm(RegisterForm):
  28. name = StringField("Username", [DataRequired(), Regexp(regex=r"^\w+$"), Length(max=150)])
  29. def validate_name(form, field):
  30. if len(field.data) <= 0:
  31. raise ValidationError(gettext("Username required"))
  32. u = db.session.query(User).filter_by(name_insensitive=field.data).first()
  33. if u:
  34. raise ValidationError(gettext("Username already taken"))
  35. class UserProfileForm(Form):
  36. display_name = StringField(gettext("Display name"), [Length(max=30)])
  37. timezone = SelectField(coerce=str, label=gettext("Timezone"), default="UTC")
  38. locale = SelectField(gettext("Locale"), default="en", choices=[["en", "English"], ["fr", "French"]])
  39. submit = SubmitField(gettext("Update profile"))
  40. class ConfigForm(Form):
  41. app_name = StringField(gettext("Instance Name"), [DataRequired(), Length(max=255)])
  42. app_description = TextAreaField(gettext("Instance description"))
  43. submit = SubmitField(gettext("Update config"))
  44. def get_albums():
  45. return Album.query.filter(Album.user_id == current_user.id).all()
  46. def get_licences():
  47. return [(licences[a]["id"], licences[a]["name"]) for a in licences]
  48. class SoundUploadForm(Form):
  49. title = StringField(gettext("Title"), [Length(max=255)])
  50. sound = FileField(gettext("File"), [FileRequired(), FileAllowed(AUDIO)])
  51. album = QuerySelectField(
  52. query_factory=get_albums,
  53. allow_blank=True,
  54. label=gettext("Album"),
  55. blank_text=gettext("No album"),
  56. get_label="title",
  57. )
  58. licence = SelectField(choices=get_licences(), coerce=int, label=gettext("Licence"))
  59. private = BooleanField(gettext("Private"), default=False)
  60. def validate_private(form, field):
  61. if field.data is True and form.album.data.private is False:
  62. raise ValidationError(gettext("Cannot put private sound in public album"))
  63. submit = SubmitField(gettext("Upload"))
  64. class SoundEditForm(Form):
  65. title = StringField(gettext("Title"), [Length(max=255)])
  66. description = TextAreaField(gettext("Description"))
  67. album = QuerySelectField(
  68. query_factory=get_albums,
  69. allow_blank=True,
  70. label=gettext("Album"),
  71. blank_text=gettext("No album"),
  72. get_label="title",
  73. )
  74. licence = SelectField(choices=get_licences(), coerce=int, label=gettext("Licence"))
  75. private = BooleanField(gettext("Private"), default=False)
  76. def validate_private(form, field):
  77. if field.data is True and form.album.data.private is False:
  78. raise ValidationError(gettext("Cannot put private sound in public album"))
  79. submit = SubmitField(gettext("Edit sound"))
  80. class AlbumForm(Form):
  81. title = StringField(gettext("Title"), [Length(max=255), DataRequired()])
  82. description = TextAreaField(gettext("Description"))
  83. private = BooleanField(gettext("Private"), default=False)
  84. submit = SubmitField(gettext("Save"))