def get_report(): if not current_user.is_mod: raise UserNotPermission(str(current_user.nick)) reports = Reports.list() reports_list = [] for report in reports: item = { "id": str(report.id), "user_id": str(report.user_id), "user_nick": str(Users.get_nick(report.user_id)), "reason": str(report.reason), "date": str(report.report_date), "product_id": str(report.product_id) } reports_list.append(item) json_reports = {"length": len(reports_list), "list": reports_list} return Response(json.dumps(json_reports), status=200, content_type='application/json')
def get_user(id): # TODO doc if not current_user.is_mod and False: raise UserNotPermission(str(current_user.nick)) user = Users.query.get(int(id)) if user is None: raise UserException(str(id), "User not found") # user_json = { "id": str(user.id), "nick": str(user.nick), "first_name": str(user.first_name), "last_name": str(user.last_name), "mail": str(user.mail), "is_mod": str(user.is_mod), "ban_reason": str(user.ban_reason), "points": str(user.points), "phone": str(user.phone), "desc": str(user.desc), "avatar": str(user.avatar), "fnac": str(user.fnac), "dni": str(user.dni), "place": str(user.place), "token": str(user.token) } return Response(json.dumps(user_json), status=200, content_type='application/json')
def bid_up_prod(id): if not request.is_json: raise JSONExceptionHandler() product = Products.query.get(int(id)) if product is None: raise ProductException(str(id), "Product not found") if product.user_id != current_user.id: raise UserNotPermission(str(current_user.id), "This user doesnt own this product" + str(id)) content = request.get_json() bid = datetime.datetime.strptime(content["bid_until"], "%Y-%m-%d %H:%M:%S") product.bid_set(bid) resp = api_resp( 0, "info", "Product: " + str(id) + ' (' + str(product.title) + ') ' + "set bid for " + bid.strftime("%Y-%m-%d %H:%M:%S")) return Response(json.dumps(resp), status=200, content_type='application/json')
def update_user(id): # TODO doc if not current_user.is_mod: raise UserNotPermission(str(current_user.nick)) if not request.is_json: raise JSONExceptionHandler() user = Users.query.get(int(id)) content = request.get_json() nick = content["nick"] first_name = content["first_name"] last_name = content["mail"] phone = int(content["phone"]) fnac = datetime.datetime.strptime(content["fnac"], "%Y-%m-%d") dni = int(content["dni"]) place = content["place"] mail = content["place"] desc = content["desc"] avatar = content["avatar"] is_mod = content["is_mod"] ban_reason = content["ban_reason"] token = content["token"] points = content["points"] user.update_me(nick, first_name, last_name, phone, fnac, dni, place, mail, avatar, desc, is_mod, ban_reason, token, points, None) resp = api_resp(0, "info", "User: "******"updated") return Response(json.dumps(resp), status=200, content_type='application/json')
def get_list_payments(): if not current_user.is_mod: raise UserNotPermission(str(current_user.nick)) payments = Payments.list() payments_list = [] for pay in payments: item = { "id": int(pay.id), "pay_date": str(pay.pay_date), "amount": float(pay.amount), "iban": str(pay.iban), "boost_date": str(pay.boost_date), "product_id": int(pay.product_id), } payments_list.append(item) json_products = {"length": len(payments_list), "list": payments_list} return Response(json.dumps(json_products), 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 update_prod_info(id): if not request.is_json: raise JSONExceptionHandler() product = Products.query.get(int(id)) if product is None: raise ProductException(str(id), "Product not found") if product.user_id != current_user.id: raise UserNotPermission(str(current_user.id), "This user doesnt own this product" + str(id)) content = request.get_json() title = content["title"] price = float(content["price"]) descript = content["descript"] bid = datetime.datetime.strptime(content["bid_date"], "%Y-%m-%d %H:%M:%S") if 'bid_date' in content else None categories = content["categories"] photo_urls = content["photo_urls"] place = content["place"] main_img = content["main_img"] if not isinstance(categories, list): raise JSONExceptionHandler("Bad format for categories, need an array") if not isinstance(photo_urls, list): raise JSONExceptionHandler("Bad format for photo_urls, need an array") CatProducts.delete_cats_by_prod(id) Images.delete_images_by_prod(id) for cat in categories: if len(cat) <= 1: raise ProductException(title, "Invalid categorie: " + cat) Categories.add_cat(cat) CatProducts.add_prod(cat, id) for photo in photo_urls: Images.add_photo(photo, id) # Notificaiones if product.price > price: users_ids = Follows.get_users_follow_prod(product.id) for user_id in users_ids: push_notify(user_id, "El precio del producto ha bajado! :D", int(product.id)) elif product.price < price: users_ids = Follows.get_users_follow_prod(product.id) for user_id in users_ids: push_notify(user_id, "El precio del producto ha subido :(", int(product.id)) product.update_me(title, price, descript, bid, place, main_img) resp = api_resp(0, "info", "Product: " + str(id) + ' (' + title + ') ' + "updated") return Response(json.dumps(resp), status=200, content_type='application/json')
def delete_user(id): # TODO doc if not current_user.is_mod: raise UserNotPermission(str(current_user.nick)) Users.query.get(int(id)).delete_me() resp = api_resp(0, "info", "User: "******" deleted") return Response(json.dumps(resp), status=200, content_type='application/json')
def delete_report(id): if not current_user.is_mod: raise UserNotPermission(str(current_user.nick)) Reports.delete_by_id(id) resp = api_resp(0, "info", "Report " + str(id) + "deleted") return Response(json.dumps(resp), status=200, content_type='application/json')
def delete_comment_user(id): if not current_user.is_mod: raise UserNotPermission(str(current_user.nick)) if Comments.query.get(id) is None: raise UserException(str(id), "Comment not found") Comments.delete_comment(id) resp = api_resp(0, "info", "Comment (" + str(id) + ")deleted") return Response(json.dumps(resp), status=200, content_type='application/json')
def delete_product(id): # TODO doc product = Products.query.get(int(id)) if product is None: raise ProductException(str(id), "Product not found") if product.user_id != current_user.id: raise UserNotPermission(str(current_user.id), "This user doesnt own this product" + str(id)) Products.query.get(int(id)).delete_me() resp = api_resp(0, "info", "Product: " + str(id) + " deleted") return Response(json.dumps(resp), status=200, content_type='application/json')
def payment_check(id): if not current_user.is_mod: raise UserNotPermission(str(current_user.nick)) pay = Payments.query.get(int(id)) if pay is None: raise ProductException(str(id), "Payment of product not found") Payments.query.get(int(id)).delete_me() resp = api_resp(0, "info", "Payment: " + str(id) + " deleted") return Response(json.dumps(resp), status=200, content_type='application/json')
def set_ban_prod(id): # TODO doc if not current_user.is_mod: raise UserNotPermission(str(current_user.nick)) if not request.is_json: raise JSONExceptionHandler() content = request.get_json() ban_reason = content["ban_reason"] Products.query.get(int(id)).ban_me(ban_reason) resp = api_resp(0, "info", "Product" + ' (' + str(id) + ') ' + "banned") return Response(json.dumps(resp), status=200, content_type='application/json')
def set_ban_user(id): # TODO doc if not current_user.is_mod: raise UserNotPermission(str(current_user.nick)) if not request.is_json: raise JSONExceptionHandler() content = request.get_json() ban_reason = content["ban_reason"] ban_until = datetime.datetime.strptime(content["ban_until"], "%Y-%m-%d") Users.query.get(int(id)).ban_me(ban_reason,ban_until) resp = api_resp(0, "info", "User" + ' (' + str(id) + ') ' + "banned") return Response(json.dumps(resp), 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_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')
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 bid_down_prod(id): product = Products.query.get(int(id)) if product is None: raise ProductException(str(id), "Product not found") if product.user_id != current_user.id: raise UserNotPermission(str(current_user.id), "This user doesnt own this product" + str(id)) bid = None product.bid_set(bid) resp = api_resp( 0, "info", "Product: " + str(id) + ' (' + str(product.title) + ') ' + "bid finished") return Response(json.dumps(resp), status=200, content_type='application/json')