diff options
author | Elrond <elrond+mediagoblin.org@samba-tng.org> | 2011-12-25 19:09:23 +0100 |
---|---|---|
committer | Elrond <elrond+mediagoblin.org@samba-tng.org> | 2011-12-29 10:41:50 +0100 |
commit | 9f264942d88c563f9d310c3fea4a554731c1bbbc (patch) | |
tree | 1c43d7be127d236e59ebcef1c441e67719551382 /mediagoblin/db/sql/base.py | |
parent | 03c22862322f42a68351e70956e24e512028f0b2 (diff) | |
download | mediagoblin-9f264942d88c563f9d310c3fea4a554731c1bbbc.tar.lz mediagoblin-9f264942d88c563f9d310c3fea4a554731c1bbbc.tar.xz mediagoblin-9f264942d88c563f9d310c3fea4a554731c1bbbc.zip |
Add a .save method on the sql db objects
This is a shortcut to adding the object to a session (if
needed) and giving a commit on the session.
In reality, calling code should probably utilize the
session on its own and call commit in an appropiate place.
Diffstat (limited to 'mediagoblin/db/sql/base.py')
-rw-r--r-- | mediagoblin/db/sql/base.py | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/mediagoblin/db/sql/base.py b/mediagoblin/db/sql/base.py index 1249bace..40140327 100644 --- a/mediagoblin/db/sql/base.py +++ b/mediagoblin/db/sql/base.py @@ -1,4 +1,4 @@ -from sqlalchemy.orm import scoped_session, sessionmaker +from sqlalchemy.orm import scoped_session, sessionmaker, object_session Session = scoped_session(sessionmaker()) @@ -28,3 +28,11 @@ class GMGTableBase(object): def get(self, key): return getattr(self, key) + + def save(self, validate = True): + assert validate + sess = object_session(self) + if sess is None: + sess = Session() + sess.add(self) + sess.commit() |