def seller_feed(page, product=None): if product == None: stocks = Stock.select().where(Stock.bought == False).paginate( int(page), 100) count = Stock.select().where(Stock.bought == False).count() else: stocks = Product.get(Product.name == product).in_stock.where( Stock.bought == False).paginate(int(page), 100) count = Product.get(Product.name == product).in_stock.where( Stock.bought == False).count() stocks = [{ 'id': json.dumps(str(stock.id)), 'stock': stock, 'form': forms.OrderForm() } for stock in stocks ] # this will be used for adding listings to the homepage stock_ids = [stock['id'] for stock in stocks] pagination = Pagination(page, 100, count) return render_template('admin-feed.html', stocks=stocks, current_user=current_user, Order=Order, Stock=Stock, fn=fn, int=int, pagination=pagination, page=page, os=os, list=list, HOST=HOST)
def order(name): orderform = forms.OrderForm() if orderform.main.data != 'Nothing' or orderform.side.data != 'Nothing' or orderform.drink.data != 'Nothing': order = models.Order( status='Order Received', date_in=datetime.datetime.now(), date_out=orderform.date.data, user=models.User.query.filter_by(username=name).first()) if orderform.main.data != 'Nothing': ofl = models.OrderFoodLog(food=models.Food.query.filter_by( item=orderform.main.data).first(), order=order) flash(f'{orderform.main.data} has been ordered successfully!!') if orderform.side.data != 'Nothing': ofl = models.OrderFoodLog(food=models.Food.query.filter_by( item=orderform.side.data).first(), order=order) flash(f'{orderform.side.data} has been ordered successfully!!') if orderform.drink.data != 'Nothing': ofl = models.OrderFoodLog(food=models.Food.query.filter_by( item=orderform.drink.data).first(), order=order) flash(f'{orderform.drink.data} has been ordered successfully!!') models.db.session.add(order) models.db.session.commit() return redirect(url_for('home', name=name)) else: flash(f'Sorry! You must choose atleast one item to make an order.') return redirect(url_for('home', name=name))
def order(): if 'username' not in session: return redirect(url_for('login')) form = forms.OrderForm(formdata=request.form) form.category.choices = [ (c.id, c.name) for c in Category.query.filter_by(subject_id=form.subject.data) ] if ('0', 'Select...') not in form.subject.choices: form.subject.choices.insert(0, ('0', 'Select...')) if request.method == 'POST' and form.validate(): new_order = Order() new_order.title = form.title.data new_order.user_id = session['user_id'] new_order.subject_id = form.subject.data new_order.category_id = form.category.data new_order.os_id = form.os.data new_order.details = form.details.data new_order.deadline = form.deadline.data new_order.explanations = form.explanations.data db.session.add(new_order) db.session.commit() files = request.files.getlist('file[]') order_files = [] for f in files: if f.filename: ext = f.filename.split('.')[-1] name = str(uuid.uuid4()) + '.' + ext path = os.path.join(app.config['FILE_UPLOAD_PATH'], name) f.save(path) of = File() of.order_id = new_order.id of.name = f.filename of.local_name = name order_files.append(of) for of in order_files: db.session.add(of) db.session.commit() flash('Order submitted :)', _CATEGORY_MESSAGE) return redirect(url_for('profile')) return render_template('order.html', form=form)
def charge_customer(): cid = session.get('customer_id', 'No Sesssion') print cid db.logger.info("cid is " + str(cid)) customer = db.Customer.query.filter_by(id=int(cid)).first() new_dict = create_dict(request.form) if customer: if new_dict['tos'] == 'main_sale': orderform = forms.InitialOrderForm(new_dict) if orderform.validate(): stripe_source = stripe_handlers.create_stripe_customer( customer.email, new_dict['stripeToken']) if stripe_source: customer.add_stripe_source(stripe_source) db.db.session.add(customer) db.db.session.commit() orderform = forms.OrderForm(new_dict) if orderform.validate(): #customer = db.Customer.query.filter_by(id=cid).first() #if customer: stripe_source = customer.get_source() if new_dict['recurring']: subscription_id = stripe_handlers.subscribe_customer( new_dict['name'], new_dict['amount'], stripe_source) result = True message_stripe_transaction = "Subscription placeholder" else: result, message_stripe_transaction = stripe_handlers.stripe_charge( stripe_source, new_dict['amount'], new_dict['description']) if result: # message = charge.receipt_number, if true customer.create_order( stripe_transaction=message_stripe_transaction, name=new_dict['name'], amount=new_dict['amount'], description=new_dict['description'], recurring=new_dict['recurring'], sales_type=new_dict['tos']) return jsonify({"result": 'success'}) else: return jsonify({'result': "session doesnt exist"}) else: return jsonify({'result': 'failed', 'errors': orderform.errors})
def charge_customer(): customer = db.Customer.query.filter_by(id=session['customer_id']).first() if customer: if request.form['tos'] == 'main_sale': orderform = forms.InitialOrderForm(request.form) if orderform.validate(): stripe_customer_id = stripe_handlers.create_stripe_customer( customer.id, customer.email, request.form['stripeToken']) if stripe_customer_id: customer.stripe_customer_id = stripe_customer_id db.db.session.add(customer) db.db.session.commit() orderform = forms.OrderForm(request.form) if orderform.validate(): if request.form['recurring']: subscription_id = stripe_handlers.subscribe_customer( request.form['name'], request.form['amount'], customer.stripe_customer_id) result = True message_stripe_transaction = "Subscription placeholder" else: result, message_stripe_transaction = stripe_handlers.stripe_charge( customer.stripe_customer_id, request.form['amount'], request.form['description']) if result: # message = charge.receipt_number, if true customer.create_order( stripe_transaction=message_stripe_transaction, amount=request.form['amount'], recurring=request.form['recurring'], sales_type=request.form['tos'], purchaser=customer) return jsonify({"result": 'success'}) else: return jsonify({'result': message_stripe_transaction}) else: return jsonify({'result': 'failed', 'errors': orderform.errors})
def route_cart(): is_deleted = False if request.method == "POST": # Получаем отправленные данные # name = request.form.get("name") mail = request.form.get("email") address = request.form.get("address") phone = request.form.get("phone") price = request.form.get("price") dishes = request.form.get("dishes") order = Order( #name=name, date=date.today(), mail=mail, address=address, phone=phone, price=price, status_id=0, dishes=dishes, user_id=session["user_id"]) db.session.add(order) db.session.commit() session["cart"] = [] return redirect("/ordered/") if session.get('is_deleted', False): is_deleted = True session['is_deleted'] = False return render_template('cart.html', cart=get_cart(), form=forms.OrderForm(), is_deleted=is_deleted)
def home(name): orderform = forms.OrderForm() myorderform = forms.MyOrdersForm() user_id = models.User.query.filter_by(username=name).first().id user_orders = models.Order.query.filter_by(user_id=user_id) order_ids = [o.id for o in user_orders] for o in order_ids: ofl = models.OrderFoodLog.query.filter_by(order_id=o) if ofl.count() == 3: myorderform.item.choices.insert( len(myorderform.item.choices), (o, ofl[0].food.item + ' with ' + ofl[1].food.item + ' and ' + ofl[2].food.item)) elif ofl.count() == 2: myorderform.item.choices.insert( len(myorderform.item.choices), (o, ofl[0].food.item + ' with ' + ofl[1].food.item)) elif ofl.count() == 1: myorderform.item.choices.insert(len(myorderform.item.choices), (o, ofl[0].food.item)) orderform.main.choices = [ (f.item, f.item + ' - ' + str(f.price) + ' EGP') for f in models.Food.query.filter_by(category='Main Dish') ] orderform.side.choices = [ (f.item, f.item + ' - ' + str(f.price) + ' EGP') for f in models.Food.query.filter_by(category='Side Dish') ] orderform.drink.choices = [ (f.item, f.item + ' - ' + str(f.price) + ' EGP') for f in models.Food.query.filter_by(category='Drink') ] orderform.main.choices.insert(0, ('Nothing', '-')) orderform.side.choices.insert(0, ('Nothing', '-')) orderform.drink.choices.insert(0, ('Nothing', '-')) context = { 'name': name, 'orderform': orderform, 'myorders': myorderform, 'status': ' ', 'price': ' ', 'date_out': ' ' } if myorderform.is_submitted(): if myorderform.item.data == '-': flash(f'You must choose an order to proceed!') return redirect(url_for('home', name=name)) else: if models.OrderFoodLog.query.filter_by( order_id=myorderform.item.data).first(): order_id = myorderform.item.data order = models.Order.query.filter_by(id=order_id).first() if myorderform.option.data == 'Check Status': status = 'Status: ' + order.status + ' - ' foodlog = models.OrderFoodLog.query.filter_by( order_id=order_id) if foodlog.count() == 3: total_price = foodlog[0].food.price + foodlog[ 1].food.price + foodlog[2].food.price elif foodlog.count() == 2: total_price = foodlog[0].food.price + foodlog[1].food.price elif foodlog.count() == 1: total_price = foodlog[0].food.price price = 'Total Price: ' + str(total_price) + ' EGP - ' date_out = 'Date of Arrival: ' + str(order.date_out) context['status'] = status context['price'] = price context['date_out'] = date_out elif myorderform.option.data == 'Cancel Order': foodlog = models.OrderFoodLog.query.filter_by( order_id=order_id) if foodlog.count() == 3: models.db.session.delete(foodlog[0]) models.db.session.delete(foodlog[0]) models.db.session.delete(foodlog[0]) elif foodlog.count() == 2: models.db.session.delete(foodlog[0]) models.db.session.delete(foodlog[0]) elif foodlog.count() == 1: models.db.session.delete(foodlog[0]) models.db.session.delete(order) models.db.session.commit() flash(f'Your order has been cancelled.') return redirect(url_for('home', name=name, context=context)) return render_template('home.html', **context)