def signup(): # get the request parameters email = None password = None if request.is_json: request_json = request.get_json() email = request_json.get('email') password = request_json.get('password') else: email = request.form['email'] password = request.form['password'] # make sure all parameters exist if email is None: return HTTPErrorResponse.raise_missing_parameter('email') if password is None: return HTTPErrorResponse.raise_missing_parameter('password') if not Validation.validate_email(email): return HTTPErrorResponse.raise_invalid_email(email) # make sure email is a valid email address user = get_user_by_email(current_app.session, email) # check for user is already in the database if user: return HTTPErrorResponse.raise_user_already_exists() # create the user new_user = create_user(current_app.session, email, password) return Response(status=200)
def login(): # get the request parameters email = None password = None if request.is_json: request_json = request.get_json() email = request_json.get('email') password = request_json.get('password') else: email = request.form['email'] password = request.form['password'] # make sure all parameters exist if email is None: return HTTPErrorResponse.raise_missing_parameter('email') if password is None: return HTTPErrorResponse.raise_missing_parameter('password') # make sure email is a valid email address if not Validation.validate_email(email): return HTTPErrorResponse.raise_invalid_email(email) # check for user in the database user = get_user_by_email(current_app.session, email) if not user or not check_hashed_string(password, user.password): return HTTPErrorResponse.raise_invalid_credentials() # login the user login_user(user, remember=True) return Response(status=200)
def create_account(): """Create an account for the current_user """ session = current_app.session name = None balance = None # get the request parameters if request.is_json: request_json = request.get_json() name = request_json.get('name') balance = request_json.get('balance') else: name = request.form['name'] balance = request.form['balance'] email = current_user.email # make sure all parameters exist if name is None: return HTTPErrorResponse.raise_missing_parameter('name') if balance is None: return HTTPErrorResponse.raise_missing_parameter('balance') if email is None: return HTTPErrorResponse.raise_internal_server_error() # find user so we can control session (as oppose to using current_user) user = get_user_by_email(session=session, email=email) user_account = acc.create_account(name=name, balance=balance, user=user) return HTTPResponse.return_json_response(account_to_dict(user_account), 200)
def create_single_transaction(): # get the request parameters account_id = None name = None amount = None post_date = None if request.is_json: request_json = request.get_json() account_id = request_json.get('account_id') name = request_json.get('name') amount = request_json.get('amount') post_date = request_json.get('date') else: account_id = request.form['account_id'] name = request.form['name'] amount = request.form['amount'] post_date = request.form['date'] # get date from string date = None try: date = parser.parse(post_date) except Exception as err: if err is ValueError: return HTTPErrorResponse.raise_invalid_parameter('date', "Invalid date format '%s'" % post_date) if err is OverflowError: return HTTPErrorResponse.raise_invalid_parameter('date', "Date parameter caused OverflowError") # get user account with ID for current user user_account = get_user_account_by_id(account_id, current_user) if user_account is None: return HTTPErrorResponse.raise_not_found('User Account with account_id') # create transaction new_transaction = transactions.create_single_transaction( account=user_account, name=name, date=date, amount=amount ) if new_transaction is None: HTTPErrorResponse.raise_internal_server_error('Failed to create new single transaction') return HTTPResponse.return_json_response(single_transaction_to_dict(new_transaction), 200)
def get_single_transactions(account_id): # get user account with ID for current user account = get_user_account_by_id(account_id=str(account_id), user=current_user) if account is None: return HTTPErrorResponse.raise_not_found('Account with id') # create list of all single transactions in account as dict dict_accounts = [single_transaction_to_dict(i) for i in account.single_transactions] return HTTPResponse.return_json_response(dict_accounts, 200)
def get_account(account_id): # find the user account with the given id user_account = current_user.accounts.filter( Account.account_id == str(account_id)).scalar() # return 404 if not found if user_account is None: return HTTPErrorResponse.raise_not_found('Account with id') # return account as dict return HTTPResponse.return_json_response(account_to_dict(user_account), 200)
def get_account_balance(account_id, date): # get date from string try: date = parser.parse(date).date() except Exception as err: if err is ValueError: return HTTPErrorResponse.raise_invalid_parameter( 'date', "Invalid date format '%s'" % date) if err is OverflowError: return HTTPErrorResponse.raise_invalid_parameter( 'date', "Date parameter caused OverflowError") # find the user account with the given id user_account = current_user.accounts.filter( Account.account_id == str(account_id)).scalar() # return 404 if not found if user_account is None: return HTTPErrorResponse.raise_not_found('Account with id') # get the account balance on the given date balance = get_account_balance_on_date(user_account, date) return HTTPResponse.return_json_response({'balance': balance}, 200)