def post(self): data = request.get_json(force=True)['purchase'] if 'description' not in data or 'amount' not in data or 'date' not in data or 'category_id' not in data: return make_error("Please fill out all fields") purchase = Purchase() purchase.from_dict(data) date_arr = data['date'].split('-') purchase.date = datetime.date(int(date_arr[0]), int(date_arr[1]), int(date_arr[2])) cat = Category.query.get_or_404(purchase.category_id) cat.purchases.append(purchase) db.session.commit() return purchase.to_dict(), 201
def checkout(): form = PurchaseForm() print(request.get_json()) # Get the csrf_token from the request cookie and put it into the # form manually to validate_on_submit can be used form['csrf_token'].data = request.cookies['csrf_token'] if form.validate_on_submit(): purchase = Purchase( user_id=form.user_id.data, listing_id=form.listing_id.data, quantity=form.quantity.data, ) db.session.add(purchase) db.session.commit() return {'item': purchase.to_dict()} return {'errors': validation_errors_to_error_messages(form.errors)}, 401
def create_purchase(): """ Creates a purchase. """ form = PurchaseForm() print(request.get_json()) # Get the csrf_token from the request cookie and put it into the # form manually to validate_on_submit can be used form['csrf_token'].data = request.cookies['csrf_token'] if form.validate_on_submit(): purchase = Purchase(node_id=form.data['node_id'], project_id=form.data['project_id'], num_shares=form.data['num_shares']) db.session.add(purchase) db.session.commit() return purchase.to_dict() return 'invalid form'