Login

RandomFileExtensionMiddleware

Author:
jezdez
Posted:
June 2, 2008
Language:
Python
Version:
.96
Score:
3 (after 3 ratings)

If you feel nostalgic for the old days of crufty URLs, put this middleware somewhere in your Django app and add the first entry in settings.MIDDLEWARE_CLASSES as shown below. Keep in mind that you need to replace 'path.to' with the correct Python path to the middleware.

MIDDLEWARE_CLASSES = (
    'path.to.RandomFileExtensionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
)

Note: Although this works it's still a joke.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import random
from django.http import HttpResponseRedirect
from django.conf import settings

extensions = (
    ".do",
    ".pl",
    ".py",
    ".asp",
    ".cfm",
    ".cgi",
    ".dll",
    ".htm",
    ".jsp",
    ".xml",
    ".exe",
    ".php",
    ".aspx",
    ".html",
    ".php3",
    ".php4",
    ".shtml",
    ".action",
)

indexes = (
    "/index",
    "/main",
)

class RandomFileExtensionMiddleware(object):
    """
    A middleware that replaces the request path automatically with a random
    file extension to make you feel nostalgic for old crufty URLs. This
    will respect settings.APPEND_SLASH when it converts the request path back
    to normal.
    """
    def _is_valid_extension(self, path):
        for i, ext in enumerate(extensions):
            if path.endswith(ext):
                return True
        return False
    
    def _is_valid_index(self, path):
        for i, index in enumerate(indexes):
            if path.startswith(index):
                return True
        return False
    
    def process_request(self, request):
        if request.path == "/":
            request.path = random.choice(indexes) + random.choice(extensions)
            return HttpResponseRedirect(request.build_absolute_uri())

        elif request.path.endswith("/"):
            request.path = request.path[:-1] + random.choice(extensions)
            return HttpResponseRedirect(request.build_absolute_uri())

        elif self._is_valid_extension(request.path):
            if self._is_valid_index(request.path):
                request.path = "/"
            else:
                path = request.path.split("/")
                path[-1] = "".join(path[-1].split(".")[:-1])
                if settings.APPEND_SLASH:
                    path[-1] = path[-1] + '/'
                request.path = "/".join(path)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.