From: Elrond Date: Sun, 25 Dec 2011 18:09:23 +0000 (+0100) Subject: Add a .save method on the sql db objects X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=9f264942d88c563f9d310c3fea4a554731c1bbbc;p=mediagoblin.git 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. --- 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()