def bookshelf(): '''用户书架,即已购买图书''' if request.method == 'GET': route = session.get('route') #search = request.form['search'] db = DBOpera() books = db.get_userBoughtBook(current_user.id) book_list = [] a = 1 for book in books: abook = {} abook['no'] = a abook['id'] = str(book.book_id) abook['name'] = book.book_name abook['author'] = book.book_author abook['message'] = book.book_message abook['price'] = book.book_price abook['num'] = book.book_num if book.book_image: abook['image'] = 'files/' + abook['id'] + '/' + book.book_image else: abook['image'] = '' book_list.append(abook) a = a + 1 return render_template('user_shelf.html', book_list=book_list)
def activity_update(activity_id): db = DBOpera() if request.method == 'GET': activity = db.get_activityAttach(activity_id) time = activity.activity_time.strftime("%Y-%m-%dT%H:%M:%S") return render_template('manage_activityupdate.html', activity=activity, time=time) if request.method == 'POST': activity_name = request.form['activity_name'] activity_guest = request.form['activity_guest'] activity_num = request.form['activity_num'] activity_message = request.form['activity_message'] activity_datetime = request.form['activity_datetime'] activity_num = int(activity_num) activity_id = db.update_activity(activity_id, activity_name, activity_guest, activity_num, activity_message, activity_datetime) #TODO(caoyue):when add a new activity,we send a email to user who have confirm users = db.get_confirmUser() for user in users: send_email(user.user_email, 'A new activity looks forward to your participation.', 'user/activity_update', user=user, activity_name=activity_name, activity_id=activity_id) return redirect(url_for('activity_list'))
def home(): ''' 用户主页 ''' if request.method == 'GET': db = DBOpera() # DONE(CAOYUE):最新图书 new_books = db.get_NewBook() new_book_list = [] for book in new_books: abook = {} abook['id'] = str(book.book_id) abook['name'] = book.book_name abook['author'] = book.book_author abook['message'] = book.book_message abook['price'] = book.book_price abook['num'] = book.book_num if book.book_image: abook['image'] = 'files/' + abook['id'] + '/' + book.book_image else: abook['image'] = '' new_book_list.append(abook) # DONE(CAOYUE):最新活动 new_activitys = db.get_NewActivity() now_activity_list = [] for activity in new_activitys: a_activity = {} a_activity['id'] = activity.activity_id a_activity['name'] = activity.activity_name a_activity['guest'] = activity.activity_guest a_activity['describe'] = activity.activity_describe now_activity_list.append(a_activity) # TODO(CAOYUE):最热图书 bass_sale = db.get_bassSaleBook() bassSale_book_list = [] for book in bass_sale: abook = {} abook['id'] = str(book.book_id) abook['name'] = book.book_name abook['author'] = book.book_author abook['message'] = book.book_message abook['price'] = book.book_price abook['num'] = book.book_num if book.book_image: abook['image'] = 'files/' + abook['id'] + '/' + book.book_image else: abook['image'] = '' bassSale_book_list.append(abook) try: search = request.args['search'] except: search = None if search: return redirect(url_for('book_list', search=search)) else: return render_template('user_index.html', new_book=new_book_list, new_activity=now_activity_list, baseSale_book=bassSale_book_list)
def book_cart(book_id): ''' 用户添加购物车 ''' if request.method == 'GET': db = DBOpera() db.add_cart(book_id, current_user.id) return "success"
def book_cart_delete(book_id): ''' 用户删除购物车商品 ''' if request.method == 'GET': db = DBOpera() db.delete_cart(current_user.id, book_id) return redirect(url_for('cart'))
def activity_detail(activity_id): ''' 活动详情 ''' if request.method == 'GET': db = DBOpera() activity = db.get_activityAttach(activity_id) return render_template('user_blog_post.html', activity=activity)
def book_collect_delete(book_id): ''' 用户删除收藏夹商品 ''' if request.method == 'GET': #book_id = request.form['book_id'] db = DBOpera() db.delete_collect(current_user.id, book_id) return redirect(url_for('collect'))
def activity_apply(): ''' 用户参与活动,生成添加活动详情 ''' if request.method == 'POST': activity_id = request.form['activity_id'] db = DBOpera() print activity_id, current_user.id db.add_activity_detail(activity_id, current_user.id) return redirect(url_for('activity_detail', activity_id=activity_id))
def book_add(): ''' 图书入库 ''' db = DBOpera() if request.method == 'GET': class_list = db.get_classList() return render_template('manage_newbook.html', class_list=class_list) if request.method == 'POST': book_name = request.form['book_name'] book_author = request.form['book_author'] book_price = request.form['book_price'] book_class = request.form['book_class'] book_message = request.form['book_message'] file = request.files['book_image'] book_num = request.form['book_num'] book_id = db.add_book(book_name, book_author, book_class, book_price, book_num, book_message) db.add_statics(book_id) if file and allowed_file(file.filename): if not os.path.exists(os.path.join(UPLOAD_FOLDER, str(book_id))): os.mkdir(os.path.join(UPLOAD_FOLDER, str(book_id))) buddha = str(time.time()) filename = buddha + '.' + file.filename.rsplit('.', 1)[1] book_image = os.path.join(UPLOAD_FOLDER, str(book_id), filename) fp = open(book_image, 'w') file.save(book_image) fp.close() db.add_bookImag(book_id, filename) return redirect(url_for('book_add'))
def book_update(book_id): db = DBOpera() if request.method == 'GET': book = db.get_bookAttach(book_id) class_list = db.get_classList() return render_template('manage_updatebook.html', book=book, class_list=class_list) if request.method == 'POST': book_name = request.form['book_name'] book_author = request.form['book_author'] book_price = request.form['book_price'] book_class = request.form['book_class'] book_message = request.form['book_message'] file = request.files['book_image'] book_num = request.form['book_num'] db.update_book(book_id, book_name, book_author, book_class, book_message, book_num, book_price) if file and allowed_file(file.filename): try: if not os.path.exists(os.path.join(UPLOAD_FOLDER, str(book_id))): os.mkdir(os.path.join(UPLOAD_FOLDER, str(book_id))) buddha = str(time.time()) filename = buddha + '.' + file.filename.rsplit('.', 1)[1] book_image = os.path.join(UPLOAD_FOLDER, str(book_id), filename) fp = open(book_image, 'w') file.save(book_image) fp.close() db.add_bookImag(book_id, filename) except BaseException, e: print e return redirect(url_for('book_list'))
def user_home(): ''' 用户信息展示 ''' if request.method == 'GET': db = DBOpera() integral = db.get_integral(current_user.id) grade = integral.integral_grade grade_list = [ u"布衣", u"童生", u"秀才", u"举人", u"进士", u"三甲", u"二甲", u"一甲", u"探花", u"榜眼", u"状元" ] grade_name = grade_list[grade] score = integral.integral_score return render_template('user_contact_us.html', userName=current_user.user_name, iphone=current_user.user_phone, email=current_user.user_email, grade=grade_name, score=score) if request.method == 'POST': password = request.form['password'] new_password = request.form['new_password'] email = request.form['email'] phone = request.form['phone'] manager = DBOpera() check = manager.user_check(current_user.user_name, password) if check: manager.update_user(new_password, email, phone) return redirect(url_for('user_home')) else: return "密码错误"
def activity_confirm(): ''' 验证入场码 ''' db = DBOpera() if request.method == 'GET': return render_template('manage_activityconfirm.html') if request.method == 'POST': postcode = request.form['postcode'] user_id = db.ticket_check(postcode) if user_id: db.update_integral(user_id, 50) pass #TODO(CAOYUE):验证成功,需要加成功提示在前端 else: pass #TODO(CAOYUE):验证失败,需要加失败提示在前端 return redirect(url_for('activity_confirm'))
def register(): ''' 用户注册 ''' if request.method == 'POST': username = request.form['username'] password = request.form['password'] email = request.form['email'] phone = request.form['sms'] manager = DBOpera() check = manager.user_register(username, password, email, phone) if check: manager.add_integral(check) return redirect(url_for('.login')) else: return render_template('user_register.html') if request.method == 'GET': return render_template('user_register.html')
def order_confirm(order_id): ''' 管理员,确认订单 ''' if request.method == 'GET': db = DBOpera() db.update_order_status(order_id) num = db.get_orderPrice(order_id) user_id = db.get_orderUser(order_id) num = int(num) db.update_integral(user_id, num) return redirect(url_for('order_list'))
def book_detail(book_id): ''' 图书详情 ''' if request.method == 'GET': db = DBOpera() book = db.get_bookAttach(book_id) #########################图书信息字典################################### abook = {} abook['id'] = str(book.book_id) abook['name'] = book.book_name abook['author'] = book.book_author abook['message'] = book.book_message abook['price'] = book.book_price if book.book_image: abook['image'] = 'files/' + abook['id'] + '/' + book.book_image else: abook['image'] = '' ######################################################################## evaluates = db.get_evaluate(book_id) evaluate_list = [] if evaluates: for evaluate in evaluates: evaluate_dict = {} evaluate_dict['user_name'] = evaluate.evaluate_user_id evaluate_dict['datatime'] = evaluate.evaluate_time evaluate_dict['message'] = evaluate.evaluate_describe score = int(evaluate.evaluate_score) list0 = [] list1 = [] for i in range(score): list0.append(i) for i in range(10 - score): list1.append(i) evaluate_dict['score'] = list0 evaluate_dict['no_score'] = list1 evaluate_list.append(evaluate_dict) #print evaluate_list return render_template('user_product_page.html', book=abook, evaluates=evaluate_list)
def manager_login(): ''' 管理员登陆验证 ''' if request.method == 'POST': username = request.form['username'] password = request.form['password'] manager = DBOpera() check = manager.manager_check(username, password) if check: session['username'] = username session['userid'] = check session['route'] = 'manager' session.permanent = True user = get_userinfo(check) login_user(user) return redirect(url_for('book_list')) else: return render_template('manage_login.html') if request.method == 'GET': return render_template('manage_login.html')
def activity_list(): ''' 列出所有活动 ''' if request.method == 'GET': route = session.get('route') try: search = request.args['search'] except: search = None db = DBOpera() if search: activitys = db.get_activityList(search) else: activitys = db.get_activityList() activity_list = [] a = 1 for activity in activitys: the_activity = {} the_activity['id'] = str(activity.activity_id) the_activity['name'] = activity.activity_name the_activity['guest'] = activity.activity_guest the_activity['describe'] = activity.activity_describe the_activity['time'] = activity.activity_time the_activity['count'] = activity.activity_count activity_list.append(the_activity) if not route or route == 'user': return render_template('user_blog.html', activity_list=activity_list) else: return render_template('manage_activitylist.html', activity_list=activity_list) if request.method == 'POST': route = session.get('route') #search = request.form['search'] db = DBOpera() keyword = request.form['keyword'] activitys = db.get_activityList(keyword) activity_list = [] a = 1 for activity in activitys: the_activity = {} the_activity['id'] = str(activity.activity_id) the_activity['name'] = activity.activity_name the_activity['guest'] = activity.activity_guest the_activity['describe'] = activity.activity_describe the_activity['time'] = activity.activity_time the_activity['count'] = activity.activity_count activity_list.append(the_activity) if route == 'user': return render_template('user_blog.html', activity_list=activity_list) else: return render_template('manage_activitylist.html', activity_list=activity_list)
def order_detail(order_id): '''用户订单详情''' db = DBOpera() if request.method == 'GET': books = db.get_orderAttach(order_id) list = [] for cart in books: dict = {} dict["book_id"] = int(cart[0].book_id) dict["book_name"] = cart[0].book_name dict["book_price"] = cart[0].book_price if cart[0].book_image: dict["book_image"] = 'files/' + str( dict["book_id"]) + '/' + cart[0].book_image else: dict["book_image"] = '' dict["book_num"] = int(cart[1]) list.append(dict) price = db.get_orderPrice(order_id) return render_template('user_orderAttach.html', book_list=list, price=price, order_id=order_id)
def cart(): ''' 用户购物车 ''' if request.method == 'GET': manager = DBOpera() carts = manager.get_cart(current_user.id) list = [] list1 = [] try: status = request.args['status'] except: status = 0 if status: reminder = {'message': u'成功'} else: reminder = None for cart in carts: dict = {} dict1 = {} dict["book_id"] = int(cart[0].book_id) dict1["book_id"] = int(cart[0].book_id) dict["book_name"] = cart[0].book_name dict["book_price"] = cart[0].book_price if cart[0].book_image: dict["book_image"] = 'files/' + str( dict["book_id"]) + '/' + cart[0].book_image else: dict["book_image"] = '' dict["book_num"] = int(cart[1]) dict1["book_num"] = int(cart[1]) list.append(dict) list1.append(dict1) return render_template('user_shopping_cart.html', books=list, book_id=list1, reminder=reminder)
def book_evaluate(book_id): db = DBOpera() if request.method == 'GET': if db.check_evaluate(book_id): return redirect(url_for('bookshelf')) else: return render_template('user_evaluate.html') if request.method == 'POST': scort = request.form['scort'] message = request.form['message'] db.add_evaluate(book_id, scort, message) db.update_integral(current_user.id, 5) return redirect(url_for('book_list'))
def activity_add(): ''' 活动发布 ''' db = DBOpera() if request.method == 'GET': return render_template('manage_activityAdd.html') if request.method == 'POST': activity_name = request.form['activity_name'] activity_guest = request.form['activity_guest'] activity_num = request.form['activity_num'] activity_message = request.form['activity_message'] activity_datetime = request.form['activity_datetime'] print activity_datetime activity_num = int(activity_num) activity_id = db.add_activity(activity_name, activity_guest, activity_num, activity_message, activity_datetime) #TODO(caoyue):when add a new activity,we send a email to user who have confirm users = db.get_confirmUser() for user in users: send_email(user.user_email,'A new activity looks forward to your participation.','user/activity',user=user,\ time=activity_datetime,guest=activity_guest,activity_id=activity_id) return redirect(url_for('activity_add'))
def collect(): ''' 用户收藏夹 ''' if request.method == 'GET': manager = DBOpera() books = manager.get_collect(current_user.id) book_list = [] a = 1 for book in books: abook = {} abook['no'] = a abook['id'] = str(book.book_id) abook['name'] = book.book_name abook['author'] = book.book_author abook['message'] = book.book_message abook['price'] = book.book_price abook['num'] = book.book_num if book.book_image: abook['image'] = 'files/' + abook['id'] + '/' + book.book_image else: abook['image'] = '' book_list.append(abook) return render_template('user_collect.html', collects=book_list)
def add_detail(): ''' 用户提交订单 ''' if request.method == 'POST': books_id = str(request.form['books_id']) #数据库操作生成订单,返回订单编号 books_id = books_id.replace('\'', '\"') book_id = json.loads(books_id) db = DBOpera() order = db.add_order(current_user.id) for book in book_id: order_detail = db.add_order_detail(str(order.order_id), int(book['book_id']), int(book['book_num'])) order.order_price += order_detail.orDetail_price db.add_order_price(str(order.order_id), order.order_price) db.delete_cart(current_user.id) return redirect(url_for('cart', status=1))
def order_list(): ''' 订单列表 ''' if request.method == 'GET': route = session.get('route') #search = request.form['search'] db = DBOpera() orders = db.get_orderList() return render_template('manage_orderlist.html', orders=orders) #TODO(caoyue):在图书表中查找图书 if request.method == 'POST': route = session.get('route') keyword = request.form['keyword'] db = DBOpera() orders = db.get_orderList(keyword) return render_template('manage_orderlist.html', orders=orders)
def process(): ''' 该程序用来循环执行系统操作 ''' db = DBOpera() #TODO(caoyue):确保程序持续循环运行 while 1: ############################################################################################### #DONE(caoyue):该程序块,查找所有未举行且开始时间在24h内的活动,通过活动报名人数及可容纳人数 #判断参与人员,并向参与人员发送入场码邮件 try: activitys_p = db.get_activity_query('pending') time = datetime.datetime.now() + datetime.timedelta(days=-1) if activitys_p: for activity in activitys_p: if activity.activity_time >= time: activity_list = db.get_activity_userquery( activity.activity_id) print activity_list i = activity.activity_count for attach in activity_list: # print i if i > 0: db.add_ticket(attach.actDetail_activiity_id, attach.actDetail_user_id) db.update_activity_detail( attach.actDetail_activiity_id, attach.actDetail_user_id, 'success') else: db.update_activity_detail( attach.actDetail_activiity_id, attach.actDetail_user_id, 'faile') i = i - 1 db.update_activity_status(activity.activity_id, 'runing') except BaseException, e: print 1, e #################################################################################################### #DONE(caoyue):该程序块,查找所有活动时间已过期活动,将其状态修改成完成 # 并将过期活动的未认证门票状态改为失败,为该门票这人扣除相应积分 try: activitys_r = db.get_activity_query('runing') time_now = datetime.datetime.now() if activitys_r: for activity in activitys_r: if time_now >= activity.activity_time: db.update_activity_status(activity.activity_id, 'success') db.update_ticket(activity.activity_id) except BaseException, e: print 2, e
def book_list(): ''' 图书列表 ''' if request.method == 'GET': route = session.get('route') try: search = request.args['search'] except: search = None db = DBOpera() if search: books = db.get_bookList(search) else: books = db.get_bookList() book_list = [] a = 1 #TODO(caoyue):书本信息的列表统计 for book in books: abook = {} abook['no'] = a abook['id'] = str(book.book_id) abook['name'] = book.book_name abook['author'] = book.book_author abook['message'] = book.book_message abook['price'] = book.book_price abook['num'] = book.book_num if book.book_image: abook['image'] = 'files/' + abook['id'] + '/' + book.book_image else: abook['image'] = '' book_list.append(abook) a = a + 1 if not route or route == 'user': return render_template('user_catalog_list.html', book_list=book_list) else: return render_template('manage_booklist.html', book_list=book_list, book_list1=str(book_list)) if request.method == 'POST': route = session.get('route') keyword = request.form['keyword'] db = DBOpera() books = db.get_bookList(keyword) book_list = [] a = 1 for book in books: abook = {} abook['no'] = a abook['id'] = str(book.book_id) abook['name'] = book.book_name abook['author'] = book.book_author abook['message'] = book.book_message abook['price'] = book.book_price abook['num'] = book.book_num if book.book_image: abook['image'] = 'files/' + abook['id'] + '/' + book.book_image else: abook['image'] = '' book_list.append(abook) a = a + 1 if route == 'user': return render_template('user_catalog_list.html', book_list=book_list) else: return render_template('manage_booklist.html', book_list=book_list, book_list1=str(book_list))
def user_order(): '''用户订单列表''' db = DBOpera() if request.method == 'GET': order_lists = db.get_orderList(current_user.user_name) return render_template('user_order_list.html', order_lists=order_lists)