Merge branch '905-activities'
[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 EditForm(wtforms.Form):
28 title = wtforms.TextField(
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.TextField(
37 _('Tags'),
38 [tag_length_validator],
39 description=_(
40 "Separate tags by commas."))
41 slug = wtforms.TextField(
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.TextField(
60 _('Website'),
61 [wtforms.validators.Optional(),
62 wtforms.validators.URL(message=_("This address contains errors"))])
63
64
65 class EditAccountForm(wtforms.Form):
66 wants_comment_notification = wtforms.BooleanField(
67 description=_("Email me when others comment on my media"))
68 wants_notifications = wtforms.BooleanField(
69 description=_("Enable insite notifications about events."))
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.'))
78
79
80 class EditAttachmentsForm(wtforms.Form):
81 attachment_name = wtforms.TextField(
82 'Title')
83 attachment_file = wtforms.FileField(
84 'File')
85
86
87 class EditCollectionForm(wtforms.Form):
88 title = wtforms.TextField(
89 _('Title'),
90 [wtforms.validators.Length(min=0, max=500), wtforms.validators.InputRequired(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.InputRequired(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."))
102
103
104 class ChangePassForm(wtforms.Form):
105 old_password = wtforms.PasswordField(
106 _('Old password'),
107 [wtforms.validators.InputRequired()],
108 description=_(
109 "Enter your old password to prove you own this account."))
110 new_password = wtforms.PasswordField(
111 _('New password'),
112 [wtforms.validators.InputRequired(),
113 wtforms.validators.Length(min=6, max=30)],
114 id="password")
115
116
117 class ChangeEmailForm(wtforms.Form):
118 new_email = wtforms.TextField(
119 _('New email address'),
120 [wtforms.validators.InputRequired(),
121 normalize_user_or_email_field(allow_user=False)])
122 password = wtforms.PasswordField(
123 _('Password'),
124 [wtforms.validators.InputRequired()],
125 description=_(
126 "Enter your password to prove you own this account."))
127
128 class 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
154 class MetaDataForm(wtforms.Form):
155 identifier = wtforms.TextField(_(u'Identifier'),[MetaDataValidator()])
156 value = wtforms.TextField(_(u'Value'))
157
158 class EditMetaDataForm(wtforms.Form):
159 media_metadata = wtforms.FieldList(
160 wtforms.FormField(MetaDataForm, ""),
161 )