Removed almost all of the code from core
[mediagoblin.git] / mediagoblin / edit / forms.py
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 WebsiteField(wtforms.StringField):
28 """A field that expects a website URL but adds http:// if not provided."""
29 def process_formdata(self, valuelist):
30 if valuelist:
31 data = valuelist[0]
32 if not data.startswith((u'http://', u'https://')):
33 data = u'http://' + data
34 self.data = data
35 else:
36 super(WebsiteField, self).process_formdata(valuelist)
37
38
39 class EditForm(wtforms.Form):
40 title = wtforms.StringField(
41 _('Title'),
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(
49 _('Tags'),
50 [tag_length_validator],
51 description=_(
52 "Separate tags by commas."))
53 slug = wtforms.StringField(
54 _('Slug'),
55 [wtforms.validators.InputRequired(message=_("The slug can't be empty"))],
56 description=_(
57 "The title part of this media's address. "
58 "You usually don't need to change this."))
59 license = wtforms.SelectField(
60 _('License'),
61 [wtforms.validators.Optional(),],
62 choices=licenses_as_choices())
63
64
65 class EditProfileForm(wtforms.Form):
66 bio = wtforms.TextAreaField(
67 _('Bio'),
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."""))
72 url = WebsiteField(
73 _('Website'),
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"))
78
79 location = wtforms.StringField(_('Hometown'))
80
81
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'),
89 [
90 wtforms.validators.Optional(),
91 wtforms.validators.AnyOf([lic[0] for lic in licenses_as_choices()]),
92 ],
93 choices=licenses_as_choices(),
94 description=_('This will be your default license on upload forms.'))
95
96
97 class EditAttachmentsForm(wtforms.Form):
98 attachment_name = wtforms.StringField(
99 'Title')
100 attachment_file = wtforms.FileField(
101 'File')
102
103
104 class EditCollectionForm(wtforms.Form):
105 title = wtforms.StringField(
106 _('Title'),
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(
114 _('Slug'),
115 [wtforms.validators.InputRequired(message=_("The slug can't be empty"))],
116 description=_(
117 "The title part of this collection's address. "
118 "You usually don't need to change this."))
119
120
121 class ChangePassForm(wtforms.Form):
122 old_password = wtforms.PasswordField(
123 _('Old password'),
124 [wtforms.validators.InputRequired()],
125 description=_(
126 "Enter your old password to prove you own this account."))
127 new_password = wtforms.PasswordField(
128 _('New password'),
129 [wtforms.validators.InputRequired(),
130 wtforms.validators.Length(min=6, max=30)],
131 id="password")
132
133
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(
140 _('Password'),
141 [wtforms.validators.InputRequired()],
142 description=_(
143 "Enter your password to prove you own this account."))
144
145
146 class MetaDataValidator(object):
147 """
148 Custom validator which runs form data in a MetaDataForm through a jsonschema
149 validator and passes errors recieved in jsonschema to wtforms.
150
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.
157 """
158 def __init__(self, schema=DEFAULT_SCHEMA, format_checker=DEFAULT_CHECKER):
159 self.schema = schema
160 self.format_checker = format_checker
161
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)
166 errors = [e.message
167 for e in validator.iter_errors(metadata_dict)]
168 if len(errors) >= 1:
169 raise wtforms.validators.ValidationError(
170 errors.pop())
171
172
173 class MetaDataForm(wtforms.Form):
174 identifier = wtforms.StringField(_(u'Identifier'),[MetaDataValidator()])
175 value = wtforms.StringField(_(u'Value'))
176
177
178 class EditMetaDataForm(wtforms.Form):
179 media_metadata = wtforms.FieldList(
180 wtforms.FormField(MetaDataForm, ""),
181 )