adding @require_mime decorator and @require_extended as well
This commit is contained in:
Родитель
e623fe3e5a
Коммит
d9e7042f1a
|
@ -1,5 +1,5 @@
|
|||
from piston.handler import BaseHandler, AnonymousBaseHandler
|
||||
from piston.utils import rc
|
||||
from piston.utils import rc, require_mime, require_extended
|
||||
|
||||
from blogserver.blog.models import Blogpost
|
||||
|
||||
|
@ -16,6 +16,7 @@ class BlogpostHandler(BaseHandler):
|
|||
def content_length(self, blogpost):
|
||||
return len(blogpost.content)
|
||||
|
||||
@require_extended
|
||||
def create(self, request):
|
||||
if request.content_type:
|
||||
print request.data
|
||||
|
|
|
@ -140,6 +140,9 @@ class Mimer(object):
|
|||
if ctype in mimes:
|
||||
return loadee
|
||||
|
||||
def content_type(self):
|
||||
return self.request.META.get('CONTENT_TYPE')
|
||||
|
||||
def translate(self):
|
||||
"""
|
||||
Will look at the `Content-type` sent by the client, and maybe
|
||||
|
@ -152,9 +155,9 @@ class Mimer(object):
|
|||
It will also set `request.mimetype` so the handler has an easy
|
||||
way to tell what's going on. `request.mimetype` will always be
|
||||
None for multipart form data (what your browser sends.)
|
||||
"""
|
||||
ctype = self.request.META.get('CONTENT_TYPE')
|
||||
|
||||
"""
|
||||
ctype = self.content_type()
|
||||
|
||||
if not self.is_multipart() and ctype:
|
||||
loadee = self.loader_for_type(ctype)
|
||||
|
||||
|
@ -181,5 +184,32 @@ class Mimer(object):
|
|||
return cls.TYPES.pop(loadee)
|
||||
|
||||
def translate_mime(request):
|
||||
request = Mimer(request).translate()
|
||||
request = Mimer(request).translate()
|
||||
|
||||
def require_mime(*mimes):
|
||||
"""
|
||||
Decorator requiring a certain mimetype. There's a nifty
|
||||
helper called `require_extended` below which requires everything
|
||||
we support except for post-data via form.
|
||||
"""
|
||||
@decorator
|
||||
def wrap(f, self, request, *args, **kwargs):
|
||||
m = Mimer(request)
|
||||
realmimes = set()
|
||||
|
||||
rewrite = { 'json': 'application/json',
|
||||
'yaml': 'application/x-yaml',
|
||||
'xml': 'text/xml',
|
||||
'pickle': 'application/python-pickle' }
|
||||
|
||||
for idx, mime in enumerate(mimes):
|
||||
realmimes.add(rewrite.get(mime, mime))
|
||||
|
||||
if not m.content_type() in realmimes:
|
||||
return rc.BAD_REQUEST
|
||||
|
||||
return f(self, request, *args, **kwargs)
|
||||
return wrap
|
||||
|
||||
require_extended = require_mime('json', 'yaml', 'xml', 'pickle')
|
||||
|
Загрузка…
Ссылка в новой задаче