Skip to content

Commit

Permalink
Merge pull request #19 from bfirsh/autofix/wrapped2_to3_fix
Browse files Browse the repository at this point in the history
Fix "Prefer `format()` over string interpolation operator" issue
  • Loading branch information
bfirsh committed Dec 15, 2016
2 parents ea8fce3 + c206131 commit 7d75e7e
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/shorturls/__init__.py
Expand Up @@ -13,8 +13,8 @@
try:
mod = import_module(mod_name)
except ImportError, e:
raise ImproperlyConfigured('Could not load converter specified by SHORTURLS_DEFAULT_CONVERTER. Error was: %s' % e)
raise ImproperlyConfigured('Could not load converter specified by SHORTURLS_DEFAULT_CONVERTER. Error was: {0!s}'.format(e))
try:
default_converter = getattr(mod, conv_name)
except AttributeError:
raise ImproperlyConfigured('Could not load converter specified by SHORTURLS_DEFAULT_CONVERTER. %s is not in %s.' % (conv_name, mod))
raise ImproperlyConfigured('Could not load converter specified by SHORTURLS_DEFAULT_CONVERTER. {0!s} is not in {1!s}.'.format(conv_name, mod))
6 changes: 3 additions & 3 deletions src/shorturls/templatetags/shorturl.py
Expand Up @@ -10,7 +10,7 @@ class ShortURL(template.Node):
def parse(cls, parser, token):
parts = token.split_contents()
if len(parts) != 2:
raise template.TemplateSyntaxError("%s takes exactly one argument" % parts[0])
raise template.TemplateSyntaxError("{0!s} takes exactly one argument".format(parts[0]))
return cls(template.Variable(parts[1]))

def __init__(self, obj):
Expand Down Expand Up @@ -43,14 +43,14 @@ def render(self, context):
def get_prefix(self, model):
if not hasattr(self.__class__, '_prefixmap'):
self.__class__._prefixmap = dict((m,p) for p,m in settings.SHORTEN_MODELS.items())
key = '%s.%s' % (model._meta.app_label, model.__class__.__name__.lower())
key = '{0!s}.{1!s}'.format(model._meta.app_label, model.__class__.__name__.lower())
return self.__class__._prefixmap[key]

class RevCanonical(ShortURL):
def render(self, context):
url = super(RevCanonical, self).render(context)
if url:
return mark_safe('<link rev="canonical" href="%s">' % url)
return mark_safe('<link rev="canonical" href="{0!s}">'.format(url))
else:
return ''

Expand Down
4 changes: 2 additions & 2 deletions src/shorturls/tests/models.py
Expand Up @@ -14,7 +14,7 @@ def __unicode__(self):
return self.name

def get_absolute_url(self):
return '/animal/%s/' % self.id
return '/animal/{0!s}/'.format(self.id)

class Vegetable(models.Model):
name = models.CharField(max_length=100)
Expand All @@ -26,7 +26,7 @@ def __unicode__(self):
return self.name

def get_absolute_url(self):
return 'http://example.net/veggies/%s' % self.id
return 'http://example.net/veggies/{0!s}'.format(self.id)

class Mineral(models.Model):
name = models.CharField(max_length=100)
Expand Down
8 changes: 4 additions & 4 deletions src/shorturls/tests/test_views.py
Expand Up @@ -28,7 +28,7 @@ def test_redirect(self):
"""
Test the basic operation of a working redirect.
"""
response = self.client.get('/A%s' % enc(12345))
response = self.client.get('/A{0!s}'.format(enc(12345)))
self.assertEqual(response.status_code, 301)
self.assertEqual(response['Location'], 'http://example.com/animal/12345/')

Expand All @@ -37,15 +37,15 @@ def test_redirect_from_request(self):
Test a relative redirect when the Sites app isn't installed.
"""
settings.SHORTEN_FULL_BASE_URL = None
response = self.client.get('/A%s' % enc(54321), HTTP_HOST='example.org')
response = self.client.get('/A{0!s}'.format(enc(54321)), HTTP_HOST='example.org')
self.assertEqual(response.status_code, 301)
self.assertEqual(response['Location'], 'http://example.org/animal/54321/')

def test_redirect_complete_url(self):
"""
Test a redirect when the object returns a complete URL.
"""
response = self.client.get('/V%s' % enc(785))
response = self.client.get('/V{0!s}'.format(enc(785)))
self.assertEqual(response.status_code, 301)
self.assertEqual(response['Location'], 'http://example.net/veggies/785')

Expand All @@ -55,7 +55,7 @@ def test_bad_short_urls(self):
self.assertEqual(404, self.client.get('/Vssssss').status_code)

def test_model_without_get_absolute_url(self):
self.assertEqual(404, self.client.get('/M%s' % enc(10101)).status_code)
self.assertEqual(404, self.client.get('/M{0!s}'.format(enc(10101))).status_code)

def enc(id):
return base62.from_decimal(id)
2 changes: 1 addition & 1 deletion src/shorturls/urls.py
Expand Up @@ -3,7 +3,7 @@

urlpatterns = patterns('',
url(
regex = '^(?P<prefix>%s)(?P<tiny>\w+)$' % '|'.join(settings.SHORTEN_MODELS.keys()),
regex = '^(?P<prefix>{0!s})(?P<tiny>\w+)$'.format('|'.join(settings.SHORTEN_MODELS.keys())),
view = 'shorturls.views.redirect',
),
)
6 changes: 3 additions & 3 deletions src/shorturls/views.py
Expand Up @@ -35,7 +35,7 @@ def redirect(request, prefix, tiny, converter=default_converter):
try:
url = obj.get_absolute_url()
except AttributeError:
raise Http404("'%s' models don't have a get_absolute_url() method." % model.__name__)
raise Http404("'{0!s}' models don't have a get_absolute_url() method.".format(model.__name__))

# We might have to translate the URL -- the badly-named get_absolute_url
# actually returns a domain-relative URL -- into a fully qualified one.
Expand All @@ -51,10 +51,10 @@ def redirect(request, prefix, tiny, converter=default_converter):

# Next, if the sites app is enabled, redirect to the current site.
elif Site._meta.installed:
base = 'http://%s/' % Site.objects.get_current().domain
base = 'http://{0!s}/'.format(Site.objects.get_current().domain)

# Finally, fall back on the current request.
else:
base = 'http://%s/' % RequestSite(request).domain
base = 'http://{0!s}/'.format(RequestSite(request).domain)

return HttpResponsePermanentRedirect(urlparse.urljoin(base, url))

0 comments on commit 7d75e7e

Please sign in to comment.