Generic AJAX app

 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
# myProject\ajax\__init__.py
from dispatcher import dispatcher

# myProject\ajax\dispatcher.py
from django import http
import re

class AlreadyRegistered(Exception):
    pass

class NotCallable(Exception):
    pass

class Dispatcher(object):

    def __init__(self):
        self._funcs = {} # Reference name -> Ajax Function

    def register(self, name, func, force_override=False):
        if not callable(func):
            raise NotCallable('The supplied AJAX function %s is not a function (it is not callable).' % func.__name__)

        if name in self._funcs and not force_override:
            raise AlreadyRegistered('The AJAX function name %s is already registered' % name)

        # Instantiate the admin class to save in the registry
        self._funcs[name] = func

    def has_permission(self, request):
        """
        Returns True if the given HttpRequest has permission to view
        *at least one* page in the admin site.
        """
        return request.user.is_authenticated() and request.user.is_staff

    def urls(self, request, url):
        """
        Handles main URL routing for the admin app.

        `url` is the remainder of the URL -- e.g. 'comments/comment/'.
        """
        if request.method == 'GET' and not request.path.endswith('/'):
            return http.HttpResponseRedirect(request.path + '/')

        # Figure out the admin base URL path and stash it for later use
        self.root_path = re.sub(re.escape(url) + '$', '', request.path)

        url = url.rstrip('/') # Trim trailing slash, if it exists.

        # Check permission to continue or display login form.
        if not self.has_permission(request):
            return http.HttpResponseForbidden('You do not have permission to view the requested information.')

        if url == '':
            return http.HttpResponseForbidden('An AJAX function must be specified.')
        else:
            return self._funcs[url](request)

        raise http.Http404('The requested admin page does not exist.')

dispatcher = Dispatcher()

More like this

  1. Exists Filter OneToOneField in Admin by davidvaz 1 year, 6 months ago
  2. A custom 500 handler which is AJAX-aware by mallipeddi 5 years, 3 months ago
  3. autocompleter with database query by bbolli 5 years, 10 months ago
  4. GeoDjango maps in admin TabularInlines by alanB 2 years, 7 months ago
  5. OracleAuthBackend by nosrednakram 3 years, 9 months ago

Comments

dougal (on June 23, 2009):

Don't add things to django.contrib or 'fake' things being in django.contrib either.

Infact, just lease django.contrib well alone! please.

#

Rupe (on July 4, 2009):

It doesn't need to go in django.contrib. I wanted to use it in some widgets I created for the Admin app and there are certain modifications where you can't avoid modifying an Admin app file. So I figured it was better to put everything in one spot.

#

(Forgotten your password?)