def post(self): addForm = CouponForm() form = CouponManageForm() coupons = Coupon.objects() status = COUPON_STATUS["ALL"] if request.args.get("status") == str(COUPON_STATUS["ACTIVE"]): coupons = Coupon.objects(status=COUPON_STATUS["ACTIVE"]) status = COUPON_STATUS["ACTIVE"] elif request.args.get("status") == str(COUPON_STATUS["NO_ACTIVE"]): coupons = Coupon.objects(status=COUPON_STATUS["NO_ACTIVE"]) status = COUPON_STATUS["NO_ACTIVE"] elif request.args.get("status") == str(COUPON_STATUS["EXPIRED"]): coupons = Coupon.objects(status=COUPON_STATUS["EXPIRED"]) status = COUPON_STATUS["EXPIRED"] if addForm.validate_on_submit(): coupon = Coupon(title=addForm.title.data, detail=addForm.detail.data, discount=addForm.discount.data, begin_time=addForm.begin_time.data, due_time=addForm.due_time.data) coupon.save() return redirect(url_for("admin.coupon")) if form.validate_on_submit(): coupon = Coupon.objects(id=form.coupon_id.data, due_time__gte=datetime.datetime.utcnow() + datetime.timedelta(hours=8)).first() if coupon == None: return "error" elif coupon.status == COUPON_STATUS["ACTIVE"]: coupon.status = COUPON_STATUS["NO_ACTIVE"] coupon.save() return "發放" elif coupon.status == COUPON_STATUS["NO_ACTIVE"]: coupon.status = COUPON_STATUS["ACTIVE"] coupon.save() return "停發" else: return "error" return render_template('admin/management/coupon.html', addForm=addForm, form=form, coupons=coupons, status=status, COUPON_STATUS=COUPON_STATUS)
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 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 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 get(self): addForm = CouponForm() form = CouponManageForm() coupons = Coupon.objects() status = COUPON_STATUS["ALL"] if request.args.get("status") == str(COUPON_STATUS["ACTIVE"]): coupons = Coupon.objects(status=COUPON_STATUS["ACTIVE"]) status = COUPON_STATUS["ACTIVE"] elif request.args.get("status") == str(COUPON_STATUS["NO_ACTIVE"]): coupons = Coupon.objects(status=COUPON_STATUS["NO_ACTIVE"]) status = COUPON_STATUS["NO_ACTIVE"] elif request.args.get("status") == str(COUPON_STATUS["EXPIRED"]): coupons = Coupon.objects(status=COUPON_STATUS["EXPIRED"]) status = COUPON_STATUS["EXPIRED"] return render_template('admin/management/coupon.html', addForm=addForm, form=form, coupons=coupons, status=status, COUPON_STATUS=COUPON_STATUS)
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 check_time(): app.config['DISCOUNT_COUNTER'] -= 1 if app.config['DISCOUNT_COUNTER'] <= 0: app.config['DISCOUNT_COUNTER'] = 3600 products = Product.objects(bidding=False, status=PRODUCT_STATUS["SELLING"]) for product in products: product.discount = 1 product.save() if len(list(products)) <= 4: for product in products: product.discount = 0.85 product.save() else: discount_list = [] while len(discount_list) < 4: products = list(products) idx = random.randint(0, len(products) - 1) if idx not in discount_list: products[idx].discount = 0.85 products[idx].save() discount_list.append(idx) products = Product.objects(status=PRODUCT_STATUS["SELLING"], bid__due_time__lte=datetime.datetime.utcnow() + datetime.timedelta(hours=8)) for product in products: if product.bid.buyer_id is None: product.status = PRODUCT_STATUS["REMOVE"] product.save() sellBiddingNtf(product, PRODUCT_STATUS["REMOVE"]) else: user = User.objects(id=product.bid.buyer_id.id).first() product.status = PRODUCT_STATUS["SOLD"] user.hicoin -= product.bid.now_price Order(buyer_id=user.id, product_id=product.id, status=ORDER_STATUS["TRANSFERING"]).save() user.save() product.save() soldProduct(product, ORDER_STATUS["TRANSFERING"]) boughtNtf(product, ORDER_STATUS["TRANSFERING"]) orders = Order.objects(status=ORDER_STATUS["RECEIPTING"], transfer_time__lte=datetime.datetime.utcnow() + datetime.timedelta(hours=-64)) for order in orders: order.finish_time = datetime.datetime.utcnow() + datetime.timedelta( hours=8) order.buyer_rating = 5 order.buyer_comment = "" order.status = ORDER_STATUS["COMPLETE"] user = User.objects(id=order.product_id.seller_id.id).first() if order.product_id.bidding is True: user.hicoin += int(order.product_id.bid.now_price * 0.88) else: user.hicoin += int(order.product_id.price * 0.88) order.save() user.save() soldProduct(order.product_id, ORDER_STATUS["COMPLETE"]) boughtNtf(order.product_id, ORDER_STATUS["COMPLETE"]) coupons = Coupon.objects(status__ne=COUPON_STATUS["EXPIRED"], due_time__lte=datetime.datetime.utcnow() + datetime.timedelta(hours=8)) for coupon in coupons: coupon.status = COUPON_STATUS["EXPIRED"] coupon.save()