Example #1
0
def getUser(email):
    user = User.query.filter_by(email=email).first()
    form = EditUserForm(g.user.email)
    if user is None:
        flash('Utilisateur %s introuvable' % email)
        users = User.query.order_by('last_connection desc').all()
        return render_template('getUsers.html', users=users, app_name=app_name)
    else:
        if form.validate_on_submit():
            try:
                g.user.firstname = form.firstname.data
                g.user.email = form.email.data
                g.user.timezone = form.timezone.data
                if form.new_password.data != '':
                    g.user.set_password(form.new_password.data)
                db.session.add(g.user)
                db.session.commit()
                flash(u'tes modifs\' sont bien enregistrées')
            except:
                db.session.rollback()
                flash(u'ERREUR : impossible d\'enregistrer tes modifs !')
                LOGGER.p_log(u'impossible d\'enregistrer les modifs', exception=exc_info())  
        else:
            for errors in form.errors.values():
                for error in errors:
                    flash(error)
                    print error
            form.firstname.data = g.user.firstname
            form.email.data = g.user.email
            form.timezone.data = g.user.timezone
        return render_template('getUser.html', app_name=app_name, user=user, form=form)
Example #2
0
def addBill(s_date, 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_date = s_date
        bill.s_type = s_type
        bill.label = s_label
        bill.total = s_total
        bill.payer_id = s_payer_id
        db.session.add(bill)

        # db.session.query(User).get(s_payer_id).given_money += float(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()
        LOGGER.p_log(u'impossible d\'ajouter la dépense', exception=exc_info())     
        return 0
Example #3
0
def delSpending(id):
    bill = Spending.query.get(int(id))

    if bill is None:
        flash(u'Dépense (id = %s) introuvable' % id)
        return redirect(url_for(
            'comptes',
            app_name=app_name,
            spends_page='depenses'
        ))

    else:
        print "on supprime %s" % bill.label
        try:
            # delete associated parts and reset users' balances
            parts = db.session.query(Spending.Part).filter_by(
                    spending_id=int(id)
                ).all()
            db.session.delete(bill)
            for part in parts:
                u_tmp = db.session.query(User).filter_by(id=part.user_id).first()
                # u_tmp.borrowed_money -= part.total
                LOGGER.p_log(u"%s récupère %s € !" % (u_tmp.getName(), part.total))
                db.session.delete(part)

            # db.session.query(User).get(bill.payer_id).given_money -= bill.total

            # and finally delete the bill
            db.session.commit()
            flash(u'dépense supprimée')
        except:
            db.session.rollback()
            flash(u'impossible de supprimer la dépense')
            LOGGER.p_log(u'impossible de supprimer la dépense', exception=exc_info())   

    return redirect(
        url_for(
        'comptes',
        spends_page='depenses'
    ))
Example #4
0
        def makeParts(value, p_size, spending_name, spending_time):
            """make p_size parts with the value.
            if centimes left, we allocate these lost ones to a random user. sometimes
                it's positive numbers, sometimes not!
            my priority was to NOT have lost centimes.

            P.S.: sorry for this madness with floating and decimal numbers, there wasn't
                any 'easy' way!
            """
            getcontext().prec = 6
            value = dec(str(value))  # I'll probably go to hell for this...

            # attribution aux parts de la valeur entière divisible par p_size
            parts = [int(value/p_size)] * p_size

            # on transforme le reste en centimes que l'on distribue
            left_centimes = int(100 * (value - sum(parts)))

            # attribution aux parts des centimes restants
            for idx, part in enumerate(parts):
                parts[idx] += (left_centimes/p_size) / 100.

            # on attribue les centimes restants à un user aléatoire
            the_last_centime = (left_centimes % p_size) / 100.
            if the_last_centime != 0:
                the_one = randint(0, len(parts)-1)
                parts[the_one] += the_last_centime

            # any error is logged. because money is money. no jokes!
            if float(value) != float(sum(parts)):
                LOGGER.p_log('error in adding a spending', level='warning')
                LOGGER.p_log('spending_time: ' + str(spending_time), blank=True)
                LOGGER.p_log('value: ' + str(value), blank=True)
                LOGGER.p_log('sum: ' + str(sum(parts)), blank=True)
                LOGGER.p_log('parts: ' + str(parts), blank=True)
            return parts