forms.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # GNU MediaGoblin -- federated, autonomous media hosting
  2. # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. import wtforms
  17. from jsonschema import Draft4Validator
  18. from mediagoblin.tools.text import tag_length_validator
  19. from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
  20. from mediagoblin.tools.licenses import licenses_as_choices
  21. from mediagoblin.tools.metadata import DEFAULT_SCHEMA, DEFAULT_CHECKER
  22. from mediagoblin.auth.tools import normalize_user_or_email_field
  23. class EditForm(wtforms.Form):
  24. title = wtforms.StringField(
  25. _('Title'),
  26. [wtforms.validators.Length(min=0, max=500)])
  27. description = wtforms.TextAreaField(
  28. _('Description of this work'),
  29. description=_("""You can use
  30. <a href="http://daringfireball.net/projects/markdown/basics">
  31. Markdown</a> for formatting."""))
  32. tags = wtforms.StringField(
  33. _('Tags'),
  34. [tag_length_validator],
  35. description=_(
  36. "Separate tags by commas."))
  37. slug = wtforms.StringField(
  38. _('Slug'),
  39. [wtforms.validators.InputRequired(message=_("The slug can't be empty"))],
  40. description=_(
  41. "The title part of this media's address. "
  42. "You usually don't need to change this."))
  43. license = wtforms.SelectField(
  44. _('License'),
  45. [wtforms.validators.Optional(),],
  46. choices=licenses_as_choices())
  47. class EditProfileForm(wtforms.Form):
  48. bio = wtforms.TextAreaField(
  49. _('Bio'),
  50. [wtforms.validators.Length(min=0, max=500)],
  51. description=_("""You can use
  52. <a href="http://daringfireball.net/projects/markdown/basics">
  53. Markdown</a> for formatting."""))
  54. url = wtforms.StringField(
  55. _('Website'),
  56. [wtforms.validators.Optional(),
  57. wtforms.validators.URL(message=_("This address contains errors"))])
  58. location = wtforms.StringField(_('Hometown'))
  59. class EditAccountForm(wtforms.Form):
  60. wants_comment_notification = wtforms.BooleanField(
  61. description=_("Email me when others comment on my media"))
  62. wants_notifications = wtforms.BooleanField(
  63. description=_("Enable insite notifications about events."))
  64. license_preference = wtforms.SelectField(
  65. _('License preference'),
  66. [
  67. wtforms.validators.Optional(),
  68. wtforms.validators.AnyOf([lic[0] for lic in licenses_as_choices()]),
  69. ],
  70. choices=licenses_as_choices(),
  71. description=_('This will be your default license on upload forms.'))
  72. class EditAttachmentsForm(wtforms.Form):
  73. attachment_name = wtforms.StringField(
  74. 'Title')
  75. attachment_file = wtforms.FileField(
  76. 'File')
  77. class EditCollectionForm(wtforms.Form):
  78. title = wtforms.StringField(
  79. _('Title'),
  80. [wtforms.validators.Length(min=0, max=500), wtforms.validators.InputRequired(message=_("The title can't be empty"))])
  81. description = wtforms.TextAreaField(
  82. _('Description of this collection'),
  83. description=_("""You can use
  84. <a href="http://daringfireball.net/projects/markdown/basics">
  85. Markdown</a> for formatting."""))
  86. slug = wtforms.StringField(
  87. _('Slug'),
  88. [wtforms.validators.InputRequired(message=_("The slug can't be empty"))],
  89. description=_(
  90. "The title part of this collection's address. "
  91. "You usually don't need to change this."))
  92. class ChangePassForm(wtforms.Form):
  93. old_password = wtforms.PasswordField(
  94. _('Old password'),
  95. [wtforms.validators.InputRequired()],
  96. description=_(
  97. "Enter your old password to prove you own this account."))
  98. new_password = wtforms.PasswordField(
  99. _('New password'),
  100. [wtforms.validators.InputRequired(),
  101. wtforms.validators.Length(min=6, max=30)],
  102. id="password")
  103. class ChangeEmailForm(wtforms.Form):
  104. new_email = wtforms.StringField(
  105. _('New email address'),
  106. [wtforms.validators.InputRequired(),
  107. normalize_user_or_email_field(allow_user=False)])
  108. password = wtforms.PasswordField(
  109. _('Password'),
  110. [wtforms.validators.InputRequired()],
  111. description=_(
  112. "Enter your password to prove you own this account."))
  113. class MetaDataValidator(object):
  114. """
  115. Custom validator which runs form data in a MetaDataForm through a jsonschema
  116. validator and passes errors recieved in jsonschema to wtforms.
  117. :param schema The json schema to validate the data against. By
  118. default this uses the DEFAULT_SCHEMA from
  119. mediagoblin.tools.metadata.
  120. :param format_checker The FormatChecker object that limits which types
  121. jsonschema can recognize. By default this uses
  122. DEFAULT_CHECKER from mediagoblin.tools.metadata.
  123. """
  124. def __init__(self, schema=DEFAULT_SCHEMA, format_checker=DEFAULT_CHECKER):
  125. self.schema = schema
  126. self.format_checker = format_checker
  127. def __call__(self, form, field):
  128. metadata_dict = {field.data:form.value.data}
  129. validator = Draft4Validator(self.schema,
  130. format_checker=self.format_checker)
  131. errors = [e.message
  132. for e in validator.iter_errors(metadata_dict)]
  133. if len(errors) >= 1:
  134. raise wtforms.validators.ValidationError(
  135. errors.pop())
  136. class MetaDataForm(wtforms.Form):
  137. identifier = wtforms.StringField(_(u'Identifier'),[MetaDataValidator()])
  138. value = wtforms.StringField(_(u'Value'))
  139. class EditMetaDataForm(wtforms.Form):
  140. media_metadata = wtforms.FieldList(
  141. wtforms.FormField(MetaDataForm, ""),
  142. )