Fix #994: Don't require users to type the website URL scheme when updating profile.
authorBen Sturmfels <ben@sturm.com.au>
Sun, 23 Aug 2015 12:17:13 +0000 (22:17 +1000)
committerBerker Peksag <berker.peksag@gmail.com>
Tue, 25 Aug 2015 00:36:59 +0000 (03:36 +0300)
Adds 'http://' if no scheme is provided. Eg. If you enter
'www.example.com', this will be updated to 'http://www.example.com'.

mediagoblin/edit/forms.py

index cf5056cf1deefedc7b2d113ad265d12a12e52140..342e18618b4cfb81b2ee522cb68ed129f28b0bc5 100644 (file)
@@ -24,6 +24,15 @@ from mediagoblin.tools.metadata import DEFAULT_SCHEMA, DEFAULT_CHECKER
 from mediagoblin.auth.tools import normalize_user_or_email_field
 
 
+class WebsiteField(wtforms.StringField):
+    """A field that expects a website URL but adds http:// if not provided."""
+    def process_formdata(self, valuelist):
+        data = valuelist[0]
+        if not data.startswith((u'http://', u'https://')):
+            data = u'http://' + data
+        self.data = data
+
+
 class EditForm(wtforms.Form):
     title = wtforms.StringField(
         _('Title'),
@@ -49,6 +58,7 @@ class EditForm(wtforms.Form):
         [wtforms.validators.Optional(),],
         choices=licenses_as_choices())
 
+
 class EditProfileForm(wtforms.Form):
     bio = wtforms.TextAreaField(
         _('Bio'),
@@ -56,13 +66,16 @@ class EditProfileForm(wtforms.Form):
         description=_("""You can use
                       <a href="http://daringfireball.net/projects/markdown/basics">
                       Markdown</a> for formatting."""))
-    url = wtforms.StringField(
+    url = WebsiteField(
         _('Website'),
         [wtforms.validators.Optional(),
-         wtforms.validators.URL(message=_("This address contains errors"))])
+         wtforms.validators.URL(message=_("This address contains errors"))],
+        description=_("www.example.com, http://www.example.com or "
+                      "https://www.example.com"))
 
     location = wtforms.StringField(_('Hometown'))
 
+
 class EditAccountForm(wtforms.Form):
     wants_comment_notification = wtforms.BooleanField(
         description=_("Email me when others comment on my media"))
@@ -126,6 +139,7 @@ class ChangeEmailForm(wtforms.Form):
         description=_(
             "Enter your password to prove you own this account."))
 
+
 class MetaDataValidator(object):
     """
     Custom validator which runs form data in a MetaDataForm through a jsonschema
@@ -152,10 +166,12 @@ class MetaDataValidator(object):
             raise wtforms.validators.ValidationError(
                 errors.pop())
 
+
 class MetaDataForm(wtforms.Form):
     identifier = wtforms.StringField(_(u'Identifier'),[MetaDataValidator()])
     value = wtforms.StringField(_(u'Value'))
 
+
 class EditMetaDataForm(wtforms.Form):
     media_metadata = wtforms.FieldList(
         wtforms.FormField(MetaDataForm, ""),