def post(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) user = serializer.save() portfolio = Portfolio(owner=user, name=user.username + '_portfolio', cash=0) portfolio.save() return Response({ 'user': UserSerializer(user, context=self.get_serializer_context()).data, 'token': AuthToken.objects.create(user)[1] })
def update_user_portfolio(): store = InMemoryDataStore() json_data = request.get_json(force=True) cash_json = json_data["cashAssets"] cash = CashAssets( cash_json["checkingAcs"], cash_json["savingsAcs"], cash_json["moneyMarketAccounts"], cash_json["savingsBonds"], cash_json["cds"], cash_json["lifeInsurance"], Other(cash_json["other"]["title"], cash_json["other"]["amount"])) invested_json = json_data["investedAssets"] invested = InvestedAssets( invested_json["brokerage"], Other(invested_json["otherTax"]["title"], invested_json["otherTax"]["amount"]), invested_json["ira"], invested_json["rothIra"], invested_json["k401"], invested_json["sepIra"], invested_json["keogh"], invested_json["pension"], invested_json["annuity"], invested_json["realEstate"], invested_json["soleProp"], invested_json["partnership"], invested_json["cCorp"], invested_json["sCorp"], invested_json["limitedLiabilityCompany"], Other(invested_json["otherBusiness"]["title"], invested_json["otherBusiness"]["amount"])) use_json = json_data["useAssets"] use = UseAssets( use_json["principalHome"], use_json["vacationHome"], use_json["vehicles"], use_json["homeFurnishings"], use_json["artsAndAntiques"], use_json["jewelryAndFurs"], Other(use_json["other"]["title"], use_json["other"]["amount"])) current_json = json_data["currentLiabilities"] current = CurrentLiabilities( current_json["creditCardBalance"], current_json["incomeTaxOwed"], Other(current_json["other"]["title"], current_json["other"]["amount"])) long_json = json_data["longTermLiabilities"] long = LongTermLiabilities( long_json["homeMortgage"], long_json["homeEquityLoan"], long_json["rentPropertiesMortgage"], long_json["carLoans"], long_json["studentLoans"], long_json["lifeInsurancePolicyLoans"], Other(long_json["other"]["title"], long_json["other"]["amount"])) currency = Currencies[json_data["currency"]] email = json_data["email"] store.update_user_portfolio(email=email, portfolio=Portfolio( currency, cash, invested, use, current, long)) return jsonify("Success")
def get_test_portfolio() -> Portfolio: """ Function to return a populated instance of Portfolio() with GBP :return: new Portfolio() """ cash, use, invested, current, long = get_test_assets_and_liabilities() p = Portfolio(Currencies.GBP, cash, invested, use, current, long) return p
def test_update_user_portfolio_persists_change(self): cash, use, invested, current, long = utils.get_test_assets_and_liabilities( ) p = Portfolio(Currencies.GBP, cash, invested, use, current, long) users_file = os.path.join(self.TEST_STORAGE_PATH, "users.pck") store = InMemoryDataStore(users_pck_file=users_file) self.assertIsNone(None, InMemoryDataStore._USERS[0].portfolio) store.update_user_portfolio("*****@*****.**", p) self.assertEqual(Currencies.GBP, InMemoryDataStore._USERS[0].portfolio.currency) p.currency = Currencies.USD store.update_user_portfolio("*****@*****.**", p) self.assertEqual(Currencies.USD, InMemoryDataStore._USERS[0].portfolio.currency)
def test_portfolio_creation_successful(self): p = Portfolio( Currencies.GBP, self.cash, self.invested, self.use, self.current, self.long) # will pass if everything is instantiated correctly time.sleep(0.1) end = datetime.now() # check the timestamp self.assertLess(p.timestamp, end)
def test_update_user_portfolio_raises_error_for_invalid_user(self): users_file = os.path.join(self.TEST_STORAGE_PATH, "users.pck") store = InMemoryDataStore(users_file) cash, use, invested, current, long = utils.get_test_assets_and_liabilities( ) with self.assertRaises(ValueError): store.update_user_portfolio( "*****@*****.**", Portfolio(Currencies.GBP, cash, invested, use, current, long))
os.makedirs(STORAGE_PATH) if "users.pck" in os.listdir(STORAGE_PATH): # load data into memory InMemoryDataStore( users_pck_file=os.path.join(STORAGE_PATH, "users.pck")) else: InMemoryDataStore().add_user("John", "Doe", 29, "*****@*****.**") cash = CashAssets(1, 2, 3, 4, 5, 6, Other("Test", 7)) use = UseAssets(10, 20, 30, 40, 50, 60, Other("TestUse", 70)) invested = InvestedAssets(1.5, Other("TestInvest", 2.5), 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5, Other("TestInvest", 16.5)) current = CurrentLiabilities(0, 111.1, Other("TestCurrent", 21.3)) long = LongTermLiabilities(10, 20, 30, 40, 50, 60, Other(amount=70)) p = Portfolio(Currencies.GBP, cash, invested, use, current, long) InMemoryDataStore().update_user_portfolio("*****@*****.**", p) # start the servers server = BackendServer("localhost", 5000) server.start() _ = input("Hit enter to kill ... ") server.stop() # persist the data to disk InMemoryDataStore().save_to_disk()
def test_portfolio_returns_correct_totals(self): p = Portfolio(Currencies.GBP, self.cash, self.invested, self.use, self.current, self.long) self.assertEqual(self.assets, p.total_assets) self.assertEqual(self.liabilities, p.total_liabilities)
def test_portfolio_creation_throws_error_with_invalid_currency(self): with self.assertRaises(ValueError): _ = Portfolio("GBP", self.cash, self.invested, self.use, self.current, self.long)