def get(self): form = CouponForm() if request.args.get('status') == str(COUPON_STATUS["ALL"]): coupons = list( Information.objects(user_id=current_user.id).first().coupon) for i in range(len(coupons) - 1, -1, -1): if coupons[i].status == COUPON_STATUS[ "EXPIRED"] or Order.objects( buyer_id=current_user.id, coupon_id=coupons[i].id, status__ne=ORDER_STATUS["CANCEL"]).first() != None: del coupons[i] status = COUPON_STATUS["ALL"] else: coupons = Coupon.objects( id__nin=[ coupon.id for coupon in Information.objects( user_id=current_user.id).first().coupon ], begin_time__lte=datetime.datetime.utcnow() + datetime.timedelta(hours=8), status=COUPON_STATUS["ACTIVE"]) status = None return render_template('user/coupon/list.html', status=status, coupons=coupons, form=form, COUPON_STATUS=COUPON_STATUS)
def post(self, product_id): form = NormalForm() product = Product.objects(id=product_id, bidding=False).first() if product == None: abort(404) if form.validate_on_submit(): information = Information.objects(user_id=current_user.id).first() if form.like.data == True: if product in information.like: information.like.remove(product) like = "far fa-heart" else: information.like.append(product) like = "fas fa-heart" information.save() return like elif form.cart.data == True and product.seller_id.id != current_user.id: if product in information.cart: information.cart.remove(product) cart = "加入購物車" else: information.cart.append(product) cart = "移出購物車" information.save() return cart abort(404)
def get(self, product_id): form = BiddingForm() product = Product.objects(id=product_id, bidding=True).first() like = "far fa-heart" if product == None: abort(404) if current_user.is_active: #update history information = Information.objects(user_id=current_user.id).first() history_product = [h['product'] for h in information.history] try: index = history_product.index(product) information.history[ index].create_time = datetime.datetime.utcnow() except ValueError: information.history.append(History(product=product)) information.save() if product in information.like: like = "fas fa-heart" product.view += 1 product.save() return render_template('product/bidding.html', form=form, product=product, like=like, product_json=product.to_json(), now=datetime.datetime.utcnow() + datetime.timedelta(hours=8))
def get(self): form = CartForm() products = Information.objects(user_id=current_user.id).first().cart return render_template('user/account/cart.html', form=form, hicoin=current_user.hicoin, products=products)
def get(self): form = CartForm() products = Information.objects(user_id=current_user.id).first().cart coupons = list( Information.objects(user_id=current_user.id).first().coupon) for i in range(len(coupons) - 1, -1, -1): if coupons[i].status == COUPON_STATUS["EXPIRED"] or Order.objects( buyer_id=current_user.id, coupon_id=coupons[i].id, status__ne=ORDER_STATUS["CANCEL"]).first() != None: del coupons[i] return render_template('user/account/cart.html', form=form, hicoin=current_user.hicoin, coupons=coupons, products=products)
def validate_coupon(form, coupon_id): coupon = Coupon.objects(id=coupon_id.data, begin_time__lte=datetime.datetime.utcnow() + datetime.timedelta(hours=8), status=COUPON_STATUS["ACTIVE"]).first() if coupon == None: raise ValidationError('此優惠券不存在') elif Information.objects(user_id=current_user.id, coupon__contain=coupon).first() != None: raise ValidationError('此優惠券已兌換')
def get(self): coupons = list() temp_coupons = Information.objects( user_id=current_user.id).first().coupon now = datetime.datetime.utcnow() + datetime.timedelta(hours=8) for temp_coupon in temp_coupons: if now > temp_coupon.begin_time and now < temp_coupon.due_time and Order.objects( buyer_id=current_user.id, coupon_id=temp_coupon.id).first() == None: coupons.append(temp_coupon.to_json()) return json.dumps({'coupons': coupons})
def post(self): form = CouponForm() if form.validate_on_submit(): information = Information.objects(user_id=current_user.id).first() coupon = Coupon.objects(id=form.coupon_id.data).first() information.coupon.append(coupon) information.save() return redirect(url_for('user.coupon', status=COUPON_STATUS["ALL"])) return self.get()
def post(self, product_id): form = BiddingForm() product = Product.objects( id=product_id, status=0, bidding=True, bid__due_time__gt=datetime.datetime.utcnow() + datetime.timedelta(hours=8)).first() if product == None: abort(404) if form.validate_on_submit(): your_price = product.bid.now_price + product.bid.per_price * form.price.data if your_price > product.price: your_price = product.price if current_user != product.bid.buyer_id and current_user.hicoin < your_price: form.price.errors.append('金額不足') elif current_user == product.bid.buyer_id and current_user.hicoin < ( your_price - product.bid.now_price): form.price.errors.append('金額不足') else: if product.bid.buyer_id != None: if product.bid.buyer_id == current_user: current_user.hicoin += product.bid.now_price else: pre_buyer = User.objects( id=product.bid.buyer_id.id).first() pre_buyer.hicoin += product.bid.now_price pre_buyer.save() product.bid.buyer_id = current_user.id if your_price >= product.price: Order(buyer_id=current_user.id, product_id=product_id).save() product.status = 1 product.bid.now_price = your_price current_user.hicoin -= your_price current_user.save() product.save() like = "far fa-heart" information = Information.objects(user_id=current_user.id).first() if product in information.like: like = "fas fa-heart" return render_template('product/bidding.html', form=form, product=product, like=like, product_json=product.to_json())
def post(self): product_id = request.values['product_id'] product = Product.objects(id=product_id).first() if product == None: abort(404) information = Information.objects(user_id=current_user.id).first() for (idx, cart) in enumerate(information.cart): if product.id == cart.id: del information.cart[idx] information.save() return "加入購物車" information.cart.append(product.id) information.save() return "移出購物車" return abort(404)
def post(self, product_id): form = LikeForm() information = Information.objects(user_id=current_user.id).first() product = Product.objects(id=product_id).first() if form.validate_on_submit(): if form.like.data == True: if product in information.like: information.like.remove(product) like = "far fa-heart" else: information.like.append(product) like = "fas fa-heart" information.save() return like return "error"
def get(self, product_id): form = BiddingForm() product = Product.objects(id=product_id, bidding=True).first() categories = Category.objects(categorycontains=product.categories) orders = Order.objects(product_id__in=Product.objects( seller_id=product.seller_id)) similar_product = Product.objects( id__ne=product_id, bid__due_time__gt=datetime.datetime.utcnow() + datetime.timedelta(hours=8), bidding=True, status=0, categories__in=product.categories).order_by('-create_time')[:12] like = "far fa-heart" remove = "下架此商品" if product == None: abort(404) if current_user.is_active: #update history information = Information.objects(user_id=current_user.id).first() history_product = [h['product'] for h in information.history] try: index = history_product.index(product) information.history[ index].create_time = datetime.datetime.utcnow() except ValueError: information.history.append(History(product=product)) information.save() if product in information.like: like = "fas fa-heart" product.view += 1 product.save() return render_template('product/bidding.html', form=form, orders=orders, product=product, similar_product=similar_product, PRODUCT_STATUS=PRODUCT_STATUS, remove=remove, like=like, now=datetime.datetime.utcnow() + datetime.timedelta(hours=8))
def post(self): form = CartForm() checkedProducts = request.form.getlist("productCheck") information = Information.objects(user_id=current_user.id).first() products = Product.objects(id__in=checkedProducts, seller_id__ne=current_user.id, status=0, bidding=False) total_price = 0 coupon_dict = dict() for product in products: try: coupon = Coupon.objects( id=request.form.get(str(product.id))).first() except: coupon = None if coupon in information.coupon and Order.objects( buyer_id=current_user.id, coupon_id=coupon.id, status__ne=ORDER_STATUS["CANCEL"]).first() == None: total_price += max( 0, int(product.price * product.discount - coupon.discount)) coupon_dict[product.id] = coupon.id else: total_price += int(product.price * product.discount) coupon_dict[product.id] = None if current_user.hicoin >= total_price: current_user.hicoin -= total_price current_user.save() for product in products: Order(buyer_id=current_user.id, product_id=product.id, coupon_id=coupon_dict[product.id]).save() product.status = 1 product.save() information.cart.remove(product) information.save() return redirect(url_for('user.purchase_list', status='4'))
def post(self): form = CartForm() checkedProducts = request.form.getlist("productCheck") information = Information.objects(user_id=current_user.id).first() products = Product.objects(id__in=checkedProducts, seller_id__ne=current_user.id, status=0, bidding=False) total_price = sum(product['price'] for product in products) if current_user.hicoin >= total_price: current_user.hicoin -= total_price current_user.save() for product in products: Order(buyer_id=current_user.id, product_id=product.id).save() product.status = 1 product.save() information.cart.remove(product) information.save() return redirect(url_for('cart'))
def get(self): products = Information.objects(user_id=current_user.id).first().history products = sorted(products, key=lambda k: k.create_time, reverse=False) return render_template('user/product/history.html', products=products)
def get(self): status = request.args.get('status') products = Information.objects( user_id=current_user.id).order_by(status).first().like status = request.args.get('status') if (status == SORT_STATUS["CREATE_TIME"]): products = sorted(products, key=lambda k: k.create_time, reverse=False) elif (status == SORT_STATUS["PRICE"]): products = sorted(products, key=lambda k: k.price, reverse=False) elif (status == SORT_STATUS["HOT"]): products = sorted(products, key=lambda k: k.view, reverse=False) elif (status == SORT_STATUS["RATING"]): rating_dict = {} # user:rating product_rating_dict = {} #product:rating for user in User.objects(): seller_orders = Order.objects( product_id__in=Product.objects(seller_id=user.id), status=ORDER_STATUS["COMPLETE"]) buyer_orders = Order.objects( product_id__in=Product.objects(seller_id=user.id), status=ORDER_STATUS["COMPLETE"]) rating = 0 count_seller_orders = 0 count_buyer_orders = 0 for order in seller_orders: print(order.seller_rating) if order.seller_rating != None: rating += order.seller_rating count_seller_orders += 1 for order in buyer_orders: print(order.buyer_rating) if order.buyer_rating != None: rating += order.buyer_rating count_buyer_orders += 1 #rating += buyer_orders.buyer_rating if count_buyer_orders + count_seller_orders != 0: rating /= count_buyer_orders + count_seller_orders else: rating = 0 rating_dict[user.id] = rating for product in products: product_rating_dict[product.id] = rating_dict.get( product.seller_id) #print(rating) products = [ x for _, x in sorted(zip(product_rating_dict, products), key=lambda pair: pair[0]) ] #sorted products by product_rating_dict #sorted(rating_dict.items(), key=lambda d: d[0]) #products = sorted(products, key=lambda k: k.product.seller_id.rating, reverse=False) return render_template('user/product/like.html', products=products, PRODUCT_STATUS=PRODUCT_STATUS, SORT_STATUS=SORT_STATUS)
def get(self): products = Information.objects(user_id=current_user.id).first().like return render_template('user/product/like.html', products=products)
def post(self, product_id): form = BiddingForm() product = Product.objects( id=product_id, status=0, bidding=True, bid__due_time__gt=datetime.datetime.utcnow() + datetime.timedelta(hours=8)).first() if product == None: abort(404) if form.remove.data == True: remove = "商品已下架" product = Product.objects(id=product_id, bidding=False).first() product.status = PRODUCT_STATUS['REMOVE'] product.save() return remove if form.validate_on_submit(): your_price = product.bid.now_price + product.bid.per_price * form.price.data if your_price > product.price: your_price = product.price flash('金額過多', 'toomuch') if product.bid.per_price * form.price.data <= 0 or product.seller_id.id == current_user.id: flash('錯誤', 'error') else: if current_user != product.bid.buyer_id and current_user.hicoin < your_price: form.price.errors.append('金額不足') flash('金額不足', 'error') elif current_user == product.bid.buyer_id and current_user.hicoin < ( your_price - product.bid.now_price): form.price.errors.append('金額不足') flash('金額不足', 'error') else: if product.bid.buyer_id != None: if product.bid.buyer_id == current_user: current_user.hicoin += product.bid.now_price else: pre_buyer = User.objects( id=product.bid.buyer_id.id).first() pre_buyer.hicoin += product.bid.now_price pre_buyer.save() flash('出價成功', 'success') if product.bid.due_time - ( datetime.datetime.utcnow() + datetime.timedelta(hours=8)) < datetime.timedelta( seconds=30): product.bid.due_time += datetime.timedelta(minutes=3) product.bid.buyer_id = current_user.id if your_price >= product.price: Order(buyer_id=current_user.id, product_id=product_id).save() product.status = 1 product.bid.now_price = your_price current_user.hicoin -= your_price current_user.save() product.save() updatePriceViaSocketIO(product.id, your_price) like = "far fa-heart" information = Information.objects(user_id=current_user.id).first() if product in information.like: like = "fas fa-heart" return redirect(url_for('show_bidding', product_id=product_id))