| 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 | |
| 17 | import wtforms |
| 18 | from jsonschema import Draft4Validator |
| 19 | |
| 20 | from mediagoblin.tools.text import tag_length_validator |
| 21 | from mediagoblin.tools.translate import lazy_pass_to_ugettext as _ |
| 22 | from mediagoblin.tools.licenses import licenses_as_choices |
| 23 | from mediagoblin.tools.metadata import DEFAULT_SCHEMA, DEFAULT_CHECKER |
| 24 | from mediagoblin.auth.tools import normalize_user_or_email_field |
| 25 | |
| 26 | |
| 27 | class EditForm(wtforms.Form): |
| 28 | title = wtforms.StringField( |
| 29 | _('Title'), |
| 30 | [wtforms.validators.Length(min=0, max=500)]) |
| 31 | description = wtforms.TextAreaField( |
| 32 | _('Description of this work'), |
| 33 | description=_("""You can use |
| 34 | <a href="http://daringfireball.net/projects/markdown/basics"> |
| 35 | Markdown</a> for formatting.""")) |
| 36 | tags = wtforms.StringField( |
| 37 | _('Tags'), |
| 38 | [tag_length_validator], |
| 39 | description=_( |
| 40 | "Separate tags by commas.")) |
| 41 | slug = wtforms.StringField( |
| 42 | _('Slug'), |
| 43 | [wtforms.validators.InputRequired(message=_("The slug can't be empty"))], |
| 44 | description=_( |
| 45 | "The title part of this media's address. " |
| 46 | "You usually don't need to change this.")) |
| 47 | license = wtforms.SelectField( |
| 48 | _('License'), |
| 49 | [wtforms.validators.Optional(),], |
| 50 | choices=licenses_as_choices()) |
| 51 | |
| 52 | class EditProfileForm(wtforms.Form): |
| 53 | bio = wtforms.TextAreaField( |
| 54 | _('Bio'), |
| 55 | [wtforms.validators.Length(min=0, max=500)], |
| 56 | description=_("""You can use |
| 57 | <a href="http://daringfireball.net/projects/markdown/basics"> |
| 58 | Markdown</a> for formatting.""")) |
| 59 | url = wtforms.StringField( |
| 60 | _('Website'), |
| 61 | [wtforms.validators.Optional(), |
| 62 | wtforms.validators.URL(message=_("This address contains errors"))]) |
| 63 | |
| 64 | location = wtforms.StringField(_('Hometown')) |
| 65 | |
| 66 | class EditAccountForm(wtforms.Form): |
| 67 | wants_comment_notification = wtforms.BooleanField( |
| 68 | description=_("Email me when others comment on my media")) |
| 69 | wants_notifications = wtforms.BooleanField( |
| 70 | description=_("Enable insite notifications about events.")) |
| 71 | license_preference = wtforms.SelectField( |
| 72 | _('License preference'), |
| 73 | [ |
| 74 | wtforms.validators.Optional(), |
| 75 | wtforms.validators.AnyOf([lic[0] for lic in licenses_as_choices()]), |
| 76 | ], |
| 77 | choices=licenses_as_choices(), |
| 78 | description=_('This will be your default license on upload forms.')) |
| 79 | |
| 80 | |
| 81 | class EditAttachmentsForm(wtforms.Form): |
| 82 | attachment_name = wtforms.StringField( |
| 83 | 'Title') |
| 84 | attachment_file = wtforms.FileField( |
| 85 | 'File') |
| 86 | |
| 87 | |
| 88 | class EditCollectionForm(wtforms.Form): |
| 89 | title = wtforms.StringField( |
| 90 | _('Title'), |
| 91 | [wtforms.validators.Length(min=0, max=500), wtforms.validators.InputRequired(message=_("The title can't be empty"))]) |
| 92 | description = wtforms.TextAreaField( |
| 93 | _('Description of this collection'), |
| 94 | description=_("""You can use |
| 95 | <a href="http://daringfireball.net/projects/markdown/basics"> |
| 96 | Markdown</a> for formatting.""")) |
| 97 | slug = wtforms.StringField( |
| 98 | _('Slug'), |
| 99 | [wtforms.validators.InputRequired(message=_("The slug can't be empty"))], |
| 100 | description=_( |
| 101 | "The title part of this collection's address. " |
| 102 | "You usually don't need to change this.")) |
| 103 | |
| 104 | |
| 105 | class ChangePassForm(wtforms.Form): |
| 106 | old_password = wtforms.PasswordField( |
| 107 | _('Old password'), |
| 108 | [wtforms.validators.InputRequired()], |
| 109 | description=_( |
| 110 | "Enter your old password to prove you own this account.")) |
| 111 | new_password = wtforms.PasswordField( |
| 112 | _('New password'), |
| 113 | [wtforms.validators.InputRequired(), |
| 114 | wtforms.validators.Length(min=6, max=30)], |
| 115 | id="password") |
| 116 | |
| 117 | |
| 118 | class ChangeEmailForm(wtforms.Form): |
| 119 | new_email = wtforms.StringField( |
| 120 | _('New email address'), |
| 121 | [wtforms.validators.InputRequired(), |
| 122 | normalize_user_or_email_field(allow_user=False)]) |
| 123 | password = wtforms.PasswordField( |
| 124 | _('Password'), |
| 125 | [wtforms.validators.InputRequired()], |
| 126 | description=_( |
| 127 | "Enter your password to prove you own this account.")) |
| 128 | |
| 129 | class MetaDataValidator(object): |
| 130 | """ |
| 131 | Custom validator which runs form data in a MetaDataForm through a jsonschema |
| 132 | validator and passes errors recieved in jsonschema to wtforms. |
| 133 | |
| 134 | :param schema The json schema to validate the data against. By |
| 135 | default this uses the DEFAULT_SCHEMA from |
| 136 | mediagoblin.tools.metadata. |
| 137 | :param format_checker The FormatChecker object that limits which types |
| 138 | jsonschema can recognize. By default this uses |
| 139 | DEFAULT_CHECKER from mediagoblin.tools.metadata. |
| 140 | """ |
| 141 | def __init__(self, schema=DEFAULT_SCHEMA, format_checker=DEFAULT_CHECKER): |
| 142 | self.schema = schema |
| 143 | self.format_checker = format_checker |
| 144 | |
| 145 | def __call__(self, form, field): |
| 146 | metadata_dict = {field.data:form.value.data} |
| 147 | validator = Draft4Validator(self.schema, |
| 148 | format_checker=self.format_checker) |
| 149 | errors = [e.message |
| 150 | for e in validator.iter_errors(metadata_dict)] |
| 151 | if len(errors) >= 1: |
| 152 | raise wtforms.validators.ValidationError( |
| 153 | errors.pop()) |
| 154 | |
| 155 | class MetaDataForm(wtforms.Form): |
| 156 | identifier = wtforms.StringField(_(u'Identifier'),[MetaDataValidator()]) |
| 157 | value = wtforms.StringField(_(u'Value')) |
| 158 | |
| 159 | class EditMetaDataForm(wtforms.Form): |
| 160 | media_metadata = wtforms.FieldList( |
| 161 | wtforms.FormField(MetaDataForm, ""), |
| 162 | ) |