def new_message(trade_id): if not request.is_json: raise JSONExceptionHandler() content = request.get_json() text = content["body"] trade = Trades.query.get(trade_id) if trade is None: raise TradeException(trade_id, "This trade isnt exist") user_from = current_user.id if trade.user_sell == user_from: user_to = trade.user_buy elif trade.user_buy == user_from: user_to = trade.user_sell else: raise TradeException(trade_id, "This user inst related with this trade") Messages.new_msg(trade_id, user_to, user_from, text) resp = api_resp(0, "info", "Message created") return Response(json.dumps(resp), status=200, content_type='application/json')
def get_messages(trade_id): trade = Trades.query.get(trade_id) if trade is None: raise TradeException(trade_id, "This trade isnt exist") user = current_user.id if trade.user_sell != user and trade.user_buy != user: raise TradeException(trade_id, "This user inst related with this trade") messages = Messages.get_msgs(trade_id) messages_list = [] for msg in messages: item = { "nick": str(Users.get_nick(msg.user_from)), "date": str(msg.msg_date), "body": str(msg.body) } messages_list.append(item) json_messages = {"length": len(messages_list), "list": messages_list} return Response(json.dumps(json_messages), status=200, content_type='application/json')
def get_trade(id): trade = Trades.query.get(int(id)) if trade is None: raise TradeException(str(id)) if trade.user_sell != current_user.id and trade.user_buy != current_user.id: raise UserNotPermission( str(id), "This user (" + str(current_user.nick) + ") is not related with this trade") product = Products.query.get(trade.product_id) products = TradesOffers.get_prods_by_id(id) prods = [] for p in products: prods.append(str(p)) trade_json = { "id": int(id), "product_id": int(trade.product_id), "product_title": str(product.title), "seller_id": int(trade.user_sell), "buyer_id": int(trade.user_buy), "closed": bool(trade.closed_s and trade.closed_b), "closed_s": bool(trade.closed_s), "closed_b": bool(trade.closed_b), "price": float(trade.price), "last_edit": str(trade.ts_edit), "products_offer": prods } return Response(json.dumps(trade_json), status=200, content_type='application/json')
def trade_confirm(id): trade = Trades.query.get(int(id)) if trade is None: raise TradeException(str(id), "Trade not found") if trade.closed_s and trade.closed_b: raise TradeException(str(id), "The trade is already closed, no chages allowed") if current_user.id == trade.user_sell: trade.switch('s') if trade.closed_s: resp = api_resp(0, "info", "Success confirm for trade " + '(' + str(id) + ')') else: resp = api_resp( 0, "info", "Success unconfirm for trade " + '(' + str(id) + ')') elif trade.user_buy == current_user.id: trade.switch('b') if trade.closed_b: resp = api_resp(0, "info", "Success confirm for trade " + '(' + str(id) + ')') else: resp = api_resp( 0, "info", "Success unconfirm for trade " + '(' + str(id) + ')') else: raise UserNotPermission( str(id), "Tis user (" + str(current_user.nick) + ") is not related with this trade") if trade.closed_s and trade.closed_b: product = Products.query.get(trade.product_id) product.sold_me() resp = api_resp( 0, "info", "Success confirm and close for trade " + '(' + str(id) + ')') return Response(json.dumps(resp), status=200, content_type='application/json')
def trade_offer(id): if not request.is_json: raise JSONExceptionHandler() content = request.get_json() price = float(content["price"]) products = content["products"] trade = Trades.query.get(int(id)) if trade is None: raise TradeException(str(id)) if trade.closed_s and trade.closed_b: raise TradeException(str(id), "The trade is already closed, no chages allowed") if trade.closed_s or trade.closed_b: raise TradeException(str(id), "The trade is on confirm status") if trade.user_sell != current_user.id and trade.user_buy != current_user.id: raise UserNotPermission( str(id), "This user (" + str(current_user.nick) + ") is not related with this trade") # Set the price trade.set_price(price) # Add products to the offer for p in products: TradesOffers.add_product(id, p) resp = api_resp(0, "info", "Successful new offer") return Response(json.dumps(resp), status=200, content_type='application/json')
def trade_close(id): trade = Trades.query.get(int(id)) if trade is None: raise TradeException(str(id)) if trade.user_sell != current_user.id: raise UserNotPermission( str(id), "Tis user (" + str(current_user.nick) + ") is not related with this trade") Trades.delete_id(id) resp = api_resp(0, "info", "Success delete of trade " + '(' + str(id) + ')') return Response(json.dumps(resp), status=200, content_type='application/json')