""" Helper utility for passing control to different views based on the HTTP method. Construct instances of this class with a dictionary mapping method names (or sequence of method names) to the view function that will handle them. The special and optional '__default__' key is used to handle all otherwise unmentioned methods. Methods not mentioned in the dictionary are implicitly forbidden if there is no '__default__' key. For example: dispatch_dict = { ('POST', 'PUT') : new_data_handler, 'GET': display_only_handler } urlconf = patterns('', url('/my/pattern/', MethodDispatcher(dispatch_dict)) ) Each view function in the dictionary will be passed the same set of arguments, so be prepared to use *args and/or **kwargs if you need to make the function signatures uniform. """ from django import http class MethodDispatcher(object): def __init__(self, method_map): self.methods = {} self.default = None for key, value in method_map.items(): if key == '__default__': self.default = value elif isinstance(key, basestring): self.methods[key] = value else: for elt in key: self.methods[elt] = value def __call__(self, request, *args, **kwargs): view_func = self.methods.get(request.method, self.default) if view_func: return view_func(request, *args, **kwargs) return http.HttpResponseNotAllowed(self.methods.keys())