def post(self): # 验证luosimao code = 1 msg = "success" data_json = json.loads(self.get_argument('data')) luosimao_rep = data_json.get('luosimao_rep') check_json = { 'api_key': settings.LUOSIMAO_API_KEY, 'response': luosimao_rep } check_response = requests.post(settings.LUOSIMAO_CHECK_ADDRESS, data=check_json) if json.loads(check_response.content).get('res') != 'success': code = 0 msg = u"人机验证失败" # 验证帐号密码 ret = session.query(Manager).filter( Manager.ManagerName == data_json.get('user_email'), Manager.ManagerPsd == sec_pass( data_json.get('user_pass'))).first() if not ret: code = 0 msg = u"帐号密码不正确" else: # 设置session self.session.set('UserSession', ret.to_json()) # 更新信息 update_json = { 'ManagerLastVisitTime': datetime.datetime.now(), 'ManagerLastVisitIP': self.request.remote_ip } session.query(Manager).filter(Manager.ManagerName == data_json.get( 'user_email')).update(update_json) self.write_json(msg, code=code)
def delete(self, argument): try: session.query(Comment).filter( Comment.CommentID == argument).delete() self.write_json("success", code=1) except Exception, e: self.write_json("failed", code=0)
def get(self): pid = self.get_argument('pid', None) ProductInfo = [] comment_list = [] comment_msg = "" comment_count = 0 if pid: ProductInfo = getProductByPidFirst(pid).to_json() try: suer_info = self.session['index_user'].to_json() order_info = session.query(Order).filter( Order.UserID == suer_info['UserID'], Order.ProductID == pid).first() if not order_info: comment_msg = u'未购买过该商品,不能进行评论' except Exception, e: comment_msg = u'未登录、登录后才能进行相关操作' comment = session.query(Comment).filter( Comment.ProductID == pid, Comment.Status == True).all() for com in comment: com_dict = com.to_json() UserID = int(com_dict['UserID']) try: UserName = session.query(Users).filter_by( UserID=UserID).first().to_json()['UserName'] com_dict['UserName'] = UserName except: com_dict['UserName'] = u"有缘网用户" comment_list.append(com_dict) comment_count = len(comment_list) # 商品详情下的推荐 # recommend_product = session.query(Users).all()[-8:-1] recommend_product = getRecommendProduct()
def patch(self, argument): updata_json = json.loads(self.get_argument('data')) print updata_json try: if updata_json['IsNew'] == '1': updata_json['IsNew'] = True else: updata_json['IsNew'] = False if updata_json['IsHot'] == '1': updata_json['IsHot'] = True else: updata_json['IsHot'] = False updata_json['ProductType'] = int(updata_json['ProductType']) updata_json['ProductMarketPrice'] = int( updata_json['ProductMarketPrice']) updata_json['ProductFavorablePrice'] = int( updata_json['ProductFavorablePrice']) updata_json['ProductVipPrice'] = int( updata_json['ProductVipPrice']) updata_json['ProductPoint'] = int(updata_json['ProductPoint']) updata_json['ProductCount'] = int(updata_json['ProductCount']) session.query(Product).filter( Product.ProductID == argument).update(updata_json) session.commit() self.write_json("success", code=1) except Exception, e: print e self.write_json("failed", code=0)
def delete(self, argument): try: session.query(ProductType).filter( ProductType.ProductTypeID == argument).delete() self.write_json("success", code=1) except Exception, e: self.write_json("failed", code=0)
def get(self): user_info = self.session['index_user'].to_json() data = json.loads(self.get_argument('data')) trade_no = sec_pass(str(int(time.time()))) # 交易号 # 加入订单 save_data = {"TRADE_NO": trade_no, "UserID": user_info.get('UserID'), "OrderTotalPrice": data.get('totalprice'), "OrderPayType": 1, "OrderStatus": False, "OrderSendAddress": data.get('address')} try: product_list = [] for i, x in enumerate(data.get('all_data')): # 删除购物车信息 session.query(ShopCart).filter(ShopCart.ShopCartID == x['shopcartid']).delete() # 修改商品信息 session.query(Product).filter(Product.ProductID == x['productid']).update( {"ProductBuyNum": Product.ProductBuyNum + int(x['num'])}) product_list.append(str(x['productid'])) save_data['ProductID'] = ','.join(product_list) session.add(Order(**save_data)) session.commit() except Exception, e: print(e) session.rollback()
def get(self): pid = self.get_argument('pid', None) productinfo = [] comment_list = [] comment_msg = "" comment_count = 0 recommend_product = '' if pid: productinfo = get_product_by_pid_first(pid).to_json() try: suer_info = self.session['index_user'].to_json() order_info = session.query(Order).filter( Order.UserID == suer_info['UserID'], Order.ProductID == pid).first() if not order_info: comment_msg = u'未购买过该商品,不能进行评论' except Exception, e: print(e) comment_msg = u'未登录、登录后才能进行相关操作' comment = session.query(Comment).filter( Comment.ProductID == pid, Comment.Status == True).all() for com in comment: com_dict = com.to_json() userid = int(com_dict['UserID']) try: username = session.query(Users).filter_by( UserID=userid).first().to_json()['UserName'] com_dict['UserName'] = username except Exception, e: print(e) com_dict['UserName'] = u"有缘网用户" comment_list.append(com_dict)
def delete(self, argument): try: session.query(Users).filter(Users.UserID == argument).delete() self.write_json("success", code=1) session.commit() except Exception, e: self.write_json("failed", code=0)
def delete(self, cartid): try: session.query(ShopCart).filter( ShopCart.ShopCartID == cartid).delete() session.commit() self.write_json("success", code=1) except Exception, e: self.write_json("failed", code=0)
def post(self): user_info = self.session['index_user'].to_json() pid = self.get_argument('pid') session.query(Collection).filter( Collection.ProductID == pid, Collection.UserID == user_info['UserID']).delete() session.commit() self.write_json(u"移除成功", code=1)
def patch(self, argument): updata_json = json.loads(self.get_argument('data')) try: session.query(Users).filter( Users.UserID == argument).update(updata_json) session.commit() self.write_json("success", code=1) except Exception, e: print e self.write_json("failed", code=0)
def get(self): search_info = self.get_argument('info') words = [u'%' + x + '%' for x in search_info.split('-')] product_search_result = [] rule = or_(*[Product.ProductName.like(w) for w in words]) product_search_result.extend(session.query(Product).filter(rule).all()) rule = or_(*[Product.ProductBrand.like(w) for w in words]) product_search_result.extend(session.query(Product).filter(rule).all()) product_search_result = [x.to_json() for x in product_search_result] self.render('search_list.html', product_search_result=product_search_result)
def get(self, argument): print "argument", argument if argument == 'All': data_list = [] ret = session.query(ProductType) for x in ret: data_list.append(x.to_json()) self.write_json(data=data_list) else: ret = session.query(Product).filter_by(ProductID=argument).first() data_list = ret.to_json() self.write_json(data=data_list, code=1)
def get(self): user_info = self.session['index_user'].to_json() result = [] ret = session.query(ShopCart).filter( ShopCart.UserID == user_info.get('UserID')).all() for x in ret: current_product_info = session.query(Product).filter_by( ProductID=x.ProductID).first().to_json() current_product_info['BuyNum'] = x.BuyNum current_product_info['ShopCartID'] = x.ShopCartID result.append(current_product_info) self.render('index_shopcart.html', shopcart_list=result)
def patch(self, argument): updata_json = json.loads(self.get_argument('data')) try: if updata_json['Status'] == '1': updata_json['Status'] = True else: updata_json['Status'] = False session.query(Comment).filter( Comment.CommentID == argument).update(updata_json) session.commit() self.write_json("success", code=1) except Exception, e: print e self.write_json("failed", code=0)
def get(self): try: email_address = self.get_argument('address') hash_code = self.get_argument('hash_code') ret = session.query(Users).filter_by( UserEmail=email_address).first() if ret and hash_code == ret.UserHashCode: # 激活账户 session.query(Users).filter( Users.UserEmail == email_address).update( {"UserHashCode": ""}) session.commit() self.redirect('/login/?active_status=1') except Exception, e: self.redirect('/login/?active_status=0')
def post(self): try: data = json.loads(self.get_argument('data')) if data.keys()[0] == "UserSex": data['UserSex'] = 1 if data['UserSex'] == u"男" else 0 user_info = self.session['index_user'].to_json() session.query(Users).filter( Users.UserID == user_info['UserID']).update(data) user_info = session.query(Users).filter_by( UserID=user_info['UserID']).first() self.session['index_user'] = user_info self.write_json(u"修改成功", code=1) session.commit() except Exception, e: print e self.write_json(u"修改失败", code=0)
def post(self, argument): msg = None code = None # 收藏商品 try: user_info = self.session['index_user'].to_json() ret = session.query(Collection).filter( Collection.UserID == user_info.get('UserID'), Collection.ProductID == argument).first() if ret: code = 0 msg = u"该商品已经收藏了" else: code = 1 msg = u"收藏成功" data = { "UserID": user_info.get('UserID'), "ProductID": argument } session.add(Collection(**data)) session.commit() except Exception, e: print e code = 3 msg = u"用户未登录"
def get(self): # 支付成功,改变订单状态 user_info = self.session['index_user'].to_json() out_trade_no = self.get_argument('out_trade_no', None) if out_trade_no: select_order_status = session.query(Order).filter_by( TRADE_NO=out_trade_no).first() if not select_order_status.OrderStatus: session.query(Order).filter( Order.TRADE_NO == out_trade_no).update( {"OrderStatus": True}) session.commit() # 发送邮件 content = ''' <html> <body> <p>亲爱的用户:</p> <pre> 您已经成功购买商品。我们已经收到您的订单 <a>''' + out_trade_no + '''</a> 的购买请求,商品正在发货中,请等待! 如果您并没有访问过 有缘婚恋网,或没有进行上述操作,请忽 略这封邮件。您不需要退订或进行其他进一步的操作。 </pre> <pre> </pre> </body> </html> ''' obj = redis_queue_send_email.REDIS_QUEUE() obj.send_email_via_queue(settings.SMTP_USER, user_info.get('UserEmail'), settings.WEB_NAME + "订单信息", content) order_list = session.query(Order).filter_by( UserID=user_info.get('UserID')).all() all_data = [] for i, x in enumerate(order_list): ProductID = x.to_json().get('ProductID') product_info_list = [] for id in ProductID.split(','): print "==========", id ProductInfo = session.query(Product).filter_by( ProductID=id).first().to_json() product_info_list.append(ProductInfo) all_data.append({ "order": x.to_json(), "product_info": product_info_list }) self.render('order_list.html', order_list=all_data)
def get(self): try: # 获取code code = self.get_argument('code') # 根据code获取access_token url1 = 'https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=' + settings.QQ_APP_ID + '&client_secret=' \ + settings.QQ_APPKey + '&code=' + code + '&redirect_uri=' + settings.QQ_CollBackUrl req1 = requests.get(url1) access_token = req1.content.split('&')[0].split('=')[1] # 根据access_token获取openid url2 = 'https://graph.qq.com/oauth2.0/me?access_token=' + access_token req2 = requests.get(url2) openid = json.loads( req2.content.split('(')[1].split(')')[0]).get('openid') # 根据openid获取用户信息 url3 = 'https://graph.qq.com/user/get_user_info?access_token=' + access_token + '&oauth_consumer_key=' + settings.QQ_APP_ID + '&openid=' + openid req3 = requests.get(url3) qq_user_info = json.loads(req3.content) nickname = qq_user_info.get('nickname') # 查询有没有绑定帐号 self.session['openid'] = openid self.session['nickname'] = nickname ret = session.query(LoginMap).filter_by(OpenID=openid).first() if not ret: self.redirect('/register/?status=1') # 未绑定帐号 else: # 登录 userid = ret.UserID ret = session.query(Users).filter_by(UserID=userid).first() self.session['index_user'] = ret session.query(Users).filter(Users.UserID == ret.UserID).update( { "UserLastVisitTime": datetime.datetime.now(), "UserLastVisitIP": self.request.remote_ip }) session.commit() self.redirect('/index') except Exception, e: print(e) self.redirect('/register/?status=2') # 第三方登录出现错误
def post(self): data = json.loads(self.get_argument('data')) luosimao_rep = data.get('luosimao_rep') check_json = { 'api_key': settings.LUOSIMAO_API_KEY, 'response': luosimao_rep } check_response = requests.post(settings.LUOSIMAO_CHECK_ADDRESS, data=check_json) if json.loads(check_response.content).get('res') != 'success': code = 3 msg = u"人机验证失败" self.write_json(msg, code=code) return # 验证帐号密码,帐号状态 ret = session.query(Users).filter( Users.UserEmail == data.get('Email'), Users.UserPwd == sec_pass(data.get('Pass'))).first() if not ret: code = 2 msg = u"帐号密码错误" self.write_json(msg, code=code) return if ret.UserHashCode: # 账户未激活 code = 0 msg = u"帐号未激活" self.write_json(msg, code=code) return else: code = 1 msg = u"登录成功" session.query(Users).filter(Users.UserID == ret.UserID).update({ "UserLastVisitTime": datetime.datetime.now(), "UserLastVisitIP": self.request.remote_ip }) self.session['index_user'] = ret session.commit() self.write_json(msg, code=code)
def post(self, argument): msg = '' code = '' try: user_info = self.session['index_user'].to_json() buy_num = 1 arg_data = self.get_argument('data') if arg_data and arg_data != "": argument_data = json.loads(self.get_argument('data')) buy_num = argument_data.get('buy_num') # 查询购物车中是否已经有该商品 ret = session.query(ShopCart).filter( ShopCart.UserID == user_info.get('UserID'), ShopCart.ProductID == argument).first() if ret: session.query(ShopCart).filter( ShopCart.UserID == user_info.get('UserID'), ShopCart.ProductID == argument).update( {"BuyNum": int(buy_num) + int(ret.BuyNum)}) else: if buy_num: data = { "UserID": user_info.get('UserID'), "ProductID": argument, "BuyNum": buy_num } else: data = { "UserID": user_info.get('UserID'), "ProductID": argument } session.add(ShopCart(**data)) session.commit() code = 1 msg = u"添加购物车成功" except Exception, e: print(e) code = 0 msg = u"添加购物车出错"
def get(self): try: result_list = [] user_info = self.session['index_user'].to_json() ret = session.query(DeliveryAddress).filter_by( UserID=user_info.get('UserID')).all() code = 1 for x in ret: result_list.append(x.to_json()) data = result_list except Exception, e: code = 0 data = u"收货地址获取失败"
def get(self): user_info = self.session['index_user'].to_json() wish_list = [] try: wish_all = session.query(Collection).filter_by( UserID=user_info['UserID']).all() for wish in wish_all: wish_dict = wish.to_json() Product = getProductByPidFirst(wish_dict['ProductID']) wish_dict['product'] = Product.to_json() wish_list.append(wish_dict) except Exception, e: print e pass
def get(self): code = self.get_argument('code') url = 'https://api.weibo.com/oauth2/access_token' url1 = 'https://api.weibo.com/2/users/show.json' canshu = { "client_id": "1816247821", "client_secret": "eda276ef28ae911ba030fea6bfbbc360", "grant_type": "authorization_code", "code": code, "redirect_uri": "http://test.com/check_weibo/" } re = requests.post(url, data=canshu) re_json = json.loads(re.content) re1 = requests.get(url1 + "?access_token=" + re_json.get('access_token') + "&uid=" + re_json.get('uid')) weibo_user_info = json.loads(re1.content) ret = session.query(LoginMap).filter_by( OpenID=weibo_user_info.get('id')).first() if not ret: self.session['openid'] = weibo_user_info.get('id') self.session['nickname'] = weibo_user_info.get('screen_name') self.redirect('/register/?status=1') # 未绑定帐号 else: # 登录 userid = ret.UserID ret = session.query(Users).filter_by(UserID=userid).first() session.query(Users).filter(Users.UserID == ret.UserID).update({ "UserLastVisitTime": datetime.datetime.now(), "UserLastVisitIP": self.request.remote_ip }) self.session['index_user'] = ret session.commit() self.redirect('/index')
def getComLast3Limit(): return session.query(Comment).filter_by(Status=True).all()[-4:-1] # 获取导航 -- 发起ajax请求 -- 装饰器 --- 如果redis中有 -- 直接返回 --- 如果没有 -- 查询数据库 -- 放进缓存
def getRecommendProduct(): RecommendProduct = session.query(Product).all()[-16:-1] result = [x.to_json() for x in RecommendProduct] return result
def getProductByPidFirst(pid): product = session.query(Product).filter(Product.ProductID == pid).first() return product
def getProductTypeByPid(pid): product = session.query(Product).filter(Product.ProductType == pid).all() return product
def getAllProductType_EN(): ret_list = session.query(ProductType).all() return ret_list