Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

Commit

Permalink
Bug 761826: Handle legacy MindTouch file URLs.
Browse files Browse the repository at this point in the history
This is simply a URL pattern and corresponding view which responds to
the MindTouch file URL scheme, looks up the correspondong kuma
Attachment, and issues a redirect.

It is relatively future-proof for the case where kuma's own file URL
patterns change (very likely), and is testable independent of having a
fully-migrated set of files (very handy). Test cases are actual
MindTouch URLs, with corresponding dummy Attachments created so kuma
has something to redirect to.
  • Loading branch information
ubernostrum committed Jun 27, 2012
1 parent c7adfd2 commit 4c748a3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
49 changes: 47 additions & 2 deletions apps/wiki/tests/test_views.py
Expand Up @@ -5,12 +5,14 @@
import json
import base64
import hashlib
import os
import time

from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.core.cache import cache
from django.core.files.base import ContentFile
from django.db.models import Q

import mock
Expand All @@ -29,7 +31,8 @@
from . import TestCaseBase

import wiki.content
from wiki.models import VersionMetadata, Document, Revision
from wiki.models import VersionMetadata, Document, Revision, \
Attachment, AttachmentRevision
from wiki.tests import (doc_rev, document, new_document_data, revision,
normalize_html, create_template_test_users)
from wiki.views import _version_groups, DOCUMENT_LAST_MODIFIED_CACHE_KEY_TMPL
Expand Down Expand Up @@ -1601,4 +1604,46 @@ def test_document_redirects(self):
if v['title'] == d['title']:
found = True
break
eq_(True, found)
eq_(True, found)


class AttachmentTests(TestCaseBase):
fixtures = ['test_users.json']

def test_legacy_redirect(self):
test_user = User.objects.get(username='testuser2')
test_file_content = 'Meh meh I am a test file.'
test_files = (
{'file_id': 97, 'filename': 'Canvas_rect.png',
'title': 'Canvas rect', 'slug': 'canvas-rect'},
{'file_id': 107, 'filename': 'Canvas_smiley.png',
'title': 'Canvas smiley', 'slug': 'canvas-smiley'},
{'file_id': 86, 'filename': 'Canvas_lineTo.png',
'title': 'Canvas lineTo', 'slug': 'canvas-lineto'},
{'file_id': 55, 'filename': 'Canvas_arc.png',
'title': 'Canvas arc', 'slug': 'canvas-arc'},
)
for f in test_files:
a = Attachment(title=f['title'], slug=f['slug'],
mindtouch_attachment_id=f['file_id'])
a.save()
now = datetime.datetime.now()
r = AttachmentRevision(
attachment=a,
mime_type='text/plain',
title=f['title'],
slug=f['slug'],
description = '',
created=now,
is_approved=True)
r.creator = test_user
r.file.save(f['filename'], ContentFile(test_file_content))
r.make_current()
mindtouch_url = reverse('wiki.mindtouch_file_redirect',
args=(),
kwargs={'file_id': f['file_id'],
'filename': f['filename']})
resp = self.client.get(mindtouch_url)
eq_(301, resp.status_code)
ok_(a.get_absolute_url() in resp['Location'])

6 changes: 6 additions & 0 deletions apps/wiki/views.py
Expand Up @@ -1697,3 +1697,9 @@ def attachment_detail(request, attachment_id, filename):
resp["Last-Modified"] = rev.created
resp["Content-Length"] = rev.size
return resp


def mindtouch_file_redirect(request, file_id, filename):
"""Redirect an old MindTouch file URL to a new kuma file URL."""
attachment = get_object_or_404(Attachment, mindtouch_attachment_id=file_id)
return HttpResponsePermanentRedirect(attachment.get_absolute_url())
3 changes: 3 additions & 0 deletions urls.py
Expand Up @@ -62,6 +62,9 @@
{'document_root': settings.HUMANSTXT_ROOT, 'path': 'humans.txt'}),

# Legacy MindTouch redirects.
url(r'^@api/deki/files/(?P<file_id>\d+)/=(?P<filename>.+)$',
'wiki.views.mindtouch_file_redirect',
name='wiki.mindtouch_file_redirect'),
(r'^(?P<path>.*)$', 'wiki.views.mindtouch_to_kuma_redirect'),
)

Expand Down

0 comments on commit 4c748a3

Please sign in to comment.