Beispiel #1
0
def main(global_config, **settings):
    """This function returns a Pyramid WSGI application."""
    with Configurator(settings=settings) as config:
        config.include("pyramid_chameleon")
        config.include(".routes")
        config.include(".security")
        config.scan()

    current_dir = os.path.dirname(os.path.abspath(__file__))
    db_file = os.path.join(current_dir, "db/billtracker.sqlite3")
    DbSession.global_init(db_file)
    load_starter_data()
    return config.make_wsgi_app()
Beispiel #2
0
def get_bill_by_id(bill_id: int) -> Optional[Bill]:
    session = DbSession.create_session()
    try:
        return session.query(Bill) \
            .filter(Bill.id == bill_id) \
            .first()
    finally:
        session.close()
def get_user_by_id(user_id: int, include_bills=True) -> Optional[User]:
    session = DbSession.create_session()
    try:
        if not include_bills:
            return session.query(User).filter(User.id == user_id).first()
        else:
            return session.query(User) \
                .options(subqueryload(User.bills)) \
                .filter(User.id == user_id) \
                .first()
    finally:
        session.close()
Beispiel #4
0
def load_starter_data():
    print("Loading starter data...")
    session = DbSession.create_session()
    if session.query(Bill).count() > 0:
        session.close()
        print("Data already loaded...")
        return

    session.expire_on_commit = False

    users = add_users(session)
    add_bills(users)

    session.commit()
    session.close()
def add_payment(amount: float, bill_id: int) -> Optional[Bill]:
    session = DbSession.create_session()
    session.expire_on_commit = False

    try:
        bill = session.query(Bill) \
            .filter(Bill.id == bill_id) \
            .first()

        if not bill:
            return None

        bill.paid += amount
        session.commit()

        return bill
    finally:
        session.close()
def init_db():
    db_file = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'db',
                           'bill_tracker.sqlite')
    DbSession.global_init(db_file)
    load_base_data.load_starter_data()