예제 #1
0
 def post(self, project):
     form = MemberForm(project, meta={"csrf": False})
     if form.validate():
         member = Person()
         form.save(project, member)
         db.session.commit()
         return member.id, 201
     return form.errors, 400
예제 #2
0
파일: api.py 프로젝트: rscoates/ihatemoney
 def add(self, project):
     form = MemberForm(project, meta={'csrf': False})
     if form.validate():
         member = Person()
         form.save(project, member)
         db.session.commit()
         return 201, member.id
     return 400, form.errors
예제 #3
0
def add_member():
    # FIXME manage form errors on the list_bills page
    form = MemberForm(g.project)
    if request.method == "POST":
        if form.validate():
            member = form.save(g.project, Person())
            db.session.commit()
            flash(_("%(member)s has been added", member=member.name))
            return redirect(url_for(".list_bills"))

    return render_template("add_member.html", form=form)
예제 #4
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()