def getgroup(uid):
    group = user.getgroup(uid)
    q = db().view("entry/groups",
        startkey=[group+u"\ufff0"],
        endkey=[group],
        include_docs=True,
        descending=True
    )

    for x in q:
        doc = x["doc"]
        username = user.getname(doc["uid"])
        deletedby = doc.get("deletedby")
        deletedby = deletedby and user.getname(deletedby)
        user.getname(doc["uid"])
        debtors = doc["debtors"]
        debtors = dict((user.getname(x),y) for x,y in debtors)
        
        creditor = user.getname(doc["creditor"])

        yield {
            "id": doc["_id"],
            "amount": doc["amount"],
            "creditor": creditor,
            "date": dateutils.fromtuple(doc["date"]),
            "description": doc["description"],
            "debtors": debtors,
            "username": username,
            "deletedby": deletedby,
        }
def new_do():
    description = local.request.form.get("description", u"")
    creditor = local.request.form.get("creditor", u"")
    amount = local.request.form.get("amount", u"")
    amount = parsenumber(amount)
    
    if amount == None:
        amount = 0

    debtors = []
    for name, weight in local.request.form.items():
        if not name.startswith("member_"):
            continue

        name = name[7:]
        weight = int(weight)
        
        if weight == 0:
            continue

        debtors.append([name, weight])
    
    username = local.session.get("uid")
    group = user.getgroup(local.session.get("uid"))
    entries.add(username, group, description, amount, creditor, debtors)

    redirect("index.index")
def getbalances(uid):
    group = user.getgroup(uid)
    q = db().view("entry/balances",
        startkey=[group],
        endkey=[group+u"\ufff0"],
        group=True,
        reduce=True,
        group_level=2
    )
    
    for x in q:
        yield user.getname(x["key"][1]), x["value"]