def transaction_delete(): id_ = request.form.get('id') t = Transaction.query_one({'_id': ObjectId(id_)}) # 做一次反向操作 if t.type_ == 'sell': t.type_ = 'buy' elif t.type_ == 'buy': t.type_ = 'sell' Position.do_op(t, reverse=True) t.remove() return jsonify(status=200)
def position_op(type_): try: user = current_user._id operated_at = datetime.strptime(request.form.get('operated_at'), '%Y%m%d') exchange = request.form.get('exchange') symbol = request.form.get('symbol') price = float(request.form.get('price')) quantity = int(request.form.get('quantity')) t = Transaction({ 'user': user, 'type_': type_, 'operated_at': operated_at, 'exchange': exchange, 'symbol': symbol, 'price': price, 'quantity': quantity, }) t.save() if not Position.do_op(t): t.remove() return jsonify(status=200) except Exception as e: log.exception('') return jsonify(status=500, reason=str(e))
def add_transaction(type_, exchange, symbol, price, quantity): t = Transaction({ 'user': user._id, 'type_': type_, 'operated_at': operated_at, 'exchange': exchange, 'symbol': symbol, 'price': price, 'quantity': quantity, }) t.save() if not Position.do_op(t): t.remove()