Example #1
0
def before_request():
    g.user = current_user # GLOBAL var to simplify access, notably in templates (global current_user is set by Flask)
    if g.user.is_authenticated:
        g.user.last_connection = datetime.utcnow()
        db.session.add(g.user)
        db.session.commit()
    #cookies initialization
        if 'spends_page' in session:
            spends_page = session['spends_page']
        else:
            session['spends_page'] = 'depenses' #default spendings page
Example #2
0
def addBill(s_type, s_label, s_total, s_payer_id, s_user_ids):
    """
        create a Spending in the database.
          1) create the Spending model and fill its attributes except parts
          2) estimate parts and add them to our Spending
          3) adjust balance for each User with this parts
          4) until no errors: add all of this in the database
    """
    try:
        bill = Spending()
        bill.timestamp = datetime.utcnow()
        bill.s_type = s_type
        bill.label = unicode(label, 'utf-8')
        bill.total = total
        bill.payer_id = payer_id
        db.session.add(bill)

        db.session.query(User).get(payer_id).given_money += bill.total
     
        
        tmp_parts = bill.computeParts(db.session, len(s_user_ids))
        user_parts = []
        for idx, i in enumerate(tmp_parts):
            db.session.add(
                Spending.Part(
                    spending=bill,
                    total=i, # == tmp_parts[idx],
                    user_id=s_user_ids[idx]
                )
            )
            user_parts.append([s_user_ids[idx], i])
        

        for user_id, user_bill in user_parts:
            db.session.query(User).get(user_id).borrowed_money += user_bill

        db.session.commit()
        return 1
    except:
        db.session.rollback()
        print exc_info()
        return 0