Show
Ignore:
Timestamp:
09/30/09 14:58:16 (2 years ago)
Author:
ged
Message:

- Provide our own Session.mapper equivalent to avoid SQLAlchemy 0.5.5+

deprecation warning. This mapper autosave object instances on init unless
save_on_init=False is passed as a mapper argument (closes #92).

- simplified a few tests in test_options.py

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • elixir/trunk/elixir/entity.py

    r466 r480  
    3434 
    3535__doc_all__ = ['Entity', 'EntityMeta'] 
     36 
     37 
     38def session_mapper_factory(scoped_session): 
     39    def session_mapper(cls, *args, **kwargs): 
     40        if kwargs.pop('save_on_init', True): 
     41            old_init = cls.__init__ 
     42            def __init__(self, *args, **kwargs): 
     43                old_init(self, *args, **kwargs) 
     44                scoped_session.add(self) 
     45            cls.__init__ = __init__ 
     46        cls.query = scoped_session.query_property() 
     47        return mapper(cls, *args, **kwargs) 
     48    return session_mapper 
    3649 
    3750 
     
    441454            self.entity.mapper = mapper(self.entity, *args, **kwargs) 
    442455        elif isinstance(self.session, ScopedSession): 
    443             self.entity.mapper = self.session.mapper(self.entity, 
    444                                                     *args, **kwargs) 
     456            session_mapper = session_mapper_factory(self.session) 
     457            self.entity.mapper = session_mapper(self.entity, *args, **kwargs) 
    445458        else: 
    446459            raise Exception("Failed to map entity '%s' with its table or "