def _create_cart(self): iphone_silver = Product('Iphone Silver', 'Silver', 999) iphone_black = Product('Iphone Black', 'Black', 899) cart = Cart(self._create_user()) cart.add(iphone_silver, 2) cart.add(iphone_black, 1) return cart
def add(): cart = Cart() id = request.cookies.get('cart') if id == None: id = randomString(16) response = make_response(redirect('/cart')) response.set_cookie('cart', id, max_age= 3600 * 24 * 30) else: response = redirect('/cart') productid = request.form.get('productid') quantity = request.form.get('quantity') cart.add((id, productid, quantity)) return response
class Controller: avg_rating = db_session.query( func.avg(ShopRatings.rating).label('average')).first().average cart_main = Cart() def __init__(self): init_db() def reset(self): self.cart_main = Cart() return render_template('template.html', title="Strona główna", avg_rating=self.avg_rating) def home(self): return render_template('template.html', title="Strona główna", avg_rating=self.avg_rating) def cart(self): try: orders = self.cart_main.getUnique() print(orders) ordered_products = [] for order in orders: db_product = Product.query.filter(Product.id == order).first() order_amount = self.cart_main.countProduct(order) ordered_products.append( Prod(db_product.name, order_amount, db_product.price)) sum = 0 for product in ordered_products: db_product = Product.query.filter( Product.name == product.name).first() sum = sum + db_product.price * product.amount return render_template('cart.html', products=ordered_products, title="Koszyk", price=sum, avg_rating=self.avg_rating) except KeyError: return render_template('cart.html', products=[], title="Koszyk", avg_rating=self.avg_rating) return render_template('cart.html', products=[], title="Koszyk", avg_rating=self.avg_rating) def catalog(self): categories = Category.query.all() return render_template('browse_categories.html', categories=categories, title='Kategorie', avg_rating=self.avg_rating) def order_form(self): return render_template('order_form.html', title='Formularz zamówienia', avg_rating=self.avg_rating) def order(self, request): if request.method == 'POST': print(request.form) delivery_type = request.form['delivery-type'] try: orders = self.cart_main.getUnique() ordered_products = [] for order in orders: db_product = Product.query.filter( Product.id == order).first() order_amount = self.cart_main.countProduct(order) ordered_products.append( Prod(db_product.name, order_amount, db_product.price)) sum = 0 for product in ordered_products: db_product = Product.query.filter( Product.name == product.name).first() sum = sum + db_product.price * product.amount if product.amount > db_product.amount: raise Exception("Przekroczyłeś produkty w magazynie") for product in ordered_products: db_product = Product.query.filter( Product.name == product.name).first() db_product.amount = db_product.amount - product.amount print(db_product.amount) db_session.add(db_product) db_session.commit() self.cart_main = Cart() return render_template('order_result.html', price=sum, avg_rating=self.avg_rating) except KeyError: print('error1') except Exception as e: self.cart_main = Cart() return render_template( 'template.html', title= "Nie można stworzyć zamówienia, przekroczyłeś liczbę produktów w magazynie", avg_rating=self.avg_rating) return render_template('order_form.html', title='Formularz zamówienia', avg_rating=self.avg_rating) def category(self, category_id): prod_in_category = Product.query.filter( Product.category == category_id).all() category_name = Category.query.filter( Category.id == category_id).first().name return render_template('browse_category.html', products=prod_in_category, title=f'Kategoria {category_name}', avg_rating=self.avg_rating) def product(self, product_id): product = Product.query.filter(Product.id == product_id).first() return render_template('browse_product.html', product=product, title=f'{product.name}', avg_rating=self.avg_rating) def add_to_cart(self, request): if request.method == 'POST': product_id = request.form['product_id'] amount = request.form['quantity'] product_to_cart = Product.query.filter( Product.id == product_id).first() if product_to_cart is not None: try: self.cart_main.add(product_id, amount) except KeyError: self.cart_main = Cart() self.cart_main.add(product_id, amount) return self.cart() def rate(self, request): rating = request.form.get('rate') ratingText = request.form.get('text') ratingText = None if ratingText == '' else ratingText db_session.add(ShopRatings(rating, ratingText)) db_session.commit() self.avg_rating = db_session.query( func.avg(ShopRatings.rating).label('average')).first().average return jsonify(status='success')
def handle_message(event): # 從event裡面取得用戶id user_id = event.source.user_id # 從event裡面取得reply_token reply_token = event.reply_token # 從資料庫取得送出postback的用戶資料 query = User.query.filter_by(id=user_id).first() # 從event裡面取得用戶傳來的訊息 message_text = str(event.message.text).lower() # 建立一個購物車物件 cart = Cart(user_id=user_id) # 綁webhook if reply_token == '00000000000000000000000000000000': return 'ok' # 如果正在註冊中 if query.is_signup: if check_cellphone(event.message.text): line_bot_api.push_message(user_id, TextSendMessage('恭喜註冊成功!')) line_bot_api.link_rich_menu_to_user(user_id, richmenu_list.RichMenu_ID.richmenu_04) query.is_member = True query.is_signup = False query.phone_number = event.message.text db_session.commit() return else: line_bot_api.push_message(user_id, AllMessage.error_cellphone_format()) if query.is_member: if query.edit_user_name: print('編輯使用者中') query.user_name_custom = message_text query.edit_user_name = False db_session.commit() line_bot_api.push_message(user_id, TextSendMessage(text='設定完成')) return elif query.edit_home_address: print('編輯住家中') query.home_address = message_text query.edit_home_address = False db_session.commit() line_bot_api.push_message(user_id, TextSendMessage(text='設定完成')) return elif query.edit_company_address: print('編輯公司中') query.company_address = message_text query.edit_company_address = False db_session.commit() line_bot_api.push_message(user_id, TextSendMessage(text='設定完成')) return # 編輯使用者名稱 if message_text == '編輯使用者名稱': query.edit_user_name = True query.edit_home_address = False query.edit_company_address = False db_session.commit() line_bot_api.reply_message(event.reply_token, AllMessage.edit_user_name()) return # 編輯住家 elif message_text == '編輯住家': query.edit_user_name = False query.edit_home_address = True query.edit_company_address = False db_session.commit() line_bot_api.reply_message(event.reply_token, AllMessage.edit_home_address()) return # 編輯公司 elif message_text == '編輯公司': query.edit_user_name = False query.edit_home_address = False query.edit_company_address = True db_session.commit() line_bot_api.reply_message(event.reply_token, AllMessage.edit_company_address()) return # 購物車相關 if "數量為:" in message_text: product_name = message_text.split(',')[0] num_item = message_text.rsplit(':')[1] product = db_session.query(Products).filter(Products.name.ilike(product_name)).first() if product: cart.add(product=product_name, num=num_item) confirm_template = ConfirmTemplate( text='{},數量為:{}?'.format(product_name, num_item), actions=[ PostbackAction(label='繼續選購', text='繼續選購', data='當日外帶'), PostbackAction(label='確認訂單', text='確認訂單', data='確認訂單') ]) line_bot_api.reply_message(reply_token, TemplateSendMessage(alt_text='還需要加購嗎?', template=confirm_template)) else: line_bot_api.reply_message(reply_token, TextSendMessage(text="抱歉,我們沒有賣{}".format(product_name))) # 功能列表 if message_text == "功能列表": line_bot_api.reply_message(reply_token, AllMessage.Menu_client(user_id)) return # 變成管理員的通關密碼 if message_text in ["霹靂卡霹靂拉拉波波莉娜貝貝魯多", "AI戰神"]: query.is_manager = True line_bot_api.reply_message(reply_token, TextSendMessage(text="恭喜你成為管理員了")) return
def handle_message(event): get_or_create_user(event.source.user_id) message_text = str(event.message.text).lower() cart = Cart(user_id=event.source.user_id) message = None if message_text in ["what is your story?", "story"]: message = [ ImageSendMessage( original_content_url='https://i.imgur.com/DKzbk3l.jpg', preview_image_url='https://i.imgur.com/DKzbk3l.jpg'), StickerSendMessage(package_id='11537', sticker_id='52002734') ] elif message_text in ['i am ready to order.', 'add']: message = Products.list_all() elif "i'd like to have" in message_text: product_name = message_text.split(',')[0] num_item = message_text.rsplit(':')[1] product = db_session.query(Products).filter( Products.name.ilike(product_name)).first() if product: cart.add(product=product_name, num=num_item) confirm_template = ConfirmTemplate( text='Sure, {} {}, anything else?'.format( num_item, product_name), actions=[ MessageAction(label='Add', text='add'), MessageAction(label="That's it", text="That's it") ]) message = TemplateSendMessage(alt_text='anything else?', template=confirm_template) else: message = TextSendMessage( text="Sorry, We don't have {}.".format(product_name)) print(cart.bucket()) elif message_text in ['my cart', 'cart', "that's it"]: if cart.bucket(): message = cart.display() else: message = TextSendMessage(text='Your cart is empty now.') elif message_text == 'empty cart': cart.reset() message = TextSendMessage(text='Your cart is empty now.') if message: line_bot_api.reply_message(event.reply_token, message)