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. XhtmlDegraderMiddleware by dmh 6 years, 8 months ago
  2. Render Decorator by dahool 4 years, 7 months ago
  3. Other approach of making middleware (by decorators) by diverman 3 years, 1 month ago
  4. Improved Accept header middleware by ludvig.ericson 5 years, 7 months ago
  5. Decorator that limits request methods by schinckel 4 years, 9 months 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?)