Ticket #994: issue_994v2.patch

File issue_994v2.patch, 4.1 KB (added by Ben Sturmfels, 9 years ago)
  • mediagoblin/edit/forms.py

    From c8ddfda0b0af15fa98c38c5b20efee3b3c6b3464 Mon Sep 17 00:00:00 2001
    From: Ben Sturmfels <ben@sturm.com.au>
    Date: Sun, 23 Aug 2015 22:17:13 +1000
    Subject: [PATCH 1/2] trac#994: Don't require users to type the website URL
     scheme when updating profile.
    
    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 | 16 +++++++++++++++-
     1 file changed, 15 insertions(+), 1 deletion(-)
    
    diff --git a/mediagoblin/edit/forms.py b/mediagoblin/edit/forms.py
    index cf5056c..b9eef04 100644
    a b from mediagoblin.tools.metadata import DEFAULT_SCHEMA, DEFAULT_CHECKER  
    2424from mediagoblin.auth.tools import normalize_user_or_email_field
    2525
    2626
     27class WebsiteField(wtforms.StringField):
     28    """A field that expects a website URL but adds http:// if not provided."""
     29    def process_formdata(self, valuelist):
     30        data = valuelist[0]
     31        if not (data.startswith(u'http://') or data.startswith(u'https://')):
     32            data = u'http://' + data
     33        self.data = data
     34
     35
    2736class EditForm(wtforms.Form):
    2837    title = wtforms.StringField(
    2938        _('Title'),
    class EditForm(wtforms.Form):  
    4958        [wtforms.validators.Optional(),],
    5059        choices=licenses_as_choices())
    5160
     61
    5262class EditProfileForm(wtforms.Form):
    5363    bio = wtforms.TextAreaField(
    5464        _('Bio'),
    class EditProfileForm(wtforms.Form):  
    5666        description=_("""You can use
    5767                      <a href="http://daringfireball.net/projects/markdown/basics">
    5868                      Markdown</a> for formatting."""))
    59     url = wtforms.StringField(
     69    url = WebsiteField(
    6070        _('Website'),
    6171        [wtforms.validators.Optional(),
    6272         wtforms.validators.URL(message=_("This address contains errors"))])
    6373
    6474    location = wtforms.StringField(_('Hometown'))
    6575
     76
    6677class EditAccountForm(wtforms.Form):
    6778    wants_comment_notification = wtforms.BooleanField(
    6879        description=_("Email me when others comment on my media"))
    class ChangeEmailForm(wtforms.Form):  
    126137        description=_(
    127138            "Enter your password to prove you own this account."))
    128139
     140
    129141class MetaDataValidator(object):
    130142    """
    131143    Custom validator which runs form data in a MetaDataForm through a jsonschema
    class MetaDataValidator(object):  
    152164            raise wtforms.validators.ValidationError(
    153165                errors.pop())
    154166
     167
    155168class MetaDataForm(wtforms.Form):
    156169    identifier = wtforms.StringField(_(u'Identifier'),[MetaDataValidator()])
    157170    value = wtforms.StringField(_(u'Value'))
    158171
     172
    159173class EditMetaDataForm(wtforms.Form):
    160174    media_metadata = wtforms.FieldList(
    161175        wtforms.FormField(MetaDataForm, ""),
  • mediagoblin/edit/forms.py

    -- 
    1.9.1
    
    
    From b5d3f9a444ac30bd151c662a0beb96a41d83ed9b Mon Sep 17 00:00:00 2001
    From: Ben Sturmfels <ben@sturm.com.au>
    Date: Tue, 25 Aug 2015 01:00:28 +1000
    Subject: [PATCH 2/2] trac#994: Simplify the "startswith" test and add website
     field description.
    
    ---
     mediagoblin/edit/forms.py | 5 +++--
     1 file changed, 3 insertions(+), 2 deletions(-)
    
    diff --git a/mediagoblin/edit/forms.py b/mediagoblin/edit/forms.py
    index b9eef04..cbc176c 100644
    a b class WebsiteField(wtforms.StringField):  
    2828    """A field that expects a website URL but adds http:// if not provided."""
    2929    def process_formdata(self, valuelist):
    3030        data = valuelist[0]
    31         if not (data.startswith(u'http://') or data.startswith(u'https://')):
     31        if not data.startswith((u'http://', u'https://')):
    3232            data = u'http://' + data
    3333        self.data = data
    3434
    class EditProfileForm(wtforms.Form):  
    6969    url = WebsiteField(
    7070        _('Website'),
    7171        [wtforms.validators.Optional(),
    72          wtforms.validators.URL(message=_("This address contains errors"))])
     72         wtforms.validators.URL(message=_("This address contains errors"))],
     73        description=_("www.example.com"))
    7374
    7475    location = wtforms.StringField(_('Hometown'))
    7576