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 EditForm(wtforms
.Form
):
28 title
= wtforms
.TextField(
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(
38 [tag_length_validator
],
40 "Separate tags by commas."))
41 slug
= wtforms
.TextField(
43 [wtforms
.validators
.InputRequired(message
=_("The slug can't be empty"))],
45 "The title part of this media's address. "
46 "You usually don't need to change this."))
47 license
= wtforms
.SelectField(
49 [wtforms
.validators
.Optional(),],
50 choices
=licenses_as_choices())
52 class EditProfileForm(wtforms
.Form
):
53 bio
= wtforms
.TextAreaField(
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(
61 [wtforms
.validators
.Optional(),
62 wtforms
.validators
.URL(message
=_("This address contains errors"))])
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'),
73 wtforms
.validators
.Optional(),
74 wtforms
.validators
.AnyOf([lic
[0] for lic
in licenses_as_choices()]),
76 choices
=licenses_as_choices(),
77 description
=_('This will be your default license on upload forms.'))
80 class EditAttachmentsForm(wtforms
.Form
):
81 attachment_name
= wtforms
.TextField(
83 attachment_file
= wtforms
.FileField(
87 class EditCollectionForm(wtforms
.Form
):
88 title
= wtforms
.TextField(
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(
98 [wtforms
.validators
.InputRequired(message
=_("The slug can't be empty"))],
100 "The title part of this collection's address. "
101 "You usually don't need to change this."))
104 class ChangePassForm(wtforms
.Form
):
105 old_password
= wtforms
.PasswordField(
107 [wtforms
.validators
.InputRequired()],
109 "Enter your old password to prove you own this account."))
110 new_password
= wtforms
.PasswordField(
112 [wtforms
.validators
.InputRequired(),
113 wtforms
.validators
.Length(min=6, max=30)],
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(
124 [wtforms
.validators
.InputRequired()],
126 "Enter your password to prove you own this account."))
128 class MetaDataValidator(object):
130 Custom validator which runs form data in a MetaDataForm through a jsonschema
131 validator and passes errors recieved in jsonschema to wtforms.
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.
140 def __init__(self
, schema
=DEFAULT_SCHEMA
, format_checker
=DEFAULT_CHECKER
):
142 self
.format_checker
= format_checker
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
)
149 for e
in validator
.iter_errors(metadata_dict
)]
151 raise wtforms
.validators
.ValidationError(
154 class MetaDataForm(wtforms
.Form
):
155 identifier
= wtforms
.TextField(_(u
'Identifier'),[MetaDataValidator()])
156 value
= wtforms
.TextField(_(u
'Value'))
158 class EditMetaDataForm(wtforms
.Form
):
159 media_metadata
= wtforms
.FieldList(
160 wtforms
.FormField(MetaDataForm
, ""),