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_notifications(): notifications = Notifications.list_by_user(current_user.id) notifications_list = [] for noti in notifications: product_id = str( noti.product_id) if noti.product_id is not None else "null" product_name = str(Products.get_title( noti.product_id)) if noti.product_id is not None else "null" category = str(noti.category) if noti.category is not None else "null" item = { "id": str(noti.id), "user_id": str(noti.user_id), "user_nick": str(Users.get_nick(noti.user_id)), "product_id": str(product_id), "product_name": str(product_name), "category": str(category), "date": str(noti.date), "text": str(noti.text) } notifications_list.append(item) json_notifications = { "length": len(notifications_list), "list": notifications_list } return Response(json.dumps(json_notifications), status=200, content_type='application/json')
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 search_products_advanced(): if not request.is_json: raise JSONExceptionHandler() content = request.get_json() title = content["title"] if 'title' in content else None price_min = float(content["price_min"]) if 'price_min' in content else None price_max = float(content["price_max"]) if 'price_max' in content else None place = str(content["place"]) if 'place' in content else None desc = str(content["descript"]) if 'descript' in content else None category = str(content["category"]) if 'category' in content else None user_id = current_user.id if current_user.id is not None else None products = Products.search_adv(user_id, title, price_min, price_max, place, desc, category) products_list = [] for prod in products: item = { "id": str(prod.id), "title": str(prod.title), "price": float(prod.price), "user_nick": str(Users.get_nick(prod.user_id)), "bid_date": str(prod.bid_date), "main_img": str(prod.main_img), "visits": int(prod.visits) } products_list.append(item) json_users = {"length": len(products_list), "list": products_list} return Response(json.dumps(json_users), status=200, content_type='application/json')
def search_products(): # TODO doc y mas cosas title_search = request.args.get('text') if title_search is None: raise Exception(str(title_search)) user_id = current_user.id if current_user.id is not None else None products = Products.search(user_id, title_search) products_list = [] for prod in products: item = { "id": str(prod.id), "title": str(prod.title), "price": float(prod.price), "user_nick": str(Users.get_nick(prod.user_id)), "bid_date": str(prod.bid_date), "main_img": str(prod.main_img), "visits": int(prod.visits) } products_list.append(item) json_users = {"length": len(products_list), "list": products_list} return Response(json.dumps(json_users), status=200, content_type='application/json')
def get_comments_user(id): comments = Comments.list_by_user(id) comments_list = [] for com in comments: item = { "nick": str(Users.get_nick(com.user_from)), "body": str(com.body), "id": str(com.id) } comments_list.append(item) json_comments = {"length": len(comments_list), "list": comments_list} return Response(json.dumps(json_comments), status=200, content_type='application/json')
def list_products_user(id): # TODO doc products = Products.list_by_id(id) products_list = [] for prod in products: categories = CatProducts.get_cat_names_by_prod(prod.id) cats = [] for cat in categories: cats.append(fix_str(str(cat))) images = Images.get_images_by_prod(id) imgs = [] for img in images: imgs.append(fix_str(str(img))) item = { "id": int(prod.id), "descript": str(prod.descript), "user_id": int(prod.user_id), "user_nick": str(Users.get_nick(prod.user_id)), "price": float(prod.price), "title": str(prod.title), "categories": cats, "photo_urls": imgs, "bid_date": str(prod.bid_date), "boost_date": str(prod.boost_date), "visits": int(prod.visits), "followers": int(prod.followers), "publish_date": str(prod.publish_date), "main_img": str(prod.main_img), "place": str(prod.place), "sold": str(prod.is_removed), "ban_reason": str(prod.ban_reason) } products_list.append(item) json_products = {"length": len(products_list), "list": products_list} return Response(json.dumps(json_products), status=200, content_type='application/json')
def get_prod_info(id): product = Products.query.get(int(id)) if product is None: raise ProductException(str(id), "Product not found") categories = CatProducts.get_cat_names_by_prod(id) cats = [] for cat in categories: cats.append(fix_str(str(cat))) product.increment_views() images = Images.get_images_by_prod(id) imgs = [] for img in images: imgs.append(fix_str(str(img))) product_json = { "id": int(product.id), "descript": str(product.descript), "user_id": int(product.user_id), "user_nick": str(Users.get_nick(product.user_id)), "price": float(product.price), "categories": cats, "title": str(product.title), "bid_date": str(product.bid_date), "boost_date": str(product.id), "visits": int(product.visits), "followers": int(product.followers), "publish_date": str(product.publish_date), "main_img": str(product.main_img), "photo_urls": imgs, "sold": str(product.is_removed), "place": str(product.place), "ban_reason": str(product.ban_reason) } return Response(json.dumps(product_json), status=200, content_type='application/json')
def list_products(): # TODO doc user_id = current_user.id if current_user.id is not None else None products = Products.list(user_id) products_list = [] for prod in products: categories = CatProducts.get_cat_names_by_prod(prod.id) cats = [] for cat in categories: cats.append(fix_str(str(cat))) item = { "id": int(prod.id), "descript": str(prod.descript), "user_id": int(prod.user_id), "user_nick": str(Users.get_nick(prod.user_id)), "price": float(prod.price), "title": str(prod.title), "categories": cats, "bid_date": str(prod.bid_date), "boost_date": str(prod.boost_date), "followers": int(prod.followers), "publish_date": str(prod.publish_date), "main_img": str(prod.main_img), "place": str(prod.place) } products_list.append(item) json_products = {"length": len(products_list), "list": products_list} return Response(json.dumps(json_products), status=200, content_type='application/json')