Beispiel #1
0
def read_cycle(cycleId):
    isOk, err = _check_params()
    if not isOk:
        return err

    billCycle = BillingCycle.query.filter(BillingCycle.id == cycleId).first()
    if billCycle is None: return api.not_found()  # noqa:E701
    return api.success(billCycle.get_api_dict())
Beispiel #2
0
def delete_bill(billId):
    try:
        if not _delete_bill(billId): return api.not_found()  # noqa:E701
        DB.session.commit()
        return api.success({'deleted': True})
    except Exception as ex:
        print("Unexpected exception deleting bill {}: {}".format(billId, ex))
        return api.success({'deleted': False})
Beispiel #3
0
def remove_from_household(userId):
    try:
        currentUser = g.user
        if not currentUser.householdCreator:
            return api.unauthorized()
        user = User.query.filter(User.id == userId and User.householdId == currentUser.householdId).first()
        if user is None:
            return api.not_found()
    except Exception as ex:
        print("Error removing user {} from household {}: {}".format(userId, g.user.householdId, ex))
        return api.server_error()
Beispiel #4
0
def update_bill(billId, formData=None):
    name, cost = formData
    bill = _get_bill(billId)
    if bill is None: return api.not_found()  # noqa:E701
    bill.name = name
    bill.cost = cost
    try:
        DB.session.add(bill)
        DB.session.commit()
        return api.success(bill.get_api_dict())
    except Exception as ex:
        print("Unexpected exception updating bill {}: {}".format(billId, ex))
        return api.server_error()
Beispiel #5
0
def leave_household():
    try:
        user = User.query.filter(User.id == g.user.id).first()
        if user.householdId is None:
            return api.not_found()

        if user.householdCreator:
            pass  # Some kind of extra check to make sure household is empty or something

        user.householdId = None
        user.householdCreator = False
        DB.session.add(user)
        DB.session.commit()
    except Exception as ex:
        print("Error leaving household {}: {}".format(g.user.householdId, ex))
        return api.server_error()
def accept_invitation(id):
    currentUser = g.user
    invitation = HouseholdInvitation.query.filter(HouseholdInvitation.id == id and HouseholdInvitation.toUserId == currentUser.id).first()
    if invitation is None:
        return api.not_found()

    currentUser = User.query.filter(User.id == currentUser.id).first()
    currentUser.householdId = invitation.householdId
    currentUser.householdCreator = False

    apiDict = invitation.household.get_api_dict()
    DB.session.add(currentUser)
    DB.session.query(HouseholdInvitation).filter(HouseholdInvitation.toUserId == currentUser.id).delete()
    DB.session.commit()

    return api.success(apiDict)
def send_invitation(formData=None):
    email, message = formData
    if g.user.email.lower() == message.lower():
        return api.bad_request(ErrorCode.Self)
    toUser = User.query.filter(User.email == email).first()
    if toUser is None:
        return api.not_found()
    if toUser.householdId is not None:
        return api.bad_request(ErrorCode.AlreadyInHousehold)

    invitation = HouseholdInvitation(householdId=g.user.householdId,
                                     fromUserId=g.user.id, toUserId=toUser.id,
                                     sendTime=dates.utcnow(), message=message)
    DB.session.add(invitation)
    DB.session.commit()
    return api.success(invitation.get_api_dict())
Beispiel #8
0
def make_bill_payment(formData=None):
    cost = formData[0]  # Unpack single value tuple
    userId = g.user.id
    bill = Bill.query.filter(Bill.id == g.billId).first()
    if bill is None: return api.not_found()  # noqa:E701

    currentCycle = bill.get_current_billing_cycle()
    payment = BillPayment(billId=bill.id,
                          billCycleId=currentCycle.id,
                          amount=cost,
                          userId=userId)
    payment.timestamp = validation.utcnow()
    DB.session.add(payment)
    DB.session.commit()

    result = {'payment': payment.get_api_dict(), 'bill': bill.get_api_dict()}
    return api.success(result)
Beispiel #9
0
def get_household_users():
    if g.user.householdId is None:
        return api.not_found()

    users = User.query.filter(User.householdId == g.user.householdId and User.id != g.user.id).all()
    return api.success({'users': [user.get_api_dict() for user in users]})
Beispiel #10
0
def my_household():
    household = Household.query.filter(Household.id == g.user.householdId).first()
    if household is None:
        return api.not_found()
    else:
        return api.success(household.get_api_dict())
Beispiel #11
0
def read_bill(billId):
    bill = _get_bill(billId)
    if bill is None: return api.not_found()  # noqa:E701
    return api.success(bill.get_api_dict())