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