From 9f264942d88c563f9d310c3fea4a554731c1bbbc Mon Sep 17 00:00:00 2001 From: Elrond Date: Sun, 25 Dec 2011 19:09:23 +0100 Subject: [PATCH] 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. --- mediagoblin/db/sql/base.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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() -- 2.25.1