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 #2
0
def register(formData=None):
    displayName, email, password = formData
    isOk, result = security.do_register(displayName, email, password)

    if isOk: result = api.success(result.get_api_dict(privacy=False))  # noqa:E701
    else: result = api.bad_request(result)  # noqa:E701

    return result
Beispiel #3
0
    def get_form(*args, **kwargs):
        isOk, data = api.get_post_data()
        if not isOk: return data  # noqa:E701

        code, results = validation.basic_form(fields, data, customValidators)
        if code is not ErrorCode.Success:
            return api.bad_request(code, results)

        return method(formData=results, *args, **kwargs)
Beispiel #4
0
def login(formData=None):
    email, password = formData

    expirationInHours = 6

    user, jwt, expires = security.do_login(email, password, expirationInHours)
    if not user: return api.bad_request(ErrorCode.IncorrectLogin)  # noqa:E701

    return token_result(user, jwt, expires)
Beispiel #5
0
def _check_params(useCycleId):
    missingParams = []
    if g.billId == -1:
        missingParams.append('billId')
    if useCycleId and g.cycleId == -1:
        missingParams.append('cycleId')
    if len(missingParams) != 0:
        return False, api.bad_request(ErrorCode.MissingParameter,
                                      missingParams)
    return True, None
Beispiel #6
0
def create_bill(formData=None):
    billType, name, cost, date = formData

    isOk, code = _date_validator_round2(date, billType)
    if not isOk: return api.bad_request(code)  # noqa:E701

    try:
        bill = Bill(type=billType, name=name, householdId=g.user.householdId, cost=cost, date=date)
        DB.session.add(bill)
        DB.session.commit()  # Commit so the new bill id is retrieved

        cycle = bill.create_current_billing_cycle()
        DB.session.add(cycle)
        DB.session.commit()
        return api.success(bill.get_api_dict())
    except Exception as ex:
        print("Exception on bill insert: {}".format(ex))
        return api.server_error()
Beispiel #7
0
def new_household(formData=None):
    name = formData[0]
    user = User.query.filter(User.id == g.user.id).first()

    if user.householdId is not None:
        return api.bad_request(ErrorCode.AlreadyInHousehold)

    household = Household(name=name, created=validation.utcnow())
    try:
        DB.session.add(household)
        DB.session.commit()

        user.householdId = household.id
        user.householdCreator = True
        DB.session.add(household)
        DB.session.commit()
        return api.success(household.get_api_dict())
    except Exception as ex:
        print("Unexpected exception on household insert: {}".format(ex))
        return api.server_error()
Beispiel #8
0
def _check_params():
    if g.billId is None:
        return False, api.bad_request(ErrorCode.MissingParameter, ['billId'])
    return True, None