def market_page(): purchase_form = PurchaseItemForm() selling_form = SellItemForm() if request.method == "POST": #Purchase Item Logic purchased_item = request.form.get('purchased_item') p_item_object = Item.query.filter_by(name=purchased_item).first() if p_item_object: if current_user.can_purchase(p_item_object): p_item_object.buy(current_user) flash(f"Congratulations! You purchased {p_item_object.name} for {p_item_object.price}$", category='success') else: flash(f"Unfortunately, you don't have enough money to purchase {p_item_object.name}!", category='danger') #Sell Item Logic sold_item = request.form.get('sold_item') s_item_object = Item.query.filter_by(name=sold_item).first() if s_item_object: if current_user.can_sell(s_item_object): s_item_object.sell(current_user) flash(f"Congratulations! You sold {s_item_object.name} back to market!", category='success') else: flash(f"Something went wrong with selling {s_item_object.name}", category='danger') return redirect(url_for('market_page')) if request.method == "GET": items = Item.query.filter_by(owner=None) owned_items = Item.query.filter_by(owner=current_user.id) return render_template('market.html', items=items, purchase_form=purchase_form, owned_items=owned_items, selling_form=selling_form)
def market_page(): purchase_form = PurchaseItemForm() selling_form = SellItemForm() if request.method == "POST": #Purchase Item Logic purchased_item = request.form.get("purchased_item") #comes from form->input name from items_modals.html p_item_object = Item.query.filter_by(name=purchased_item).first() if p_item_object: if current_user.can_purchase(p_item_object): p_item_object.buy(current_user) flash(f"You have purchased {p_item_object.name} for ${p_item_object.price} Current Balance: {current_user.budget}",category='success') else: flash(f"Insufficient Balance to buy {p_item_object.name}",category="danger") #Sell Item Logic sold_item = request.form.get("sold_item") s_item_object = Item.query.filter_by(name=sold_item).first() if s_item_object: if current_user.can_sell(s_item_object): s_item_object.sell(current_user) flash(f"You have sold {s_item_object.name} for ${s_item_object.price} Current Balance: {current_user.budget}",category='success') else: flash(f"Something went wrong in selling {s_item_object.name}") return redirect(url_for('market_page')) if request.method == "GET": items = Item.query.filter_by(owner=None) owned_items = Item.query.filter_by(owner=current_user.id) return render_template('market.html',items=items,purchase_form=purchase_form,owned_items=owned_items,selling_form=selling_form)
def market(): # Initializing the forms purchase_form = PurchaseItemForm() selling_form = SellItemForm() # If the method is POST if request.method == 'POST': # Purchase Item Logic # Get item name from form purchased_item = request.form.get('purchased_item') # Find item in Database p_item_object = Item.query.filter_by(name=purchased_item).first() # Check if there exists such an object if p_item_object: # Only if user can purchase if current_user.can_purchase(p_item_object): # Call a function to set ownership of item p_item_object.setOwnership(current_user) flash( f'Congratulations! You just purchased {p_item_object.name} for ₹{p_item_object.price}', category='success') else: flash( f'Sorry! your budget is not enough to purchase {p_item_object.name}', category='danger') # Sell Item Logic # Get the item name from form sold_item = request.form.get('sold_item') # Find item in Database s_item_object = Item.query.filter_by(name=sold_item).first() # Check if there exists such an item if s_item_object: # Check if this user can sell the item if current_user.can_sell(s_item_object): s_item_object.sell(current_user) flash( f'Congratulations! You just sold {s_item_object.name} back to the market for ₹{s_item_object.price}', category='success') else: flash(f'Something went wrong with selling {s_item_object}.', category='danger') return redirect(url_for('market')) # If the request is GET if request.method == 'GET': # Only show items that have no owners items = Item.query.filter_by(owner=None) # Check if user owns anything owned_items = Item.query.filter_by(owner=current_user.id) return render_template('market.html', items=items, purchase_form=purchase_form, owned_items=owned_items, selling_form=selling_form)
def market_page(): # the next line is for the modal windows purchase_form = PurchaseItemForm() selling_form = SellItemForm() # this give me a dict with the values of the instance # print(purchase_form.__dict__) # then, I can access the elements like a dict # print(purchase_form['submit']) # I need to have imported request # print(request.form.get('purchased_item')) if request.method == 'POST': purchased_item = request.form.get('purchased_item') p_item_object = Item.query.filter_by(name=purchased_item).first() # scenarrio where the user wants to buy an item if p_item_object: # validate budget if current_user.can_purchase(p_item_object): # assign the product to the logged user # p_item_object.owner = current_user.id # current_user.budget -= p_item_object.price # db.session.commit() p_item_object.buy(current_user) flash(f"Congratulations! You purchased {p_item_object.name}", category='success') else: flash(f"Not enough fonds to buy {p_item_object.name}.", category='danger') # scenario where the user wants to sell the item sold_item = request.form.get("sold_item") s_item_object = Item.query.filter_by(name=sold_item).first() if s_item_object: if current_user.can_sell(s_item_object): s_item_object.sell(current_user) flash( f"Congratulations! You sold {s_item_object.name} back to market.", category='success') else: flash( f"Something when wrong when selling {s_item_object.name}.", category='danger') return redirect(url_for('market_page')) if request.method == 'GET': # items = Item.query.all() items = Item.query.filter_by(owner=None) owned_items = Item.query.filter_by(owner=current_user.id) return render_template('market.html', items=items, purchase_form=purchase_form, owned_items=owned_items, selling_form=selling_form)
def market_page(): purchase_form = PurchaseItemForm() selling_form = SellItemForm() if request.method == "POST": # Purchasing logic purchased_item = request.form.get('purchased_item') p_item_object = Item.query.filter_by(name=purchased_item).first() if p_item_object: if current_user.can_purchase(p_item_object): p_item_object.owner = current_user.id current_user.budget -= p_item_object.price db.session.commit() flash( "Congratulations! You have successfully purchased {} for {}$" .format(p_item_object.name, p_item_object.price), category="success") else: flash("Unfortunately you dont have budget to buy {}".format( p_item_object.name), category="danger") # Selling logic sold_item = request.form.get("sold_item") s_item_object = Item.query.filter_by(name=sold_item).first() if s_item_object: if current_user.can_sell(s_item_object): s_item_object.owner = None current_user.budget += s_item_object.price db.session.commit() flash("Congratulations! You have successfully sold {} for {}$". format(s_item_object.name, s_item_object.price), category="success") else: flash("Unfortunately cannot sell the item {}".format( s_item_object.name), category="danger") return redirect(url_for("market_page")) if request.method == "GET": items = Item.query.filter_by(owner=None) owned_items = Item.query.filter_by(owner=current_user.id) return render_template('market.html', items=items, purchase_form=purchase_form, owned_items=owned_items, selling_form=selling_form)
def market_page(): # items = [ # { 'id': 1, 'name': 'Phone', 'barcode': '8933224455697', 'price': 500}, # { 'id': 2, 'name': 'Laptop', 'barcode': '4554323322756', 'price': 900}, # { 'id': 3, 'name': 'Keyboard', 'barcode': '3242564455601', 'price': 150} # ] purchase_form = PurchaseItemForm() selling_form = SellItemForm() if request.method == 'POST': # Purchase Item Logic purchased_item = request.form.get('purchased_item') p_item_obj = Item.query.filter_by(name=purchased_item).first() if p_item_obj: if current_user.can_purchase(p_item_obj): p_item_obj.buy(current_user) flash( f"Congratulations! You purchased {p_item_obj.name} for {p_item_obj.price}$.", category='success') else: flash( f"Unfortunately, you don't have enough money to purchase {p_item_obj.name}", category='danger') # Sell Item Logic sold_item = request.form.get('sold_item') s_item_obj = Item.query.filter_by(name=sold_item).first() if s_item_obj: if current_user.can_sell(s_item_obj): s_item_obj.sell(current_user) flash( f"Congratulations! You sold {s_item_obj.name} back to market.", category='success') else: flash(f"Something went wrong with selling {s_item_obj.name}.", category='danger') return redirect(url_for('market_page')) if request.method == 'GET': # List all the items # items = Item.query.all() items = Item.query.filter_by(owner=None) owned_items = Item.query.filter_by(owner=current_user.id) return render_template('market.html', items=items, purchase_form=purchase_form, owned_items=owned_items, selling_form=selling_form)
def market_page(): purchase_form = PuchaseItemForm() selling_form = SellItemForm() if request.method == "POST": # Purchase Item Logic purchased_item = request.form.get('purchased_item') if purchased_item: p_item_obj = Item.query.filter_by(name=purchased_item).first() if p_item_obj: if current_user.can_purchase(p_item_obj): p_item_obj.buy(current_user) flash( f"Congratulations! You purchased {p_item_obj.name} for {p_item_obj.price}$", category="success") else: flash( f"Oops! You have insufficient funds to buy this item.", category="danger") else: # Sell Item Logic sold_item = request.form.get('sold_item') if sold_item: s_item_obj = Item.query.filter_by(name=sold_item).first() if s_item_obj: if current_user.can_sell(s_item_obj): s_item_obj.sell(current_user) flash( f"Congratulations! You have sold {s_item_obj.name} for {s_item_obj.price}$ to the Market", category="success") else: flash(f"Oops! You don't own this item.", category="danger") return redirect(url_for("market_page")) if request.method == 'GET': items = Item.query.filter_by(owner=None) owned_items = current_user.items # owned_items=Item.query.filter_by(owner=current_user.id) return render_template("market.html", items=items, purchase_form=purchase_form, selling_form=selling_form, owned_items=owned_items)
def market(): purchase_form = PurchaseItemForm() selling_form = SellItemForm() if request.method == 'POST': # Buying Items purchased_item = request.form.get('purchased_item') purchased_item_object = Item.query.filter_by( name=purchased_item).first() if purchased_item_object: if current_user.can_purchase(purchased_item_object): purchased_item_object.buy(current_user) flash(f"You have bought {purchased_item_object.name}!", category="success") else: flash( f"Unfortunately, you don't have enough money to buy {purchased_item_object.name}!", category="danger") #Selling Items sold_item = request.form.get('sold_item') sold_item_object = Item.query.filter_by(name=sold_item).first() if sold_item_object: if current_user.can_sell(sold_item_object): sold_item_object.sell(current_user) flash(f"You have sold {sold_item_object.name}!", category="success") else: flash("Something went wrong, item was not sold!", category="danger") return redirect(url_for('market')) else: # Show only items that no one has bought items = Item.query.filter_by(owner=None) owned_items = Item.query.filter_by(owner=current_user.id) return render_template('market.html', items=items, purchase_form=purchase_form, owned_items=owned_items, selling_form=selling_form)
def market_page(): purchase_form = PurchanseItemForm() selling_form = SellItemForm() if request.method == "POST": # Logica de compra purchased_item = request.form.get('purchased_item') p_item_object = Item.query.filter_by(name=purchased_item).first() if p_item_object: if current_user.can_purchange(p_item_object): p_item_object.buy(current_user) flash( f"Felicidades por comprar {p_item_object.name} por ${p_item_object.price}", category="success") else: flash( f"por desgracia eres pobre y no puedes comprar este producto {p_item_object.name}", category="danger") # Logica de venta sold_item = request.form.get('sold_item') s_item_object = Item.query.filter_by(name=sold_item).first() if s_item_object: if current_user.can_sell(s_item_object): s_item_object.sell(current_user) flash( f"Felicidades por vender {s_item_object.name} por ${s_item_object.price}", category="success") else: flash( f"Algo salio mal para este producto {s_item_object.name} por ${s_item_object.price}", category="danger") return redirect(url_for("market_page")) if request.method == "GET": items = Item.query.filter_by(owner=None) owned_items = Item.query.filter_by(owner=current_user.id) return render_template('market.html', items=items, purchase_form=purchase_form, owned_items=owned_items, selling_form=selling_form)
def market_page(): purchase_form = PurchaseItemForm() selling_form = SellItemForm() if request.method == "POST": #purchase item logic purchased_item = request.form.get('purchased_item') p_item_object = Item.query.filter_by(name=purchased_item).first() if p_item_object: if current_user.can_purchase(p_item_object): p_item_object.buy(current_user) flash( f"Congratulations! You purchased {p_item_object.name} for ${p_item_object.price}", category="success") else: flash("haha you broke, get some money", category="danger") #sell item logic sold_item = request.form.get('sold_item') s_item_object = Item.query.filter_by(name=sold_item).first() if s_item_object: if current_user.can_sell(s_item_object): s_item_object.sell(current_user) flash( f"Congratulations! You sold {s_item_object.name} back to market", category="success") else: flash(" Something went wrong :/", category="success") return redirect(url_for("market_page")) if request.method == "GET": items = Item.query.filter_by(owner=None) owned_items = Item.query.filter_by(owner=current_user.id) return render_template("market.html", items=items, purchase_form=purchase_form, owned_items=owned_items, selling_form=selling_form)
def market_page(): purchase_form = PurchaseItemForm() selling_form = SellItemForm() if request.method == "POST": # Purchase item purchased_item = request.form.get('purchased_item') p_item_obj = Items.query.filter_by(name=purchased_item).first() if p_item_obj: if current_user.can_purchase(p_item_obj): p_item_obj.buy(current_user) flash(f'Purchased {p_item_obj.name} for ${p_item_obj.price}', category='success') else: flash(f"You don't have enough money to buy {p_item_obj.name}!", category='danger') # Sell item sold_item = request.form.get('sold_item') s_item_obj = Items.query.filter_by(name=sold_item).first() if s_item_obj: if current_user.can_sell(s_item_obj): s_item_obj.sell(current_user) flash(f"Sold {s_item_obj.name} for ${s_item_obj.price}", category='success') else: flash(f'Something went wrong with selling {s_item_obj.name}', category='danger') return redirect(url_for('market_page')) if request.method == "GET": items = Items.query.filter_by(owner=None) owned_items = Items.query.filter_by(owner=current_user.id) return render_template('market.html', items=items, purchase_form=purchase_form, owned_items=owned_items, selling_form=selling_form)
def market_page(): selling_form = SellItemForm() purchase_form = PurchaseItemForm() if request.method == "POST": purchased_item = request.form.get("purchased-item") p_item_object = Item.query.filter_by(name=purchased_item).first() if p_item_object: if current_user.can_purchase(p_item_object): p_item_object.buy(current_user) flash( f"Congratulation! You have purchased {p_item_object.name}", category="success") else: flash( f"Unfortunately, you don't have enough money to purchase {p_item_object.name}", category="danger") sold_item = request.form.get("sold-item") s_item_object = Item.query.filter_by(name=sold_item).first() if s_item_object: if current_user.can_sell(s_item_object): s_item_object.sell(current_user) flash(f" You have sold {s_item_object.name} successfully!", category="success") else: flash( f"Something went wrong while selling this item {s_item_object.name}", category="danger") return redirect(url_for("market_page")) if request.method == "GET": items = Item.query.filter_by(owner=None) owned_items = Item.query.filter_by(owner=current_user.id) return render_template('market.html', items=items, purchase_form=purchase_form, owned_items=owned_items, selling_form=selling_form)
def market_page(): selling_form = SellItemForm() purchase_form = PurchaseItemForm() if request.method == "POST": #Purchase Item Logic purchased_item = request.form.get('purchased_item') purchased_Item_object = Item.query.filter_by( name=purchased_item).first() if purchased_Item_object: if current_user.can_purchase(purchased_Item_object): purchased_Item_object.assign_owner(current_user) flash(f'{purchased_item} purchased successfully', category='success') else: flash( f'Insufficient funds to purchase {purchased_Item_object.name}', category='danger') #Selling Item Logic sold_item = request.form.get('sold_item') sold_item_object = Item.query.filter_by(name=sold_item).first() if sold_item_object: if current_user.can_sell(sold_item_object): sold_item_object.change_owner(current_user) flash(f'{sold_item} sold successfully', category='success') else: flash( f"Something went wrong. {sold_item_object.name} can't be sold at the moment", category='danger') return redirect(url_for('market_page')) if request.method == 'GET': items = Item.query.filter_by(owner=None) owned_items = Item.query.filter_by(owner=current_user.id) return render_template('market.html', item=items, purchase_form=purchase_form, owned_items=owned_items, selling_form=selling_form)
def market_page(): # purchase fn purchase_form = PurchaseItemForm() selling_form = SellItemForm() if request.method == 'POST': purchased_item = request.form.get('purchased_item') fetch_item = Item.query.filter_by(name=purchased_item).first() if fetch_item: fetch_item.buy(current_user) flash( f"Congratulations! You bought {fetch_item.name} for ₱{fetch_item.price}", category='success') return redirect(url_for('market_page')) # Sell fn sold_item = request.form.get('sold_item') item_to_sell = Item.query.filter_by(name=sold_item).first() if item_to_sell: if current_user.can_sell(item_to_sell): item_to_sell.sell(current_user) flash( f"Congratulations! You sold {item_to_sell.name} for ₱{item_to_sell.price}", category='success') return redirect(url_for('market_page')) else: flash(f"Something went wrong with selling {item_to_sell.name}", category='danger') return redirect(url_for('market_page')) if request.method == "GET": items = Item.query.filter_by(owner=None) owned_items = Item.query.filter_by(owner=current_user.id) return render_template('market.html', items=items, purchase_form=purchase_form, owned_items=owned_items, selling_form=selling_form)