views:

20

answers:

0

I was trying to get the last value "before_insert" a new item but i didn't find out, so i change of strategy and i saved the last value of this method, but the problem is that when i do other commits my old MapperExtension attribute (self.last_value) get the last value and i dislike it, even when I close and open a new Session (discovered in my tests)

This is my code:

class _ValueExtension(MapperExtension):
    """Set new value for SkuDynamic and SKuUnique"""
    def __init__(self):
        self.last_value = {} # <<--- THE PROBLEM
        self.first_value = FIRST_VALUE


    def before_insert(self, mapper, connection, instance):
        session = object_session(instance)
    cls = instance.__class__

    last_sku_value = self.get_last_value(session, cls, instance)


        if not last_sku_value:
        instance.value = self.first_value
    else:
        instance.value = next_value(last_sku_value)

    self.last_value[self.last_value_key] = instance.value
        self.set_values(instance)

    #def after_insert(self, mapper, connection, instance):
    #    self.__init__()   #<--- IF I DO THIS I DON'T HAVE THE LAST VALUE IN THE SAME FLUSH

    #def after_update(self, mapper, connection, instance):
    #    self.__init__()


    def get_last_value(self, session, cls, instance):
        """This have to return just one value
    and set self.last_value_key is nedded to update elements in same session
        Execute The Query or check self.last_value
    """
        raise NotImplementedError, 'implement this function'

    def set_values(self, instance):
        """Use if necesary"""
    pass

Thanks