Accept Header Middleware

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import new

def accepts( self, mime ):
    return mime in self.accepted_types

class AcceptMiddleware(object):
    def process_request(self, request):
        acc = [a.split(';')[0] for a in request.META['HTTP_ACCEPT'].split(',')]
        setattr(request, 'accepted_types', acc )
        request.accepts = new.instancemethod(accepts, request, request.__class__)
        return None

More like this

  1. Other approach of making middleware (by decorators) by diverman 11 months ago
  2. Improved Accept header middleware by ludvig.ericson 3 years, 4 months ago
  3. XhtmlDegraderMiddleware by dmh 4 years, 6 months ago
  4. Improved Accept middleware with webkit workaround by raven_nevermore 1 year, 2 months ago
  5. AjaxCheckMiddleware by kylefox 4 years ago

Comments

ludvig.ericson (on September 10, 2008):

I'd say the new.instancemethod trickery is indeed uncalled for:

request.accepts = lambda type: type in acc

Not to mention the list comprehension. :-)

#

(Forgotten your password?)