Exemple #1
0
def get_billform_form(project, set_default=True, bill_id=None, **kwargs):
    """Return an instance of BillForm configured for a particular project.

    :set_default: if set to True, on GET methods (usually when we want to
                  display the default form, it will call set_default on it.

    """

    # we're using the billowers object here to fill in the attributes used by the form
    bill = Bill()
    is_advanced_form = False
    if set_default and request.method == "GET":
        billowers = []
        # set the default values for all members in the project
        for member in project.active_members:
            billower = BillOwers()
            billower.included = True
            billower.person_id = member.id
            billower.person_name = member.name
            billower.weight = member.weight
            if billower.weight != 1:
                is_advanced_form = True
            billowers.append(billower)
        bill.billowers = billowers
        bill.date = datetime.now()

    # if we're editing the bill a bill_id is passed
    # we create a new billowers list
    # to ensure that all members of the project are included in the list
    # no matter whether they are an ower or not

    if bill_id is not None:
        # get the existing bill
        bill_to_edit = Bill.query.get(project, bill_id)

        # loop through the active member
        for member in project.active_members:
            # loop through the billowers and check if the member is a billower
            for owers_to_edit in bill_to_edit.billowers:
                if owers_to_edit.ower.id == member.id:
                    # set the included and person_name as they are attributes in the form
                    # that aren't specified explicitly in the billowers table
                    if owers_to_edit.weight != 0:
                        owers_to_edit.included = True
                    else:
                        owers_to_edit.included = False
                    owers_to_edit.person_name = owers_to_edit.ower.name
                    if owers_to_edit.weight != 1:
                        is_advanced_form = True
                    break

        bill = bill_to_edit

    form = BillForm(obj=bill, meta={"csrf": False})
    active_members = [(m.id, m.name) for m in project.active_members]
    form.payer.choices = active_members
    form.advanced = is_advanced_form
    return form
Exemple #2
0
def add_bill():
    form = get_billform_form(g.project)
    if g.project.advanced_weighting_enabled:
        form.advanced = True

    if request.method == "POST":

        if form.validate():
            # save last selected payer in session
            session["last_selected_payer"] = form.payer.data
            session.update()

            bill = Bill()
            db.session.add(form.save(bill))
#            for
            db.session.commit()

            flash(_("The bill has been added"))

            args = {}
            if form.submit2.data:
                args["add_bill"] = True

            return redirect(url_for(".list_bills", **args))

    return render_template("add_bill.html", form=form)
Exemple #3
0
 def post(self, project):
     form = get_billform_for(project, True, meta={"csrf": False})
     if form.validate():
         bill = Bill()
         form.save(bill, project)
         db.session.add(bill)
         db.session.commit()
         return bill.id, 201
     return form.errors, 400
Exemple #4
0
 def add(self, project):
     form = get_billform_for(project, True, meta={'csrf': False})
     if form.validate():
         bill = Bill()
         form.save(bill, project)
         db.session.add(bill)
         db.session.commit()
         return 201, bill.id
     return 400, form.errors
Exemple #5
0
def undo_delete_bill():
    global ghost_billform
    args = {}

    bill = Bill()
    db.session.add(ghost_billform.save(bill, g.project))
    db.session.commit()

    flash(_("Restored bill"))

    return redirect(url_for(".list_bills"))
Exemple #6
0
def add_bill():
    form = get_billform_for(g.project)
    if request.method == 'POST':
        if form.validate():
            # save last selected payer in session
            session['last_selected_payer'] = form.payer.data
            session.update()

            bill = Bill()
            db.session.add(form.save(bill, g.project))
            db.session.commit()

            flash(_("The bill has been added"))

            args = {}
            if form.submit2.data:
                args['add_bill'] = True

            return redirect(url_for('.list_bills', **args))

    return render_template("add_bill.html", form=form)
Exemple #7
0
def import_project(file, project):
    json_file = json.load(file)

    # Check if JSON is correct
    attr = ["what", "payer_name", "payer_weight", "amount", "date", "owers"]
    attr.sort()
    for e in json_file:
        if len(e) != len(attr):
            raise ValueError
        list_attr = []
        for i in e:
            list_attr.append(i)
        list_attr.sort()
        if list_attr != attr:
            raise ValueError

    # From json : export list of members
    members_json = get_members(json_file)
    members = project.members
    members_already_here = list()
    for m in members:
        members_already_here.append(str(m))

    # List all members not in the project and weight associated
    # List of tuples (name,weight)
    members_to_add = list()
    for i in members_json:
        if str(i[0]) not in members_already_here:
            members_to_add.append(i)

    # List bills not in the project
    # Same format than JSON element
    project_bills = project.get_pretty_bills()
    bill_to_add = list()
    for j in json_file:
        same = False
        for p in project_bills:
            if same_bill(p, j):
                same = True
                break
        if not same:
            bill_to_add.append(j)

    # Add users to DB
    for m in members_to_add:
        Person(name=m[0], project=project, weight=m[1])
    db.session.commit()

    id_dict = {}
    for i in project.members:
        id_dict[i.name] = i.id

    # Create bills
    for b in bill_to_add:
        owers_id = list()
        for ower in b["owers"]:
            owers_id.append(id_dict[ower])

        bill = Bill()
        form = get_billform_for(project)
        form.what = b["what"]
        form.amount = b["amount"]
        form.date = parse(b["date"])
        form.payer = id_dict[b["payer_name"]]
        form.payed_for = owers_id

        db.session.add(form.fake_form(bill, project))

    # Add bills to DB
    db.session.commit()