def put(self, fund_id): put_data = request.get_json() if not put_data: return UTILS.api_response(msg=UTILS.INVALID_PAYLD, code=400) name = put_data.get('name') try: fund = Fund.query.get(fund_id) existingFunds = Fund.query.filter_by(name=name).first() if existingFunds: return UTILS.api_response(msg=UTILS.EXISTS(TYPE, name), code=400, data=existingFunds.to_json()) if fund: fund = UTILS.update(fund, 'name', name) return UTILS.api_response(msg=UTILS.UPDATED(TYPE, name), code=200, data=fund.to_json()) else: return UTILS.api_response( msg=UTILS.NOT_EXISTS(TYPE, fund_id), code=404, ) except exc.IntegrityError as e: db.session.rollback() return UTILS.api_response( msg=f'{UTILS.INTEGRITY_ERR} {self.__name__}', code=400, data=f'{str(e)}')
def test_update_committment_no_id(self): """Ensure error is thrown if an id is not provided.""" UTILS.add_committment(self.fund_1.id, 1000) with self.client as client: response = client.put( '/committments/blah', data=json.dumps({'fund_id': 1}), content_type='application/json', ) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 404) self.assertIn(UTILS.NOT_EXISTS(TYPE, 'blah'), data['message']) self.assertIn('fail', data['status']) self.assertFalse(data['data'])
def test_update_fund_incorrect_id(self): """Ensure error is thrown if the id is incorrect for updating fund.""" UTILS.add_fund("fund!") with self.client as client: response = client.put( '/funds/999', data=json.dumps({ 'name': 'fund_1', }), content_type='application/json', ) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 404) self.assertIn(UTILS.NOT_EXISTS(TYPE, '999'), data['message']) self.assertIn('fail', data['status']) self.assertFalse(data['data'])
def get(self, fund_id): """Get single fund details""" try: fund = Fund.query.filter_by(id=int(fund_id)).first() if not fund: return UTILS.api_response( msg=UTILS.NOT_EXISTS(TYPE, fund_id), code=404, ) else: return UTILS.api_response(msg=UTILS.SUCCESS(TYPE, fund.id), code=200, data=fund.to_json()) except ValueError as e: return UTILS.api_response(msg=f'{UTILS.VALUE_ERR} {self.__name__}', code=404, data=f'{str(e)}')
def delete(self, fund_id): """Delete single fund details""" try: fund = Fund.query.filter_by(id=int(fund_id)).first() if not fund: return UTILS.api_response( msg=UTILS.NOT_EXISTS(TYPE, fund_id), code=404, ) else: UTILS.delete(fund) return UTILS.api_response(msg=UTILS.DELETED(TYPE, fund.id), code=200) except ValueError as e: return UTILS.api_response(msg=f'{UTILS.VALUE_ERR} {self.__name__}', code=404, data=f'{str(e)}')
def get(self, capitalcall_id): """Get single capitalcall details""" try: call = CapitalCall.query \ .filter_by(id=int(capitalcall_id)).first() if not call: return UTILS.api_response( msg=UTILS.NOT_EXISTS(TYPE, capitalcall_id), code=404, ) else: return UTILS.api_response(msg=UTILS.SUCCESS(TYPE, call.id), code=200, data=call.to_json()) except ValueError as e: return UTILS.api_response(msg=f'{UTILS.VALUE_ERR} {self.__name__}', code=404, data=f'{str(e)}')
def delete(self, investment_id): """Delete single investment details""" try: inv = Investment.query.filter_by(id=int(investment_id)).first() if not inv: return UTILS.api_response( msg=UTILS.NOT_EXISTS(TYPE, investment_id), code=404, ) else: UTILS.delete(inv) return UTILS.api_response(msg=UTILS.DELETED(TYPE, inv.id), code=200) except ValueError as e: return UTILS.api_response(msg=f'{UTILS.VALUE_ERR} {self.__name__}', code=404, data=f'{str(e)}')
def delete(self, capitalcall_id): """Delete single capitalcall details""" try: capitalcall = CapitalCall.query \ .filter_by(id=int(capitalcall_id)).first() if not capitalcall: return UTILS.api_response( msg=UTILS.NOT_EXISTS(TYPE, capitalcall_id), code=404, ) else: UTILS.delete(capitalcall) return UTILS.api_response(msg=UTILS.DELETED( TYPE, capitalcall.id), code=200) except ValueError as e: return UTILS.api_response(msg=f'{UTILS.VALUE_ERR} {self.__name__}', code=404, data=f'{str(e)}')
def get(self, committment_id): """Get single committment details""" try: committment = Committment.query \ .filter_by(id=int(committment_id)).first() if not committment: return UTILS.api_response( msg=UTILS.NOT_EXISTS(TYPE, committment_id), code=404, ) else: return UTILS.api_response(msg=UTILS.SUCCESS( TYPE, committment.id), code=200, data=committment.to_json()) except ValueError as e: return UTILS.api_response(msg=f'{UTILS.VALUE_ERR} {self.__name__}', code=404, data=f'{str(e)}')
def delete(self, committment_id): """Delete single committment details""" try: committment = Committment.query \ .filter_by(id=int(committment_id)).first() if not committment: return UTILS.api_response( msg=UTILS.NOT_EXISTS(TYPE, committment_id), code=404, ) else: UTILS.delete(committment) return UTILS.api_response(msg=UTILS.DELETED( TYPE, committment.id), code=200) except ValueError as e: return UTILS.api_response(msg=f'{UTILS.VALUE_ERR} {self.__name__}', code=404, data=f'{str(e)}')
def get(self, investment_id): """Get single investment details""" try: inv = Investment.query.filter_by(id=int(investment_id)).first() stmt = FundInvestments.select().where( FundInvestments.c.fundinvestment_id == int(investment_id)) res = db.session.execute(stmt).fetchall() obj = [create_obj(r, [inv]) for r in res][0] if not inv: return UTILS.api_response( msg=UTILS.NOT_EXISTS(TYPE, investment_id), code=404, ) else: return UTILS.api_response(msg=UTILS.SUCCESS(TYPE, inv.id), code=200, data=obj) except ValueError as e: return UTILS.api_response(msg=f'{UTILS.VALUE_ERR} {self.__name__}', code=404, data=f'{str(e)}')
def put(self, capitalcall_id): put_data = request.get_json() if not put_data: return UTILS.api_response(msg=UTILS.INVALID_PAYLD, code=400) name = put_data.get('name') capital = put_data.get('capital') try: call = CapitalCall.query.get(capitalcall_id) existingCalls = CapitalCall.query \ .filter_by(name=name).first() if existingCalls: return UTILS.api_response(msg=UTILS.EXISTS(TYPE, name), code=400, data=existingCalls.to_json()) if call: if (capital and (capital == call.capital)): return UTILS.api_response(msg=UTILS.NO_CHANGE( TYPE, f'{call.name}'), code=400, data=call.to_json()) if name: call = UTILS.update(call, 'name', name) if capital: call = UTILS.update(call, 'capital', capital) return UTILS.api_response( msg=UTILS.UPDATED(TYPE, name), code=200, data=CapitalCall.query.get(capitalcall_id).to_json()) else: return UTILS.api_response( msg=UTILS.NOT_EXISTS(TYPE, capitalcall_id), code=404, ) except exc.IntegrityError as e: db.session.rollback() return UTILS.api_response( msg=f'{UTILS.INTEGRITY_ERR} {self.__name__}', code=400, data=f'{str(e)}')
def put(self, committment_id): """Update single committment details""" put_data = request.get_json() if not put_data: return UTILS.api_response(msg=UTILS.INVALID_PAYLD, code=400) fund_id = put_data.get('fund_id') amount = put_data.get("amount") try: c = Committment.query.get(committment_id) if c: if (fund_id and (fund_id == c.fund_id)) or \ (amount and (amount == c.amount)): return UTILS.api_response( msg=UTILS.NO_CHANGE(TYPE, f'{c.id} in fund {c.fund_id}'), code=400, data=c.query.get(committment_id).to_json()) if fund_id: c = UTILS.update(c, 'fund_id', int(fund_id)) if amount: c = UTILS.update(c, 'amount', int(amount)) return UTILS.api_response(msg=UTILS.UPDATED( TYPE, f'{c.id} in fund {c.fund_id}'), code=200, data=c.to_json()) else: return UTILS.api_response( msg=UTILS.NOT_EXISTS(TYPE, committment_id), code=404, ) except exc.IntegrityError as e: db.session.rollback() return UTILS.api_response( msg=f'{UTILS.INTEGRITY_ERR} {self.__name__}', code=400, data=f'{str(e)}')