def create_ad(user_id): ad_data = request.get_json(force=True, silent=True) if ad_data == None: return "Bad request", 400 ad = Ad(user_id, ad_data["title"], ad_data["desc"], ad_data["price"], ad_data["date"], None, 1, None) ad.save() return json.dumps(ad.to_dict()), 201
def update_ad(ad_id): current_user = User.find_by_email(auth.username()) ad = Ad.find(ad_id) if current_user.id == ad.creator_id: ad_data = request.get_json(force=True, silent=True) if ad_data == None: return "Bad request", 400 ad = Ad.find(ad_id) if "title" in ad_data: ad.title = ad_data["title"] if "content" in ad_data: ad.content = ad_data["content"] if "price" in ad_data: ad.price = ad_data["price"] if "release_date" in ad_data: ad.release_date = ad_data["release_date"] if "is_active" in ad_data: ad.is_active = ad_data["is_active"] if "buyer" in ad_data: ad.buyer_id = ad_data["buyer"] return jsonify(ad.save().to_dict()), 201 else: return "Permission denied", 401
def delete_ad(ad_id): current_user = User.find_by_email(auth.username()) ad = Ad.find(ad_id) if current_user.id == ad.creator_id: ad = Ad.find(ad_id) ad.delete(ad_id) return "The ad was deleted", 200 else: return "Permission denied", 401
def create_ad(): ad_data = request.get_json(force=True, silent=True) if ad_data == None: return "Bad request", 400 ad = Ad(ad_data["title"], ad_data["content"], ad_data["price"], ad_data["release_date"], ad_data["is_active"], ad_data["buyer"], ad_data["creator_id"]) ad.save() return jsonify(ad.to_dict()), 201
def create_ad_postman(): ad_data = request.get_json(force=True, silent=True) if ad_data == None: print(ad_data) return "Bad request", 400 ad = Ad(ad_data["id"], ad_data["title"], ad_data["description"], ad_data["price"], ad_data["date"], ad_data["is_active"], ad_data["owner_id"]) ad.save() return json.dumps(ad.to_dict()), 201
def delete_ad(user_id, ad_id): ad_data = request.get_json(force=True, silent=True) if ad_data == None: return "Bad request", 400 ad = Ad.find_by_id(ad_id) if ad.creator_id is not user_id: return "Forbidden", 403 Ad.delete(ad_id) return ""
def create_ad(): values = (request.form['id'], request.form['title'], request.form['description'], request.form['price'], request.form['date'], request.form['is_active'], request.form['owner_id']) Ad(*values).save() return redirect("/ads")
def buy_item(user_id, id): ad = Ad.find_by_id(id) if ad.is_active == 1: ad.is_active = 0 else: return "Another user bought this item!" ad.buyer_id = user_id return json.dumps(ad.save().to_dict())
def buy_article(user_id, ad_id): ad = Ad.find_by_id(ad_id) if ad.is_available == 0: return "Bad request", 400 ad.is_available = 0 ad.buyer = user_id return json.dumps(ad.save().to_dict())
def change_ad(user_id, ad_id): ad_data = request.get_json(force=True, silent=True) if ad_data == None: return "Bad request", 400 ad = Ad.find_by_id(ad_id) if ad.creator_id is not user_id: return "Forbidden", 403 if "title" in ad_data: ad.title = ad_data["title"] if "desc" in ad_data: ad.desc = ad_data["desc"] if "price" in ad_data: ad.price = ad_data["price"] return json.dumps(ad.save().to_dict())
def change_ad_info(id): ad_data = request.get_json(force=True, silent=True) if ad_data == None: return "Bad request", 400 ad = Ad.find_by_id(id) if "title" in ad_data: ad.title = ad_data["title"] if "description" in ad_data: ad.description = ad_data["description"] if "price" in ad_data: ad.price = ad_data["price"] if "date" in ad_data: ad.date = ad_data["date"] return json.dumps(ad.save().to_dict())
def show_ad(id): ad = Ad.find_by_id(id) return render_template("ad.html", ad=ad)
def owner_inventory(id): inventory = {"ALL ADS": []} for ads in Ad.get_all_by_owner_id(id): inventory["ALL ADS"].append(ads.to_dict()) return json.dumps(inventory)
def view_ad(ad_id): return render_template("ad.html", ad=Ad.find(ad_id))
def delete_ad(owner_id): Ad.delete(owner_id) return "Deleted succesfully"
def new_ad(): return render_template("new_ad.html", ads=Ad.get_all())
def find_ad(id): return json.dumps(Ad.find_by_id(id).to_dict())
def list_ads(): return render_template("ads.html", ads=Ad.get_all())
def buy_ad(user_id, ad_id): ad = Ad.find(ad_id) user = User.find(user_id) ad.is_active = 0 ad.buyer_id = user.name return jsonify(ad.save().to_dict())
def sold_ads(user_id): return jsonify([ ad.to_dict() for ad in Ad.all() if (ad.creator_id == int(user_id) and not ad.is_active) ])
def list_ads(): result = {"result": []} for ad in Ad.all(): result["result"].append(ad.to_dict()) return jsonify(result)
def get_ad(ad_id): return jsonify(Ad.find(ad_id).to_dict())
def add_ad(text, img, url): img_path = storage.upload_image_file(img, "ad") ad = Ad(text, img_path, url) insert(ad)
def list_purchased(user_id): return json.dumps(Ad.find_all_purchased(user_id))