def get(self): payments = Payment.objects(payer_id=current_user.id, confirm=True) buy_orders = Order.objects(buyer_id=current_user.id, status__ne=ORDER_STATUS["CANCEL"]) sell_orders = Order.objects(product_id__in=Product.objects(seller_id=current_user.id), status=ORDER_STATUS["COMPLETE"]) records = list() for payment in payments: record = Record("儲值", payment.amount, payment.create_time) records.append(record) for order in buy_orders: if order.product_id.bidding == False: if order.coupon_id == None: record = Record("購買" + order.product_id.name, -1 * int(order.product_id.price * order.product_id.discount), order.create_time) else: record = Record("購買" + order.product_id.name, -1 * max(0, int(order.product_id.price * order.product_id.discount - order.coupon_id.discount)), order.create_time) else: record = Record("購買" + order.product_id.name, -1 * order.product_id.bid.now_price, order.create_time) records.append(record) for order in sell_orders: if order.product_id.bidding == False: record = Record("販賣" + order.product_id.name, int(order.product_id.price * RATE), order.finish_time) else: record = Record("販賣" + order.product_id.name, int(order.product_id.bid.now_price * RATE), order.finish_time) records.append(record) records = sorted(records, key=lambda k: k.time, reverse=True) return render_template('user/hicoin/record.html', records=records)
def get(self): form = OrderListForm() products = Product.objects(seller_id=current_user.id) status = request.args.get('status') if status == ORDER_STATUS['TRANSFERING']: orders = Order.objects(product_id__in=products, status=ORDER_STATUS["TRANSFERING"]) elif status == ORDER_STATUS['RECEIPTING']: orders = Order.objects(product_id__in=products, status=ORDER_STATUS["RECEIPTING"]) elif status == ORDER_STATUS['COMPLETE']: orders = Order.objects(product_id__in=products, status=ORDER_STATUS["COMPLETE"]) elif status == ORDER_STATUS['CANCEL']: orders = Order.objects(product_id__in=products, status=ORDER_STATUS["CANCEL"]) else: status = ORDER_STATUS["ALL"] orders = Order.objects(product_id__in=products) orders = sorted(orders, key=lambda k: k.create_time, reverse=False) return render_template('user/selling/order.html', orders=orders, ORDER_STATUS=ORDER_STATUS, status=status, form=form)
def order_delete(): meta = { 'title': '订单管理', 'css_nav_sub_order': 'active', 'css_nav_reptile': 'active' } ids = request.values.get('ids', '') type = request.values.get('type', 1) if not ids: return jsonify(success=False, message='缺少请求参数!') try: arr = ids.split(',') for d in arr: order = Order.objects(_id=bson.objectid.ObjectId(d)).first() order.mark_delete() if order else None except (Exception) as e: return jsonify(success=False, message=str(e)) return jsonify(success=True, message='操作成功!', data={ 'ids': ids, 'type': type }, redirect_to=url_for('admin.order_list'))
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 get(self): form = ProfileForm(name=current_user.name, birth=current_user.birth, phone=current_user.phone, store_name=current_user.store_name, icon=current_user.icon, address=current_user.address, prefer_begin_time=current_user.prefer_begin_time, prefer_end_time=current_user.prefer_end_time) seller_orders = Order.objects( product_id__in=Product.objects(seller_id=current_user.id), status=ORDER_STATUS["COMPLETE"]) buyer_orders = Order.objects( product_id__in=Product.objects(seller_id=current_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 rating = round(rating, 1) else: rating = 0 print(rating) return render_template('user/account/profile.html', form=form, rating=rating)
def post(self): form = ManagementForm() if form.validate_on_submit(): user = User.objects( id=form.user_id.data, status__in=[ACCOUNT_STATUS["ACTIVE"], ACCOUNT_STATUS["LOCK"]]).first() if user.status == ACCOUNT_STATUS["ACTIVE"]: user.status = ACCOUNT_STATUS["LOCK"] orders = Order.objects(product_id__in=Product.objects( seller_id=form.user_id.data), status__in=[ int(ORDER_STATUS["TRANSFERING"]), int(ORDER_STATUS["RECEIPTING"]) ]) for order in orders: if order.product_id.bidding == False: if order.coupon_id == None: order.buyer_id.hicoin += order.product_id.price else: order.buyer_id.hicoin += max( 0, order.product_id.price - order.coupon_id.discount) else: order.buyer_id.hicoin += order.product_id.bid.now_price order.product_id.status = PRODUCT_STATUS["REMOVE"] order.status = int(ORDER_STATUS["CANCEL"]) order.product_id.save() order.buyer_id.save() order.save() normal_products = Product.objects( seller_id=form.user_id.data, bidding=False, status__ne=PRODUCT_STATUS["FROZEN"]) for product in normal_products: product.status = PRODUCT_STATUS["REMOVE"] product.save() bidding_product = Product.objects(seller_id=form.user_id.data, bidding=True, bid__buyer_id__ne=None) for product in bidding_product: product.status = PRODUCT_STATUS["REMOVE"] if product.bid.buyer_id == None: pass else: product.bid.buyer_id.hicoin += product.bid.now_price product.save() user.save() return "解凍" elif user.status == ACCOUNT_STATUS["LOCK"]: user.status = ACCOUNT_STATUS["ACTIVE"] user.save() return "凍結" return "error"
def get(self): form = PerchaseListForm() status = request.args.get('status') if status == ORDER_STATUS['TRANSFERING']: orders = Order.objects(buyer_id=current_user.id, status=ORDER_STATUS["TRANSFERING"]) elif status == ORDER_STATUS['RECEIPTING']: orders = Order.objects(buyer_id=current_user.id, status=ORDER_STATUS["RECEIPTING"]) elif status == ORDER_STATUS['COMPLETE']: orders = Order.objects(buyer_id=current_user.id, status=ORDER_STATUS["COMPLETE"]) elif status == ORDER_STATUS['CANCEL']: orders = Order.objects(buyer_id=current_user.id, status=ORDER_STATUS["CANCEL"]) else: status = ORDER_STATUS["ALL"] orders = Order.objects(buyer_id=current_user.id) print(orders) orders = sorted(orders, key=lambda k: k.create_time, reverse=False) return render_template('user/product/list.html', orders=orders, ORDER_STATUS=ORDER_STATUS, status=status, form=form)
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 get(self): status = request.args.get('status') '''test = Order.objects.aggregate(*[ { '$lookup': { 'from': 'Product', 'localField': 'product_id', 'foreignField': 'id', 'as':'order2product', } }, ])''' if status == ORDER_STATUS['TRANSFERING']: orders = Order.objects(buyer_id=current_user.id, status=ORDER_STATUS["TRANSFERING"]) elif status == ORDER_STATUS['RECEIPTING']: orders = Order.objects(buyer_id=current_user.id, status=ORDER_STATUS["RECEIPTING"]) elif status == ORDER_STATUS['COMPLETE']: orders = Order.objects(buyer_id=current_user.id, status=ORDER_STATUS["COMPLETE"]) elif status == ORDER_STATUS['CANCEL']: orders = Order.objects(buyer_id=current_user.id, status=ORDER_STATUS["CANCEL"]) else: status = ORDER_STATUS["ALL"] orders = Order.objects(buyer_id=current_user.id) print(orders) orders = sorted(orders, key=lambda k: k.create_time, reverse=False) return render_template('user/product/list.html', orders=orders, ORDER_STATUS=ORDER_STATUS, status=status)
def order_list(): meta = { 'title': '订单管理', 'css_nav_sub_order': 'active', 'css_nav_reptile': 'active' } query = {} page = int(request.args.get('page', 1)) per_page = 20 status = int(request.args.get('status', 0)) deleted = int(request.args.get('deleted', 0)) page_url = url_for('admin.order_list', page="#p#", status=status) if status == -1: meta['css_disable'] = 'active' query['status'] = 0 elif status == 1: query['status'] = 1 meta['css_verify'] = 'active' elif status == 5: query['status'] = 5 meta['css_success'] = 'active' else: meta['css_all'] = 'active' query['deleted'] = deleted data = Order.objects(**query).order_by('create_at').paginate( page=page, per_page=per_page) total_count = Order.objects(**query).count() meta['data'] = data.items pager = Pager(page, per_page, total_count, page_url) meta['pager'] = pager.render_view() return render_template('admin/order/list.html', meta=meta)
def order_submit(): meta = { 'title': '订单管理', 'css_nav_sub_order': 'active', 'css_nav_reptile': 'active' } id = request.args.get('id', None) meta['data'] = None if id: order = Order.objects(_id=bson.objectid.ObjectId(id)).first() meta['data'] = order form = SaveForm() #current_app.logger.debug(id) return render_template('admin/order/submit.html', meta=meta, form=form)
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 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 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 = PerchaseListForm() status = request.args.get('status') if status == ORDER_STATUS['TRANSFERING']: orders = Order.objects(buyer_id=current_user.id, status=ORDER_STATUS["TRANSFERING"]) elif status == ORDER_STATUS['RECEIPTING']: orders = Order.objects(buyer_id=current_user.id, status=ORDER_STATUS["RECEIPTING"]) elif status == ORDER_STATUS['COMPLETE']: orders = Order.objects(buyer_id=current_user.id, status=ORDER_STATUS["COMPLETE"]) elif status == ORDER_STATUS['CANCEL']: orders = Order.objects(buyer_id=current_user.id, status=ORDER_STATUS["CANCEL"]) else: status = ORDER_STATUS["ALL"] orders = Order.objects(buyer_id=current_user.id) #print(orders) orders = sorted(orders, key=lambda k: k.create_time, reverse=False) if 'score' not in request.form: flash('請點選評價星星',category='error') if form.validate_on_submit() and 'score' in request.form: order = Order.objects(product_id=request.values['commentProductID'],buyer_id=current_user.id).first() if order == None: abort(404) else: if str(order.status) != ORDER_STATUS['RECEIPTING']: abort(404) else: #order implementaion order.buyer_comment = form.detail.data order.buyer_rating = request.values['score'] order.status = int(ORDER_STATUS['COMPLETE']) order.finish_time = datetime.datetime.utcnow()+datetime.timedelta(hours=8) order.save() #seller implementation seller = User.objects(id=order.product_id.seller_id.id).first() if order.product_id.bidding: seller.hicoin += order.product_id.bid.now_price*0.88 else: seller.hicoin += order.product_id.price*0.88 seller.save() print(request.values['score']) #return render_template('user/product/list.html', orders=orders, ORDER_STATUS=ORDER_STATUS, status=status, form=form) 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()
def get_orders_by_user_id(self, user_id, page, per_page): orders = Order.objects(user_id=user_id) return Pagination(orders, page, per_page).items
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 post(self): form = OrderListForm() if 'cancelOrderID' in request.form: if form.validate_on_submit: print(request.values['cancelOrderID']) order = Order.objects(id=request.values['cancelOrderID'], product_id__in=(Product.objects( seller_id=current_user.id))).first() if (order == None): abort(404) else: if str(order.status) == ORDER_STATUS['TRANSFERING']: order.status = int(ORDER_STATUS['CANCEL']) order.save() product = Product.Objects(id=order.product_id.id) product.status = PRODUCT_STATUS["REMOVE"] product.save() buyer = User.objects(id=order.buyer_id.id).first() if order.product_id.bidding: buyer.hicoin += order.product_id.bid.now_price else: buyer.hicoin += order.product_id.price buyer.save() userSellingRomovedUrl = url_for( 'user.purchase_list', status=ORDER_STATUS['CANCEL']) #userSellingRomovedUrl = app.config['REVERSE_PROXY_PATH'] + userSellingRomovedUrl message = ('<div class="d-inline"><商品名><a href="' + userSellingRomovedUrl + '">' + str(order.product_id.name) + '</a>' + " 已被取消" '</div>') if message is not None: try: sendMessageViaSocketIO( app.config['HISHOP_UID'], str(order.buyer_id.id), message) except: print("something went wrong...") else: abort(404) else: if 'score' not in request.form: flash('請點選評價星星', category='error') if form.validate_on_submit and 'score' in request.form: order = Order.objects(product_id__in=Product.objects( id=request.values['ProductID'], seller_id=current_user.id)).first() print(request.values['ProductID']) if order == None: abort(404) else: if str(order.status) != ORDER_STATUS["TRANSFERING"]: abort(404) else: order.seller_comment = form.detail.data # correct order.seller_rating = request.values[ 'score'] # correct order.status = ORDER_STATUS["RECEIPTING"] order.transfer_time = datetime.datetime.utcnow( ) + datetime.timedelta(hours=8) order.save() print(request.values['score']) products = Product.objects(seller_id=current_user.id) status = request.args.get('status') if status == ORDER_STATUS['TRANSFERING']: orders = Order.objects(product_id__in=products, status=ORDER_STATUS["TRANSFERING"]) elif status == ORDER_STATUS['RECEIPTING']: orders = Order.objects(product_id__in=products, status=ORDER_STATUS["RECEIPTING"]) elif status == ORDER_STATUS['COMPLETE']: orders = Order.objects(product_id__in=products, status=ORDER_STATUS["COMPLETE"]) elif status == ORDER_STATUS['CANCEL']: orders = Order.objects(product_id__in=products, status=ORDER_STATUS["CANCEL"]) else: status = ORDER_STATUS["ALL"] orders = Order.objects(product_id__in=products) orders = sorted(orders, key=lambda k: k.create_time, reverse=False) return render_template('user/selling/order.html', orders=orders, ORDER_STATUS=ORDER_STATUS, status=status, form=form)