Source code for djangorpc.responses
from __future__ import unicode_literals
from Cookie import SimpleCookie
[docs]class RpcResponse(dict):
"""
Base class for rpc response clasess
"""
pass
[docs]class Error(RpcResponse):
"""
Simple responses. Just for pretty code and some kind of "protocol". Example::
return Error('Something happened', code=error_code, traceback=traceback)
"""
def __init__(self, text, **kwargs):
super(Error, self).__init__(error=text, **kwargs)
[docs]class Msg(RpcResponse):
"""
Simple responses. Just for pretty code and some kind of "protocol". Example::
return Msg('Object saved!')
"""
def __init__(self, text, **kwargs):
super(Msg, self).__init__(msg=text, **kwargs)
[docs]class RpcHttpResponse(RpcResponse):
"""
This is vrapper for method's reponse, which allow save some modification of HTTP response.
For example set COOKIES. This should be flexible and useful for in future.
"""
def __init__(self, *args, **kwargs):
super(RpcHttpResponse, self).__init__(*args, **kwargs)
self.cookies = SimpleCookie()
[docs] def set_cookie(self, key, value='', max_age=None, expires=None, path='/',
domain=None, secure=False):
"""
Set cookie.
"""
self.cookies[key] = value
if max_age is not None:
self.cookies[key]['max-age'] = max_age
if expires is not None:
self.cookies[key]['expires'] = expires
if path is not None:
self.cookies[key]['path'] = path
if domain is not None:
self.cookies[key]['domain'] = domain
if secure:
self.cookies[key]['secure'] = True