Skip to content

Commit

Permalink
Merge f791ecf into df26299
Browse files Browse the repository at this point in the history
  • Loading branch information
jcushman committed Jun 13, 2014
2 parents df26299 + f791ecf commit 63701e2
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 10 deletions.
6 changes: 3 additions & 3 deletions perma_web/perma/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,15 @@ def get_canonical_guid(self, guid):
if '-' not in guid and (len(guid) == 10 or len(guid) == 11):
return guid

canonical_guid = guid.replace('-', '')

# uppercase and remove non-alphanumerics
canonical_guid = re.sub('[^0-9A-Z]+', '', guid.upper())

# split guid into 4-char chunks, starting from the end
guid_parts = [canonical_guid[max(i - 4, 0):i] for i in
range(len(canonical_guid), 0, -4)]

# stick together parts with '-'
return "-".join(reversed(guid_parts)).upper()
return "-".join(reversed(guid_parts))

def move_to_folder_for_user(self, folder, user):
"""
Expand Down
2 changes: 1 addition & 1 deletion perma_web/perma/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
url(r'^cdx$', 'common.cdx', name='cdx'),

# Our Perma ID catchall
url(r'^%s/?$' % r'(?P<guid>.+)', 'common.single_linky', name='single_linky'),
url(r'^%s/?$' % r'(?P<guid>[^\./]+)', 'common.single_linky', name='single_linky'),

)

Expand Down
8 changes: 2 additions & 6 deletions perma_web/perma/views/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,8 @@ def single_linky(request, guid):

return HttpResponseRedirect(reverse('single_linky', args=[guid]))

# Replace all non-[a-zA-Z0-9] chars with a single hyphen
# We want to translate em-dashes, double hyphens and the like and redirect
if guid != re.sub('[^0-9a-zA-Z]+', '-', guid):
return HttpResponsePermanentRedirect(reverse('single_linky',
args=[re.sub('[^0-9a-zA-Z]+', '-', guid)]))

# Create a canonical version of guid (non-alphanumerics removed, hyphens every 4 characters, uppercase),
# and forward to that if it's different from current guid.
canonical_guid = Link.get_canonical_guid(guid)
if canonical_guid != guid:
return HttpResponsePermanentRedirect(reverse('single_linky', args=[canonical_guid]))
Expand Down

0 comments on commit 63701e2

Please sign in to comment.