def __get__(self, item, item_class):
     wrapper = self.get_wrapper()
     compat.set_func_name(wrapper, self.func_name)
     compat.set_func_doc(wrapper, compat.get_func_doc(self.func))
     try:
         # python 2.6
         wrapper.func_dict['cnd'] = self.conditions
     except AttributeError:
         # python 3
         wrapper.__dict__['cnd'] = self.conditions
     return types.MethodType(wrapper, item)  # , item_class)
Exemple #2
0
 def __get__(self, item, item_class):
     wrapper = self.get_wrapper()
     compat.set_func_name(wrapper, self.func_name)
     compat.set_func_doc(wrapper, compat.get_func_doc(self.func))
     try:
         # python 2.6
         wrapper.func_dict['cnd'] = self.conditions
     except AttributeError:
         # python 3
         wrapper.__dict__['cnd'] = self.conditions
     return types.MethodType(wrapper, item)  # , item_class)
Exemple #3
0
def requires_transactional_context(function):
    """
    Use this descriptor to ensure that a function or method is
    run in a transactional context. Required for functions/methods that perform
    database updates.
    """
    def rtc_wrapper(*args, **kwargs):
        if not context._is_txnal:
            raise exceptions.InternalServerError(
                "Not in a transactional context. Use @db.transactional().")
        return function(*args, **kwargs)

    compat.set_func_name(rtc_wrapper, compat.get_func_name(function))
    compat.set_func_doc(rtc_wrapper, compat.get_func_doc(function))
    rtc_wrapper.__module__ = function.__module__
    return rtc_wrapper
 def __get__(self, item, item_class):
     def wm_wrapper(item, context):
         if utils.permsresolver.get_access(item, context.user) == 0:
             raise exceptions.PermissionDenied
         context.response.content_type = self.content_type
         context.response.charset = self.encoding
         if self.max_age is not None:
             context.response.set_expiration(self.max_age)
         return self.execute(item, context)
     compat.set_func_name(wm_wrapper, self.func_name)
     compat.set_func_doc(wm_wrapper, compat.get_func_doc(self.func))
     try:
         # python 2.6
         wm_wrapper.func_dict['cnd'] = self.conditions
     except AttributeError:
         # python 3
         wm_wrapper.__dict__['cnd'] = self.conditions
     return types.MethodType(wm_wrapper, item)  # , item_class)
Exemple #5
0
    def __get__(self, item, item_class):
        def wm_wrapper(item, context):
            if utils.permsresolver.get_access(item, context.user) == 0:
                raise exceptions.PermissionDenied
            context.response.content_type = self.content_type
            context.response.charset = self.encoding
            if self.max_age is not None:
                context.response.set_expiration(self.max_age)
            return self.execute(item, context)

        compat.set_func_name(wm_wrapper, self.func_name)
        compat.set_func_doc(wm_wrapper, compat.get_func_doc(self.func))
        try:
            # python 2.6
            wm_wrapper.func_dict['cnd'] = self.conditions
        except AttributeError:
            # python 3
            wm_wrapper.__dict__['cnd'] = self.conditions
        return types.MethodType(wm_wrapper, item)  # , item_class)
Exemple #6
0
    def transactional_decorator(function):
        """
        This is the descriptor for making a function or a Web method
        transactional.
        """
        def transactional_wrapper(*args):
            # check if running in a replicated site and is master
            rep_mgr = _db.get_replication_manager()
            if rep_mgr is not None and not rep_mgr.is_master():
                # TODO: abort txn in chained calls?
                raise exceptions.DBReadOnly(
                    'Attempted write operation in read-only database.')

            has_txn = False
            txn_flags = 0
            if not context._is_txnal:
                if context._trans is not None:
                    # abort previous txn
                    context._trans.abort()
                    has_txn = True
                    txn_flags = context._trans._flags

                # create new txn handle
                txn = _db.get_transaction(nosync=nosync)
                context._trans = txn
                is_top_level = True
                context._is_txnal = True
            else:
                txn = context._trans
                is_top_level = False

            retries = 0
            sleep_time = _min_sleep_time

            try:
                while retries < txn.txn_max_retries:
                    try:
                        if is_top_level:
                            cargs = copy.deepcopy(args, {'_dup_ext_': False})
                            if retries > 0:
                                time.sleep(sleep_time)
                                sleep_time *= 2
                                if sleep_time > _max_sleep_time:
                                    sleep_time = _max_sleep_time + \
                                                 (retries * _min_sleep_time)
                                txn._retry()
                        else:
                            cargs = args

                        val = function(*cargs)
                        if is_top_level and auto_commit:
                            txn.commit()
                        return val

                    except exceptions.DBRetryTransaction:
                        if is_top_level:
                            retries += 1
                        else:
                            # allow propagation
                            raise

                    except:
                        txn.abort()
                        raise

                # maximum retries exceeded
                raise exceptions.DBDeadlockError

            finally:
                if is_top_level:
                    if has_txn:
                        context._trans = _db.get_transaction(flags=txn_flags)
                    else:
                        context._trans = None
                    context._is_txnal = False

        compat.set_func_name(transactional_wrapper,
                             compat.get_func_name(function))
        compat.set_func_doc(transactional_wrapper,
                            compat.get_func_doc(function))
        transactional_wrapper.__module__ = function.__module__
        return transactional_wrapper