Exemplo n.º 1
0
class CookieClientIdentification(ext.Adapter):
    ext.context(ext.IRequest)
    ext.implements(IClientIdentification)

    def __init__(self, request):
        self.request = request

    def identification(self):
        """ cookie expiration will do on server. BE SURE A OLD COOKIE
            WILL BE OVERIDE AFTER HIS LIVE TIME!
        """
        return self.request.cookies.get(COOKIE_SESSION_KEY)

    def apply(self, secure=False):
        """ the secure flag means that this
            cookie can be set on http and https.
        """
        cookie = self.genereate_id()
        self.request.cookies[COOKIE_SESSION_KEY] = cookie
        self.request.response.set_cookie(key=COOKIE_SESSION_KEY,
                                         value=cookie,
                                         secure=secure)
        return cookie

    def genereate_id(self):
        letters = string.ascii_letters + string.digits
        return ''.join(
            random.choice(letters) for i in range(COOKIE_SESSION_LENGHT))
Exemplo n.º 2
0
class DefaultHTTPExceptionHandler(ext.Adapter):
    """ This is a default adapter that do
        nothing else as return the same error.
    """
    ext.context(HTTPError)

    def __call__(self):
        transaction.abort()
        return self.context
class DefaultHTTPExceptionHandler(DefaultExceptionHandler):
    """ This is a default adapter for HTTP errors. The client receive
        as json object with the a http error code.
    """
    ext.context(HTTPError)

    def __call__(self, request):
        self.request = request
        transaction.abort()
        self.errorresponse(str(self.context), self.context.code)
Exemplo n.º 4
0
class DefaultExceptionHandler(ext.Adapter):
    """ This adapter is for all exceptions types.
        It recreate the exceptions and send it as
        InternalServerError.

        IN FUTURE WE SHOULD REMOVE THE ERROR MESSAGE FOR THE WEBUSER !!
    """

    ext.context(Exception)

    def __call__(self):
        transaction.abort()
        print(traceback.format_exc())
        return HTTPInternalServerError(str(self.context))
class DefaultExceptionHandler(ext.Adapter):
    """ This adapter is for all exceptions types.
        The handler wrap the error message to a json
        response object and send it back to client.

        IN FUTURE WE SHOULD REMOVE THE ERROR MESSAGE FOR THE WEBUSER !!
    """

    ext.context(Exception)

    def __call__(self, request):
        self.request = request
        transaction.abort()
        print(traceback.format_exc())
        self.errorresponse(str(self.context), 500)

    def errorresponse(self, message, code):
        self.request.response.status_code = code
        self.request.response.write(
            json.dumps(dict(success=False,
                            message=message,
                            total=0,
                            data=list()),
                       indent=' ' * 4))
Exemplo n.º 6
0
class RamSession(ext.Adapter):
    """ simple session thats store data unencrypted in ram. After shutdown
        the server all data will lost.
        For a persistent session class you properly should
        inherit from this class.
    """
    ext.implements(ISession)
    ext.context(ext.IRequest)

    user_session_data_cls = UserSessionData

    def __init__(self, request):
        self.request = request

    def __setitem__(self, key, value):
        client = IClientIdentification(self.request)
        identification = client.identification()
        if identification is None or identification not in self.store():
            identification = client.apply()
        data = self.store().setdefault(identification,
                                       self.user_session_data_cls())
        data[key] = self.encrypt(value)
        data.reset_lastchanged()

    def __getitem__(self, key):
        self.refresh()
        identification = IClientIdentification(self.request).identification()
        if identification is None or identification not in self.store():
            raise KeyError('user has no identification or data')
        return self.decrypt(self.store()[identification][key])

    def __delitem__(self, key):
        identification = IClientIdentification(self.request).identification()
        if identification is None or identification not in self.store():
            raise KeyError('user has no identification or data')
        del self.store()[identification]

    def __contains__(self, key):
        identification = IClientIdentification(self.request).identification()
        if identification is None:
            return False
        if identification not in self.store():
            return False
        return key in self.store()[identification]

    def set_expiration_time(self, time):
        self.store().expiration = time

    def get_expiration_time(self):
        return self.store().expiration

    def store(self):
        """ return a store in form of a dict
        """
        return ram

    def decrypt(self, value):
        """ this function do nothing but
            can easily overridden in a subclass
        """
        return value

    def encrypt(self, value):
        """ this function do nothing but
            can easily overridden in a subclass
        """
        return value

    def refresh(self):
        removes = list()
        for key, data in self.store().items():
            if data.lastchanged + self.get_expiration_time() < time():
                removes.append(key)
        for key in removes:
            del self.store()[key]