def view_update_wallet(req): if req.method == 'GET': user_id = req.GET.get('user_id', None) api_key = req.GET.get('api_key', None) wallet = req.GET.get('new_wallet', None) if all([user_id, api_key, wallet]): if keyRepository(api_key).getKeyBySk() == False and 'test' not in req.GET: response = returnResponse("Api key not found", {}, 'false', 203) return JsonResponse(response, status=203, safe=False) elif not userRepository().getById(user_id): response = returnResponse("Unauthorize, check that 'user_id' is valid", {}, 'false', 401) return JsonResponse(response, status=401, safe=False) else: # Update wallet u = userRepository(None, True) u.createWallet(user_id, wallet) dt = str(datetime.datetime.now()) summary = str("Wallet was updated at ") summary = summary + dt transaction_id = 3 u.createTransaction(user_id, summary, transaction_id) response = returnResponse("Successful", {'summary': summary, 'transaction_id': transaction_id}, 'true', 200) return JsonResponse(response, status=200, safe=False) else: response = returnResponse("Some stuffs are missing in your request", {}, 'false', 406) return JsonResponse(response, status=406, safe=False)
def api_signup(req): if req.method == 'GET': username = req.GET.get('username', None) email = req.GET.get('email', None) password = req.GET.get('password', None) api_key = req.GET.get('api_key', None) test = req.GET.get('test', None) if all([username, email, password, api_key]): form = signupForm(req.GET) # Check API key is correct if keyRepository(api_key).getKeyBySk() == False and test is None: response = returnResponse("Api key not found", {}, 'false', 203) return JsonResponse(response, status=203, safe=False) elif not form.is_valid(): response = returnResponse("There are some errors", form.errors, 'false', 203) return JsonResponse(response, status=203, safe=False) else: user = userRepository(req.GET) user.create() response = returnResponse( "Created Successfully", { 'username': user.getByEmail(req.GET['email']).username, 'email': user.getByEmail(req.GET['email']).email, 'user_id': user.getByEmail(req.GET['email']).id }, 'true', 200) return JsonResponse(response, status=200, safe=False) else: response = returnResponse( "Some things are missing in your request", {}, 'false', 406) return JsonResponse(response, status=406, safe=False)
def view_user_stocks(req): if req.method == 'GET': user_id = req.GET.get('user_id', None) api_key = req.GET.get('api_key', None) if all([user_id, api_key]): if keyRepository( api_key).getKeyBySk() == False and 'test' not in req.GET: response = returnResponse("Api key not found", {}, 'false', 203) return JsonResponse(response, status=203, safe=False) elif not userRepository().getById(user_id): response = returnResponse( "Unauthorize, check that 'user_id' is valid", {}, 'false', 401) return JsonResponse(response, status=401, safe=False) else: stocks = stockRepository() stocks = stocks.getUserStock(user_id, True) response = returnResponse("Query Successful", list(stocks), 'true', 200) return JsonResponse(response, status=200, safe=False) else: response = returnResponse( "Some stuffs are missing in your request", {}, 'false', 406) return JsonResponse(response, status=406, safe=False)
def view_search_stock_price(req): if req.method == 'GET': user_id = req.GET.get('user_id', None) api_key = req.GET.get('api_key', None) stock_symbol = req.GET.get('stock_symbol', None) check_stock = stockRepository() check_stock = check_stock.checkIfStockIsSoldToOthers(stock_symbol) if check_stock is not False: u = userRepository() u = u.getById(user_id) sold = {'sold': True, 'username': u.username} else: sold = False if all([user_id, api_key, stock_symbol]): if keyRepository( api_key).getKeyBySk() == False and 'test' not in req.GET: response = returnResponse("Api key not found", {}, 'false', 203) return JsonResponse(response, status=203, safe=False) elif not userRepository().getById(user_id): response = returnResponse( "Unauthorize, check that 'user_id' is valid", {}, 'false', 401) return JsonResponse(response, status=401, safe=False) else: iex = iexRepository(settings.IEX_TOKEN_SK, stock_symbol) iex = iex.getStockViaClient() if iex is not False: response = returnResponse( "Query Successful", { 'latestPrice': iex['latestPrice'], 'iexRealtimePrice': iex['iexRealtimePrice'], 'extendedPrice': iex['iexRealtimePrice'] }, 'true', 200, sold) return JsonResponse(response, status=200, safe=False) else: response = returnResponse( "IEX returned a query error, check your symbol and try again", {}, 'false', 203) return JsonResponse(response, status=203, safe=False) else: response = returnResponse( "Some stuffs are missing in your request", {}, 'false', 406) return JsonResponse(response, status=406, safe=False)
def update_test_wallet(self, user_id): # Update wallet u = userRepository(None, True) u.createWallet(user_id, 10000) dt = str(datetime.datetime.now()) summary = str("Wallet was updated at ") summary = summary + dt transaction_id = 3 u.createTransaction(user_id, summary, 3)
def view_user_api_wallet(req): if req.method == 'GET': user_id = req.GET.get('user_id', None) api_key = req.GET.get('api_key', None) if all([user_id, api_key]): if keyRepository(api_key).getKeyBySk() == False and 'test' not in req.GET: response = returnResponse("Api key not found", {}, 'false', 203) return JsonResponse(response, status=203, safe=False) elif not userRepository().getById(user_id): response = returnResponse("Unauthorize, check that 'user_id' is valid", {}, 'false', 401) return JsonResponse(response, status=401, safe=False) else: # Get transactions data = userRepository().getUserWalletValues(user_id) response = returnResponse("Successful", model_to_dict(data), 'true', 200) return JsonResponse(response, status=200, safe=False) else: response = returnResponse("Some stuffs are missing in your request", {}, 'false', 406) return JsonResponse(response, status=406, safe=False)
def view_share_stock(req): if req.method == 'GET': user_id = req.GET.get('user_id', None) receiver_id = req.GET.get('receiver_id', None) stock_symbol = req.GET.get('stock_symbol', None) api_key = req.GET.get('api_key', None) if all([user_id, receiver_id, stock_symbol, api_key]): if keyRepository( api_key).getKeyBySk() == False and 'test' not in req.GET: response = returnResponse("Api key not found", {}, 'false', 203) return JsonResponse(response, status=203, safe=False) elif not userRepository().getById(user_id): response = returnResponse( "Unauthorize, check that 'user_id' is valid", {}, 'false', 401) return JsonResponse(response, status=401, safe=False) elif not userRepository().getById(receiver_id): response = returnResponse( "Receiver ID is not a registered user", {}, 'false', 401) return JsonResponse(response, status=401, safe=False) else: stock = stockRepository(req.GET) if stock.checkIfStockIsSold(stock_symbol, user_id) is not False: receiver = userRepository().getById(req.GET['receiver_id']) stock.sellStock(user_id, receiver_id) response = returnResponse( "This stock now belongs to " + receiver.username, {}, 'true', 200) return JsonResponse(response, status=200, safe=False) else: response = returnResponse( "This stock does not belong to this user", {}, 'false', 203) return JsonResponse(response, status=203, safe=False) else: response = returnResponse( "Some stuffs are missing in your request", {}, 'false', 406) return JsonResponse(response, status=406, safe=False)
def api_user_data(req): if req.method == 'GET': user_id = req.GET.get('user_id', None) api_key = req.GET.get('api_key', None) if all([api_key, user_id]): if keyRepository(api_key).getKeyBySk() == False: response = returnResponse("Api key not found", {}, 'false', 203) return JsonResponse(response, status=203, safe=False) elif not userRepository().getById(user_id): response = returnResponse( "Unauthorize, check that 'user_id' is valid", {}, 'false', 401) return JsonResponse(response, status=401, safe=False) else: data = userRepository().getById(user_id) response = returnResponse("Successful", list(data), 'true', 200) return JsonResponse(response, status=200, safe=False) else: response = returnResponse( "Some things are missing in your request", {}, 'false', 406) return JsonResponse(response, status=406, safe=False)
def view_buy_stock(req): if req.method == 'GET': user_id = req.GET.get('user_id', None) stock_symbol = req.GET.get('stock_symbol', None) api_key = req.GET.get('api_key', None) if all([user_id, api_key, stock_symbol]): if keyRepository( api_key).getKeyBySk() == False and 'test' not in req.GET: response = returnResponse("Api key not found", {}, 'false', 203) return JsonResponse(response, status=203, safe=False) elif not userRepository().getById(user_id): response = returnResponse( "Unauthorize, check that 'user_id' is valid", {}, 'false', 401) return JsonResponse(response, status=401, safe=False) else: iex = iexRepository(settings.IEX_TOKEN_SK, stock_symbol) iex = iex.getStockViaClient() if iex == False: response = returnResponse( "This stock does not exist on IEX", {}, 'false', 406) return JsonResponse(response, status=406, safe=False) stock = stockRepository(req.GET) if stock.checkIfStockIsSoldToOthers(stock_symbol) is not False: response = returnResponse("This stock has been purchased", {}, 'false', 406) return JsonResponse(response, status=406, safe=False) if stock.buyStock(req.user.id): response = returnResponse("Stock Successfully Purchased", {}, 'true', 200) return JsonResponse(response, status=200, safe=False) else: response = returnResponse( "You could not purchase this stock, ensure you have enough in wallet", {}, 'false', 406) return JsonResponse(response, status=406, safe=False) else: response = returnResponse( "Some stuffs are missing in your request", {}, 'false', 406) return JsonResponse(response, status=406, safe=False)
def api_login(req): if req.user.is_authenticated: response = returnResponse("Already Logged In", {}, 'false', 403) return JsonResponse(response, status=403, safe=False) if req.method == 'GET': username = req.GET.get('username', None) password = req.GET.get('password', None) api_key = req.GET.get('api_key', None) test = req.GET.get('test', None) if all([username, password, api_key]): # Check API key is correct if keyRepository(api_key).getKeyBySk() == False and test is None: response = returnResponse("Api key not found", {}, 'false', 203) return JsonResponse(response, status=203, safe=False) elif not auth.authenticate(username=username, password=password): response = returnResponse( "Invalid Credentials", { "errors": { "credential": ["Your username or password does not exist"] } }, 'false', 406) return JsonResponse(response, status=406, safe=False) else: user = userRepository() response = returnResponse( "Authenticate Successfully", { 'username': user.getByUsername( req.GET['username']).email, 'email': user.getByUsername(req.GET['username']).email, 'user_id': user.getByUsername(req.GET['username']).id }, 'true', 200) return JsonResponse(response, status=200, safe=False) else: response = returnResponse( "Some things are missing in your request", {}, 'false', 406) return JsonResponse(response, status=406, safe=False)
def test_create_transactions(self): user = json.loads(self.login_sample_user()) response = userRepository(None,True).createTransaction(user['data']['user_id'], "Test", 3, True) self.assertEqual(1, len(response))
def test_repo_transactions(self): user = json.loads(self.login_sample_user()) response = userRepository().getUserTransaction(user['data']['user_id'], True) self.assertEqual(0, len(response))