Let SQL objects support .setdefault() and .delete()

Some parts in the code like to use .setdefault(). So make
them happy and provide a minimal version. It ignores the
given default and expects the attribute to already exist.

Other parts use .delete() to delete a complete object. This
version expects the object to live in a session and also
does the final commit.
This commit is contained in:
Elrond 2012-02-19 12:14:58 +01:00
parent 8efcd40558
commit c60bbe07c5

View File

@ -67,6 +67,10 @@ class GMGTableBase(object):
def get(self, key):
return getattr(self, key)
def setdefault(self, key, defaultvalue):
# The key *has* to exist on sql.
return getattr(self, key)
def save(self, validate=True):
assert validate
sess = object_session(self)
@ -75,6 +79,12 @@ class GMGTableBase(object):
sess.add(self)
sess.commit()
def delete(self):
sess = object_session(self)
assert sess is not None, "Not going to delete detached %r" % self
sess.delete(self)
sess.commit()
Base = declarative_base(cls=GMGTableBase)