X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=daklib%2Fdbconn.py;h=c0b7d0e841498097bf2fc6cc32522317f98f1717;hb=0b7a883287f980e1d44e6d4fbf6050d85d1e4963;hp=b63b4a9380d1fed873d0031e25dd1286e08055cc;hpb=5a88b5a5f6632024f2f3735908972090470252e7;p=dak.git diff --git a/daklib/dbconn.py b/daklib/dbconn.py index b63b4a93..c0b7d0e8 100755 --- a/daklib/dbconn.py +++ b/daklib/dbconn.py @@ -981,6 +981,42 @@ class Keyring(object): __all__.append('Keyring') +def get_or_set_keyring(keyring, session=None): + """ + If C{keyring} does not have an entry in the C{keyrings} table yet, create one + and return the new Keyring + If C{keyring} already has an entry, simply return the existing Keyring + + @type keyring: string + @param keyring: the keyring name + + @rtype: Keyring + @return: the Keyring object for this keyring + + """ + privatetrans = False + if session is None: + session = DBConn().session() + privatetrans = True + + try: + obj = session.query(Keyring).filter_by(keyring_name=keyring).first() + + if obj is None: + obj = Keyring(keyring_name=keyring) + session.add(obj) + if privatetrans: + session.commit() + else: + session.flush() + + return obj + finally: + if privatetrans: + session.close() + +__all__.append('get_or_set_keyring') + ################################################################################ class Location(object): @@ -1095,15 +1131,16 @@ def get_or_set_maintainer(name, session=None): __all__.append('get_or_set_maintainer') -def get_maintainer(maintainer_id, session=True): +def get_maintainer(maintainer_id, session=None): """ - Return the name of the maintainer behind C{maintainer_id}. + Return the name of the maintainer behind C{maintainer_id} or None if that + maintainer_id is invalid. @type maintainer_id: int @param maintainer_id: the id of the maintainer - @rtype: string - @return: the name of the maintainer + @rtype: Maintainer + @return: the Maintainer with this C{maintainer_id} """ privatetrans = False @@ -1112,7 +1149,7 @@ def get_maintainer(maintainer_id, session=True): privatetrans = True try: - return session.query(Maintainer).get(maintainer_id).name + return session.query(Maintainer).get(maintainer_id) finally: if privatetrans: session.close()