Permalink
Browse files

Merge pull request #138 from lmorchard/bug-733610-profile-urls

Bug 733610 profile urls
  • Loading branch information...
2 parents 489cdc4 + 1b86810 commit 638dffe6f0cf91bf4d6d3fb73a4d3746a158a1f3 @ubernostrum ubernostrum committed Mar 23, 2012
Showing with 44 additions and 25 deletions.
  1. +2 −23 apps/devmo/forms.py
  2. +5 −0 apps/devmo/models.py
  3. +37 −0 apps/devmo/tests/test_forms.py
  4. +0 −2 apps/devmo/views.py
View
@@ -8,27 +8,6 @@
from devmo.models import UserProfile
-class PrefixedURLField(forms.URLField):
- """URLField which also enforces a base URL prefix"""
-
- def __init__(self, *args, **kwargs):
- self.prefix = kwargs['prefix']
- del kwargs['prefix']
- return super(PrefixedURLField, self).__init__(*args, **kwargs)
-
- def to_python(self, value):
- if value == self.prefix:
- return ''
- return super(PrefixedURLField, self).to_python(value)
-
- def validate(self, value):
- """Ensure the URL value starts with the given prefix"""
- super(PrefixedURLField, self).validate(value)
- if (self.required or value) and not value.startswith(self.prefix):
- raise ValidationError(_("URL should begin with %s") %
- self.prefix)
-
-
class UserProfileEditForm(forms.ModelForm):
class Meta:
@@ -50,8 +29,8 @@ def __init__(self, *args, **kwargs):
# Dynamically add URLFields for all sites defined in the model.
sites = kwargs.get('sites', UserProfile.website_choices)
for name, meta in sites:
- self.fields['websites_%s' % name] = PrefixedURLField(
- prefix=meta['prefix'], required=False)
+ self.fields['websites_%s' % name] = forms.RegexField(
+ regex=meta['regex'], required=False)
def clean_expertise(self):
"""Enforce expertise as a subset of interests"""
View
@@ -70,22 +70,27 @@ class UserProfile(ModelBase):
('website', dict(
label=_(u'Website'),
prefix='http://',
+ regex='^https?://',
)),
('twitter', dict(
label=_(u'Twitter'),
prefix='http://twitter.com/',
+ regex='^https?://twitter.com/',
)),
('github', dict(
label=_(u'GitHub'),
prefix='http://github.com/',
+ regex='^https?://github.com/',
)),
('stackoverflow', dict(
label=_(u'StackOverflow'),
prefix='http://stackoverflow.com/users/',
+ regex='^https?://stackoverflow.com/users/',
)),
('linkedin', dict(
label=_(u'LinkedIn'),
prefix='http://www.linkedin.com/in/',
+ regex='^https?://www.linkedin.com/in/',
)),
]
@@ -0,0 +1,37 @@
+import logging
+
+from mock import patch
+from nose.tools import assert_equal, eq_, ok_
+from nose.plugins.attrib import attr
+import test_utils
+
+from devmo.forms import UserProfileEditForm
+
+
+class TestUserProfileEditForm(test_utils.TestCase):
+
+ def test_https_profile_urls(self):
+ """bug 733610: Profile URLs should allow https"""
+ protos = (
+ ('http://', True),
+ ('ftp://', False),
+ ('gopher://', False),
+ ('https://', True),
+ )
+ sites = (
+ ('website', 'mozilla.org'),
+ ('twitter', 'twitter.com/lmorchard'),
+ ('github', 'github.com/lmorchard'),
+ ('stackoverflow', 'stackoverflow.com/users/lmorchard'),
+ ('linkedin', 'www.linkedin.com/in/lmorchard'),
+ )
+ for proto, expected_valid in protos:
+ for name, site in sites:
+ url = '%s%s' % (proto, site)
+ form = UserProfileEditForm({
+ "email": "lorchard@mozilla.com",
+ "websites_%s" % name: url
+ })
+ result_valid = form.is_valid()
+ eq_(expected_valid, result_valid)
+
View
@@ -114,8 +114,6 @@ def profile_edit(request, username):
else:
form = UserProfileEditForm(request.POST, request.FILES,
instance=profile)
- logging.debug('form.is_valid(): %s' % form.is_valid())
- logging.debug('form.errors: %s' % form.errors)
if form.is_valid():
profile_new = form.save(commit=False)

0 comments on commit 638dffe

Please sign in to comment.