def setUp(self): self.app = create_app('testing') self.client = self.app.test_client() self.app.app_context().push() with self.app.app_context(): db.create_all() self.user1 = Users( username='******', email='*****@*****.**', password='******') self.user1.save() self.user2 = Users( username='******', email='*****@*****.**', password='******') self.user2.save() self.project1 = Projects( name="User1's Project", description='sample project', contract_value='5000', percentage_return='22', start_date=datetime.date.today(), end_date=datetime.date.today()+datetime.timedelta(days=10), user_email=self.user1.email) self.project1.save() self.project2 = Projects( name="User2's Project", description='sample project', contract_value='3000', percentage_return='10', start_date=datetime.date.today(), end_date=datetime.date.today()+datetime.timedelta(days=10), user_email=self.user2.email) self.project2.save() self.bid = Bids( amount='200', date=datetime.date.today(), user_email=self.user1.email, project_id=self.project2.id,) self.bid.save()
def create_project(): """Create a project""" data = { 'email': request.json.get('email'), 'password': request.json.get('password'), 'name': request.json.get('name'), 'description': request.json.get('description'), 'contract_value': request.json.get('value'), 'percentage_return': request.json.get('return'), 'end_date': request.json.get('end_date'), } check_valid(CreateProjectSchema().validate(data)) user = Users.get_user(data['email'], data['password']) project = Projects( name=data['name'], description=data['description'], contract_value=data['contract_value'], percentage_return=data['percentage_return'], start_date=datetime.date.today(), end_date=data['end_date'], user_email=user.email) project.save() data['project_id'] = project.id return jsonify({ 'message': 'Project Created Successfully.', 'data': data, }), 201
def new_proj(request): """添加一个项目""" name = request_post(request, 'pname', required=True) desc = request_post(request, 'pdesc', required=True) p = Projects.objects.filter(name=name).first() if not p: p = Projects() p.name = name p.desc = desc p.domain = 'http://' + request.META.get('HTTP_HOST') p.created_by = request.user p.name = name p.desc = desc p.save() return HttpResponseRedirect(reverse('index'))
def update_bid(): """Update a bid""" data = { 'email': request.json.get('email'), 'password': request.json.get('password'), 'amount': request.json.get('amount'), 'bid_id': request.json.get('bid_id'), } check_valid(UpdateBidSchema().validate(data)) user = Users.get_user(data['email'], data['password']) bid = Bids.get_bid(data['email'], data['bid_id']) project = Projects.get_project(bid.project_id, 0) total_bids = 0 current_amnt = float(bid.amount) new_amnt = float(request.json['amount']) for item in project.bids_received: total_bids = total_bids + float(item.amount) total_bids = total_bids - current_amnt + new_amnt if float(project.contract_value) < total_bids: return jsonify({ "msg": "The amount added is too much. Reduce it by {}".format( (total_bids - float(project.contract_value)))}), 400 bid.amount = new_amnt bid.date = datetime.date.today() bid.save() return jsonify({ 'message': 'Updated Bid.', 'data': bid.serialize(), }), 200
def bid_to_project(): """Bid to fund a project""" data = { 'email': request.json.get('email'), 'password': request.json.get('password'), 'amount': request.json.get('amount'), 'project_id': request.json.get('project_id'), } check_valid(BidSchema().validate(data)) data['amount'] = float(data['amount']) user = Users.get_user(data['email'], data['password']) project = Projects.get_project( request.json['project_id'], data['amount']) bid = Bids( amount=data['amount'], date=datetime.date.today(), user_email=user.email, project_id=project.id,) bid.save() data['user_email'] = user.email data['project_id'] = project.id data['bid_id'] = bid.id data['potential_profit_margin'] = "{0:.2f}".format( float(project.percentage_return) / 100 * data['amount']) return jsonify({ 'message': 'Bid Created Successfully.', 'data': data, }), 201
def get_projects(): """Get all projects""" id = request.args.get('id') if id: projects = Projects.query.filter_by( id=id, active=True).all() else: projects = Projects.query.filter_by(active=True).all() res = list() for item in projects: if item.active is False: continue res.append(Projects.serialize(item)) return jsonify({'data': res}), 200
def my_projects(): """Get single user's projects""" data = { 'email': request.json.get('email'), 'password': request.json.get('password'), } check_valid(EmailPasswordSchema().validate(data)) user = Users.get_user(data['email'], data['password']) projects = Projects.query.filter_by(user_email=user.email) res = list() for item in projects: if item.active is False: continue res.append(Projects.serialize(item)) return jsonify({'data': res}), 200
def delete_bid(): """Delete a bid""" data = { 'email': request.json.get('email'), 'password': request.json.get('password'), 'bid_id': request.json.get('bid_id'), } err = DeleteBidSchema().validate(data) if err: return jsonify(err), 400 user = Users.get_user(data['email'], data['password']) bid = Bids.query.filter_by( user_email=user.email, id=data['bid_id']).first() if not bid: return jsonify({'msg': "This bid was not found"}), 404 # Retrieve project to check whether project still active project = Projects.get_project(bid.project_id, 0) bid.delete() return jsonify({'msg': "Bid Deleted"}), 200