def get(self, user): furniture_id = request.args.get('furniture_id') furniture = find_furniture_by_id(furniture_id) if furniture is None: return jsonify({ "status": 319, "msg": "Can not find the furniture" }) # Get detail from the database product_name = furniture['furniture_name'] category = furniture['category'] images = furniture['images'] price = furniture['price'] location = furniture['location'] description = furniture['description'] seller = furniture['seller'] retJson = { "status": 200, "msg": "Get furniture detail succeeded", 'furniture_name': product_name, 'category': category, 'images': images, 'price': price, 'location': location, 'description': description, 'seller': seller, } return jsonify(retJson)
def post(self, user): # Get post's json file posted_data = request.get_json() product_name = posted_data['furniture_name'] category = posted_data['category'] price = posted_data['price'] description = posted_data['description'] # Get current furniture id. furniture_id = posted_data['furniture_id'] # TODO: Change category here when updated. old_furniture = find_furniture_by_id(furniture_id) # Check if category has been changed if category != old_furniture['category']: change_category(old_furniture['category'], category, str(furniture_id)) # Update furniture by its id update_furniture_by_id( furniture_id, { "furniture_name": product_name, "category": category, # "images": images, # "is_delivery_included": is_delivery_included, "price": price, # "location": location, "description": description }) return jsonify({"status": 200, "msg": "Update/Edit succeeded"})
def post(self, user): posted_data = request.get_json() title = posted_data["title"] seller_id = posted_data["seller_id"] content = posted_data["content"] furniture_id = posted_data["furniture_id"] buyer_id = str(user['_id']) buyer_email = user['email'] buyer_username = user['username'] furniture = find_furniture_by_id(furniture_id) if furniture is None: return jsonify({ "status": 319, "msg": "Can not find the furniture" }) furniture_name = furniture["furniture_name"] seller = find_user_by_id(seller_id) if seller is None: return jsonify({"status": 312, "msg": "User doesn't exist"}) seller_email = seller["email"] seller_username = seller["username"] contact_form = add_contact_form({ "buyer": buyer_id, "buyer_username": buyer_username, "seller": seller_id, "buyer_email": buyer_email, "furniture": furniture_id, "content": content, "title": title, "furniture_name": furniture_name }) msg = Message(title, recipients=[seller_email], html=render_template( 'contact_email.html', seller_username=seller_username, buyer_username=buyer_username, buyer_email="mailto:" + buyer_email, furniture_name=furniture_name, detail_link=current_app.config['FRONTEND_DOMAIN'] + "message/" + str(contact_form.inserted_id)), sender=('Furnitrade', current_app.config['MAIL_USERNAME'])) mail.send(msg) return jsonify({ "status": 200, "msg": "Contact messge successfully send", "contact_form_id": str(contact_form.inserted_id) })
def get(self, user, furniture_id): furniture = find_furniture_by_id(furniture_id) if furniture is None: return jsonify({ "status": 319, "msg": "Can not find the furniture" }) delete_furniture_by_id(furniture_id) delete_my_furniture_by_id(user["_id"], furniture_id) return jsonify({"status": 200, "msg": "Delete succeeded"})
def get(self, user): # get user id and furniture id from param user_id = user['_id'] furniture_id = request.args.get('furniture_id') # Validation of object id if not ObjectId.is_valid(furniture_id) or \ find_furniture_by_id(furniture_id) is None: return jsonify({"status": 615, "msg": "Invalid furniture_id"}) # Use $pull operations. delete_wishlist_by_id(user_id, furniture_id) # TODO: catch and report error returned by delete. return jsonify({ "status": 200, "msg": "Furniture deleted from wishlist" })
def get(self, user): # Step 1: check if empty my_furnitures my_furnitures = user['my_furnitures'] # Step 2: query all funriture_ids to get details furnitures_json = [] for furniture_id in my_furnitures: furniture = find_furniture_by_id(furniture_id) # Error checking if not ObjectId.is_valid(furniture_id) or furniture is None: # return jsonify({ # "status": 614, # "msg": "furniture no longer available" # }) continue try: product_name = furniture['furniture_name'] category = furniture['category'] images = furniture['images'] price = furniture['price'] furnitures_json.append({ 'furniture_name': product_name, 'category': category, 'product_image': images, 'price': price, 'furniture_id': furniture_id }) except KeyError: continue if not furnitures_json: return jsonify({"status": 613, "msg": "Empty my_furnitures"}) return jsonify({ "status": 200, "msg": "get my furnitures succeeded", "result": json.dumps(furnitures_json), })
def get(self, user): # step 1: check if history is empty history = user['history'] # step 2: query all furniture_ids to get details furnitures_json = [] for furniture_id in history: furniture = find_furniture_by_id(furniture_id) if not ObjectId.is_valid(furniture_id) or furniture is None: # return jsonify({ # "status": 614, # "msg": "furniture no longer available" # }) continue try: product_name = furniture['furniture_name'] category = furniture['category'] images = furniture['images'] price = furniture['price'] furnitures_json.append({ 'furniture_name': product_name, 'category': category, 'product_image': images, 'price': price, 'furniture_id': furniture_id }) except KeyError: continue if not furnitures_json: return jsonify({"status": 613, "msg": "Empty history"}) # step 3: return json representation of furnitures return jsonify({ "status": 200, "msg": "get wishlist succeeded", "result": json.dumps(furnitures_json) })
def get(self): category_name = request.args.get('category_name') category = get_category_by_catname(category_name) if category is None: return jsonify({"status": 321, "msg": "Can not find the category"}) size = len(category['furniture_id']) count = size if size < 10 else 10 result = [] for x in range(count): furniture_id = category['furniture_id'][x] furniture = find_furniture_by_id(furniture_id) if furniture is None: continue try: product_name = furniture['furniture_name'] product_image = furniture['images'] product_price = furniture['price'] retJson = { 'furniture_name': product_name, 'product_image': product_image, 'price': product_price, 'furniture_id': furniture_id, } result.append(retJson) except KeyError: continue if not result: return jsonify({"status": 613, "msg": "Empty furniture_id"}) return jsonify({ "status": 200, "msg": "get subcategory success", "result": json.dumps(result) })
def is_furniture_id_invalid(furniture_id): # Validation of object id return (not ObjectId.is_valid(furniture_id) or find_furniture_by_id(furniture_id) is None)