Change codebase to query or create correct User model
[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):
f4686cde 28 title = wtforms.StringField(
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."""))
f4686cde 36 tags = wtforms.StringField(
4b1adc13 37 _('Tags'),
cee794a8
JS
38 [tag_length_validator],
39 description=_(
6f559060 40 "Separate tags by commas."))
f4686cde 41 slug = wtforms.StringField(
7b80685a 42 _('Slug'),
0742e11d 43 [wtforms.validators.InputRequired(message=_("The slug can't be empty"))],
7b80685a 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."""))
f4686cde 59 url = wtforms.StringField(
4b1adc13 60 _('Website'),
20d82d60 61 [wtforms.validators.Optional(),
f646e2e1 62 wtforms.validators.URL(message=_("This address contains errors"))])
c8071fa5 63
f4686cde 64 location = wtforms.StringField(_('Hometown'))
c8071fa5
JS
65
66class EditAccountForm(wtforms.Form):
1e21471a 67 wants_comment_notification = wtforms.BooleanField(
1e21471a 68 description=_("Email me when others comment on my media"))
93d805ad 69 wants_notifications = wtforms.BooleanField(
560fd638 70 description=_("Enable insite notifications about events."))
dc4dfbde
MH
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.'))
3a8c3a38 79
7b80685a 80
3a8c3a38 81class EditAttachmentsForm(wtforms.Form):
f4686cde 82 attachment_name = wtforms.StringField(
3a8c3a38
JW
83 'Title')
84 attachment_file = wtforms.FileField(
85 'File')
be5be115 86
74ae6fb0 87
be5be115 88class EditCollectionForm(wtforms.Form):
f4686cde 89 title = wtforms.StringField(
be5be115 90 _('Title'),
0742e11d 91 [wtforms.validators.Length(min=0, max=500), wtforms.validators.InputRequired(message=_("The title can't be empty"))])
be5be115
AW
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."""))
f4686cde 97 slug = wtforms.StringField(
be5be115 98 _('Slug'),
0742e11d 99 [wtforms.validators.InputRequired(message=_("The slug can't be empty"))],
be5be115
AW
100 description=_(
101 "The title part of this collection's address. "
102 "You usually don't need to change this."))
39aa1db4
RE
103
104
105class ChangePassForm(wtforms.Form):
106 old_password = wtforms.PasswordField(
107 _('Old password'),
0742e11d 108 [wtforms.validators.InputRequired()],
39aa1db4
RE
109 description=_(
110 "Enter your old password to prove you own this account."))
111 new_password = wtforms.PasswordField(
112 _('New password'),
0742e11d 113 [wtforms.validators.InputRequired(),
2ba76034
RE
114 wtforms.validators.Length(min=6, max=30)],
115 id="password")
402f4360
RE
116
117
118class ChangeEmailForm(wtforms.Form):
f4686cde 119 new_email = wtforms.StringField(
402f4360 120 _('New email address'),
0742e11d 121 [wtforms.validators.InputRequired(),
402f4360
RE
122 normalize_user_or_email_field(allow_user=False)])
123 password = wtforms.PasswordField(
124 _('Password'),
0742e11d 125 [wtforms.validators.InputRequired()],
402f4360
RE
126 description=_(
127 "Enter your password to prove you own this account."))
fffc5dcf 128
0d6550fb 129class 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
f0cfd339 155class MetaDataForm(wtforms.Form):
f4686cde
JK
156 identifier = wtforms.StringField(_(u'Identifier'),[MetaDataValidator()])
157 value = wtforms.StringField(_(u'Value'))
f0cfd339 158
fffc5dcf 159class EditMetaDataForm(wtforms.Form):
160 media_metadata = wtforms.FieldList(
0d6550fb 161 wtforms.FormField(MetaDataForm, ""),
e80596c8 162 )