1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
18 from jsonschema
import Draft4Validator
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
27 class WebsiteField(wtforms
.StringField
):
28 """A field that expects a website URL but adds http:// if not provided."""
29 def process_formdata(self
, valuelist
):
32 if not data
.startswith((u
'http://', u
'https://')):
33 data
= u
'http://' + data
36 super(WebsiteField
, self
).process_formdata(valuelist
)
39 class EditForm(wtforms
.Form
):
40 title
= wtforms
.StringField(
42 [wtforms
.validators
.Length(min=0, max=500)])
43 description
= wtforms
.TextAreaField(
44 _('Description of this work'),
45 description
=_("""You can use
46 <a href="http://daringfireball.net/projects/markdown/basics">
47 Markdown</a> for formatting."""))
48 tags
= wtforms
.StringField(
50 [tag_length_validator
],
52 "Separate tags by commas."))
53 slug
= wtforms
.StringField(
55 [wtforms
.validators
.InputRequired(message
=_("The slug can't be empty"))],
57 "The title part of this media's address. "
58 "You usually don't need to change this."))
59 license
= wtforms
.SelectField(
61 [wtforms
.validators
.Optional(),],
62 choices
=licenses_as_choices())
65 class EditProfileForm(wtforms
.Form
):
66 bio
= wtforms
.TextAreaField(
68 [wtforms
.validators
.Length(min=0, max=500)],
69 description
=_("""You can use
70 <a href="http://daringfireball.net/projects/markdown/basics">
71 Markdown</a> for formatting."""))
74 [wtforms
.validators
.Optional(),
75 wtforms
.validators
.URL(message
=_("This address contains errors"))],
76 description
=_("www.example.com, http://www.example.com or "
77 "https://www.example.com"))
79 location
= wtforms
.StringField(_('Hometown'))
82 class EditAccountForm(wtforms
.Form
):
83 wants_comment_notification
= wtforms
.BooleanField(
84 description
=_("Email me when others comment on my media"))
85 wants_notifications
= wtforms
.BooleanField(
86 description
=_("Enable insite notifications about events."))
87 license_preference
= wtforms
.SelectField(
88 _('License preference'),
90 wtforms
.validators
.Optional(),
91 wtforms
.validators
.AnyOf([lic
[0] for lic
in licenses_as_choices()]),
93 choices
=licenses_as_choices(),
94 description
=_('This will be your default license on upload forms.'))
97 class EditAttachmentsForm(wtforms
.Form
):
98 attachment_name
= wtforms
.StringField(
100 attachment_file
= wtforms
.FileField(
104 class EditCollectionForm(wtforms
.Form
):
105 title
= wtforms
.StringField(
107 [wtforms
.validators
.Length(min=0, max=500), wtforms
.validators
.InputRequired(message
=_("The title can't be empty"))])
108 description
= wtforms
.TextAreaField(
109 _('Description of this collection'),
110 description
=_("""You can use
111 <a href="http://daringfireball.net/projects/markdown/basics">
112 Markdown</a> for formatting."""))
113 slug
= wtforms
.StringField(
115 [wtforms
.validators
.InputRequired(message
=_("The slug can't be empty"))],
117 "The title part of this collection's address. "
118 "You usually don't need to change this."))
121 class ChangePassForm(wtforms
.Form
):
122 old_password
= wtforms
.PasswordField(
124 [wtforms
.validators
.InputRequired()],
126 "Enter your old password to prove you own this account."))
127 new_password
= wtforms
.PasswordField(
129 [wtforms
.validators
.InputRequired(),
130 wtforms
.validators
.Length(min=6, max=30)],
134 class ChangeEmailForm(wtforms
.Form
):
135 new_email
= wtforms
.StringField(
136 _('New email address'),
137 [wtforms
.validators
.InputRequired(),
138 normalize_user_or_email_field(allow_user
=False)])
139 password
= wtforms
.PasswordField(
141 [wtforms
.validators
.InputRequired()],
143 "Enter your password to prove you own this account."))
146 class MetaDataValidator(object):
148 Custom validator which runs form data in a MetaDataForm through a jsonschema
149 validator and passes errors recieved in jsonschema to wtforms.
151 :param schema The json schema to validate the data against. By
152 default this uses the DEFAULT_SCHEMA from
153 mediagoblin.tools.metadata.
154 :param format_checker The FormatChecker object that limits which types
155 jsonschema can recognize. By default this uses
156 DEFAULT_CHECKER from mediagoblin.tools.metadata.
158 def __init__(self
, schema
=DEFAULT_SCHEMA
, format_checker
=DEFAULT_CHECKER
):
160 self
.format_checker
= format_checker
162 def __call__(self
, form
, field
):
163 metadata_dict
= {field
.data
:form
.value
.data
}
164 validator
= Draft4Validator(self
.schema
,
165 format_checker
=self
.format_checker
)
167 for e
in validator
.iter_errors(metadata_dict
)]
169 raise wtforms
.validators
.ValidationError(
173 class MetaDataForm(wtforms
.Form
):
174 identifier
= wtforms
.StringField(_(u
'Identifier'),[MetaDataValidator()])
175 value
= wtforms
.StringField(_(u
'Value'))
178 class EditMetaDataForm(wtforms
.Form
):
179 media_metadata
= wtforms
.FieldList(
180 wtforms
.FormField(MetaDataForm
, ""),