Skip to content

Commit

Permalink
Sync created files when the object is attached to a session
Browse files Browse the repository at this point in the history
  • Loading branch information
amol- committed Mar 15, 2017
1 parent 0963497 commit fdccacb
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions depot/fields/sqlalchemy.py
Expand Up @@ -82,6 +82,11 @@ def _field_set(cls, target, value, oldvalue, initiator):
if session is not None:
session._depot_new = getattr(session, '_depot_new', set())
session._depot_new.update(value.files)
else:
# If object is not attached to a session preserve the files
# until it gets added to one.
target._depot_new = getattr(target, '_depot_new', set())
target._depot_new.update(value.files)

return value

Expand Down Expand Up @@ -148,11 +153,17 @@ def _session_flush(cls, session, flush_context, instances):
session._depot_old = getattr(session, '_depot_old', set())
session._depot_old.update(deleted_files)

@classmethod
def _session_attached(cls, session, instance):
session._depot_new = getattr(session, '_depot_new', set())
session._depot_new.update(getattr(instance, '_depot_new', set()))

@classmethod
def setup(cls):
event.listen(orm.mapper, 'mapper_configured', cls._mapper_configured)
event.listen(Session, 'after_soft_rollback', cls._session_rollback)
event.listen(Session, 'after_commit', cls._session_committed)
event.listen(Session, 'before_attach', cls._session_attached)
event.listen(Session, 'before_flush', cls._session_flush)

_SQLAMutationTracker.setup()
Expand Down

0 comments on commit fdccacb

Please sign in to comment.