From 1e2e253e33600a47d8a421febf322e6c2d2bee31 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Mar 18 2014 14:39:10 +0000 Subject: Add the skeleton of the backend library for progit --- diff --git a/progit/lib.py b/progit/lib.py new file mode 100644 index 0000000..0fa53f1 --- /dev/null +++ b/progit/lib.py @@ -0,0 +1,51 @@ +#-*- coding: utf-8 -*- + +""" + (c) 2014 - Copyright Red Hat Inc + + Authors: + Pierre-Yves Chibon + +""" + + +import sqlalchemy +from datetime import timedelta +from sqlalchemy.orm import sessionmaker +from sqlalchemy.orm import scoped_session +from sqlalchemy.orm.exc import NoResultFound +from sqlalchemy.exc import SQLAlchemyError + +from progit import model + + +def create_session(db_url, debug=False, pool_recycle=3600): + """ Create the Session object to use to query the database. + + :arg db_url: URL used to connect to the database. The URL contains + information with regards to the database engine, the host to connect + to, the user and password and the database name. + ie: ://:@/ + :kwarg debug: a boolean specifying wether we should have the verbose + output of sqlalchemy or not. + :return a Session that can be used to query the database. + + """ + engine = sqlalchemy.create_engine( + db_url, echo=debug, pool_recycle=pool_recycle) + scopedsession = scoped_session(sessionmaker(bind=engine)) + return scopedsession + + +def get_user_project(session, username): + ''' Retrieve the list of projects managed by a user. + + ''' + + query = session.query( + model.Project + ).filter( + model.Project.user == username + ) + + return query.all()