Ajax API class

 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
from django.conf.urls.defaults import url
from django.utils.decorators import wraps

def func_to_view(f):
    @wraps(f)
    def _dec(request, *a, **kw):
        kwargs = dict(kw)
        for k, v in request.REQUEST.items():
            kwargs[str(k)] = v
        return f(request, *a, **kwargs)
    return _dec

class AjaxAPI(object):
    def url_patterns(self, prefix=''):
        attrs = ((name, getattr(self, name)) for name in dir(self)
                 if not name.startswith('__'))
        methods = ((name, method) for (name, method) in attrs if callable(method))

        res = []
        for name, method in methods:
            exported = getattr(method, 'exported', False)
            if not exported: continue
            if isinstance(exported, basestring):
                export_name = exported
            else:
                export_name = name # Default to exporting by function name
            res.append(url('^%s%s/$' % (prefix, export_name),
                           func_to_view(method)))
        return res

    @staticmethod
    def export(f, name=True):
        f.exported = name
        return f

More like this

  1. Effective content caching for mass-load site using redirect feature by nnseva 7 months ago
  2. RFC: Shim to allow view classes rather than functions by peterbraden 2 years, 11 months ago
  3. URL models by diverman 2 years, 4 months ago
  4. Decorating class-based views by lqc 1 week, 2 days ago
  5. Management command decorator by eternicode 1 year, 5 months ago

Comments

(Forgotten your password?)