def set_item_dict( pfx, item_dict ) : for (k,v) in item_dict.items() : try: raw_data = util.encode( v ) mc = memcache.Client( etc.memcached_addr, debug=0 ) mc.set( pfx+'_item_'+str(k), raw_data, etc.item_timeout ) except Exception as e : log.exp( e )
def delete_ss( ss_id ) : try: mc = memcache.Client( etc.memcached_addr, debug=0 ) if mc.delete( 'ss_'+ss_id, 0 ) : return True return False except Exception as e: log.exp(e) return False
def clear_all() : try: mc = memcache.Client( etc.memcached_addr, debug=0 ) if mc.flush_all() : return True return False except Exception as e : log.exp( e ) return False
def del_item( pfx, key ) : try: r = redis.Redis( connection_pool=pool ) if r.delete( pfx+'_item_'+str(key) ) : return True return False except Exception as e : log.exp( e ) return False
def set_item( pfx, key, item ) : try: raw_data = util.encode( item ) r = redis.Redis( connection_pool=pool ) if r.set( pfx+'_item_'+str(key), raw_data ) : return True return False except Exception as e : log.exp( e ) return False
def update_name( pAppid, pName) : try : session = Session() session.query( UrlItem ).filter( UrlItem.appid==pAppid ).update( {UrlItem.name:pName} , synchronize_session=False ) session.commit() #提交事务 return True except Exception as e : log.exp(e) session.rollback() return False
def update_item( pItem ) : try : session = Session() session.merge( pItem ) session.commit() #提交事务 return True except Exception as e : log.exp(e) session.rollback() return False
def update_desc( pUrlid, pDesc ) : try : session = Session() session.query( UrlItem ).filter( UrlItem.urlid==pUrlid ).update( {UrlItem.tag:pDesc} , synchronize_session=False ) session.commit() #提交事务 return True except Exception as e : log.exp(e) session.rollback() return False
def replace_ss( ss_data ) : try: raw_data = util.encode( ss_data ) mc = memcache.Client( etc.memcached_addr, debug=0 ) if mc.replace( 'ss_'+ss_data.ss_id, raw_data, etc.session_timeout ) : return True return False except Exception as e: log.exp(e) return False
def update_appid( pUrlids, pAppid ) : try : session = Session() session.query( UrlItem ).filter( UrlItem.urlid.in_(pUrlids) ).update( {UrlItem.appid:pAppid} , synchronize_session=False ) session.commit() #提交事务 return True except Exception as e : log.exp(e) session.rollback() return False
def get_item( pUrlid ) : try : session = Session() query = session.query( UrlItem ) item = query.filter_by(urlid=pUrlid).first() item.item_imgs = eval( item.item_imgs ) return item except Exception as e : log.exp(e) return None
def set_node( pfx, key, node ) : try: raw_data = util.encode( node ) mc = memcache.Client( etc.memcached_addr, debug=0 ) if mc.set( pfx+'_node_'+str(key), raw_data, etc.node_timeout ) : return True return False except Exception as e : log.exp( e ) return False
def Process( handler, *args ) : #请求前重建Session数据的过程 try : email = handler.get_argument( 'email',default=None ) passwd = handler.get_argument( 'passwd',default=None ) rememberme = handler.get_argument( 'rememberme',default=None ) log.i( 'email=%s , passwd=%s , rememberme=%s' % (email,passwd,rememberme) ) expires = None if rememberme == "on" : expires = datetime.datetime.utcnow() + datetime.timedelta(days=365) if not email or not passwd: log.w( 'empty email or passwd' ) res = { 'op':False, 'msg':'邮箱和密码不能为空' } handler.write( json.dumps(res) ) handler.finish() return email = email.strip().lower() domain = util.get_domain_from_host( handler.request.host ) #user = data_user.get_user_by_email( mongo.db_pool[domain].user, email, passwd ) user = None handler.ss_id = None if not user: log.w( 'no such user' ) res = { 'op':False, 'msg':'邮箱或密码错误' } handler.write( json.dumps(res) ) handler.finish() return if passwd != user['pw'] : log.w( 'passwd err'+' '+ user['pw'] ) res = { 'op':False, 'msg':'邮箱或密码错误' } handler.write( json.dumps(res) ) handler.finish() return if user['ss']['ssid'] : old_ss_id = str( user['ss']['ssid'] ) old_ss_data = handler.ss_store.get( old_ss_id ) if old_ss_data : log.i( "old session : uid=%s , ssid=%s" % (user['uid'],old_ss_id) ) handler.ss_id = old_ss_id if not handler.ss_id : handler.ss_id = generate_id() log.i( "new session : uid=%s , ssid=%s" % (user['uid'],handler.ss_id) ) handler.ss_id_hmac = generate_hmac( handler.ss_id ) handler.set_secure_cookie( etc.cookie_name, handler.ss_id, domain=domain, expires=expires ) handler.set_secure_cookie( etc.cookie_verify, handler.ss_id_hmac, domain=domain, expires=expires ) #data_user.set_login( mongo.db_pool[domain].user, user['uid'], handler.ss_id ) handler.ss_user = user #执行原本请求的方法 request( handler, *args ) except Exception as e : log.exp(e) res = { 'op':False, 'msg':etc.err_500 } handler.write( json.dumps(res) ) handler.finish() return
def get_persist( pfx, key ) : try: mc = memcache.Client( etc.memcached_addr, debug=0 ) raw_data = mc.get( pfx+'_persist_'+str(key) ) persist = None if raw_data : persist = util.decode( raw_data ) return persist except Exception as e : log.exp( e ) return None
def get_node( pfx, key ) : try: mc = memcache.Client( etc.memcached_addr, debug=0 ) raw_data = mc.get( pfx+'_node_'+str(key) ) node = None if raw_data : node = util.decode( raw_data ) return node except Exception as e : log.exp( e ) return None
def get_item( pfx, key ) : try: r = redis.Redis( connection_pool=pool ) raw_data = r.get( pfx+'_item_'+str(key) ) item = None if raw_data : item = util.decode( raw_data ) return item except Exception as e : log.exp( e ) return None
def get_item( pfx, key ) : try: mc = memcache.Client( etc.memcached_addr, debug=0 ) raw_data = mc.get( pfx+'_item_'+str(key) ) item = None if raw_data : item = util.decode( raw_data ) return item except Exception as e : log.exp( e ) return None
def add_item( pItem ) : try : session = Session() session.add( pItem ) #往session中添加内容 session.flush() #保存数据 session.commit() #数据库事务的提交,sisson自动过期而不需要关闭 return True except Exception as e : log.exp(e) session.rollback() return False
def get_ss( ss_id ) : try: mc = memcache.Client( etc.memcached_addr, debug=0 ) raw_data = mc.get( 'ss_'+ss_id ) ss_data = None if raw_data : ss_data = util.decode( raw_data ) return ss_data except Exception as e: log.exp(e) return None
def get( self ) : try: self.clear_all_cookies() if self.get_current_user() : domain = util.get_domain_from_host( self.request.host ) #data_user.set_logout( mongo.db_pool[domain].user, self.ss_data['uid'] ) self.ss_store.delete( self.ss_data.ss_id ) except Exception as e : log.exp( e ) finally: self.ss_data = None self.redirect(etc.url['user_home'])
def del_item( pUrlid ) : try : session = Session() query = session.query( UrlItem ) query.filter_by( urlid=pUrlid ).delete() session.flush() #保存数据 session.commit() #数据库事务的提交,sisson自动过期而不需要关闭 return True except Exception as e : log.exp(e) session.rollback() return False
def set_item_dict( pfx, item_dict ) : r = redis.Redis( connection_pool=pool ) pipe = r.pipeline() #原子pipe #pipe = r.pipeline(transaction=False) #非原子pipe for (k,v) in item_dict.items() : try: raw_data = util.encode( v ) pipe.set( pfx+'_item_'+str(k), raw_data ) except Exception as e : log.exp( e ) try: pipe.execute() except Exception as e : log.exp( e )
def del_item_dict( pfx, key_arr ) : r = redis.Redis( connection_pool=pool ) pipe = r.pipeline() #原子pipe #pipe = r.pipeline(transaction=False) #非原子pipe for key in key_arr : try: r.delete( pfx+'_item_'+str(key) ) except Exception as e : log.exp( e ) try: pipe.execute() except Exception as e : log.exp( e ) return False return True
def get_item_dict( pfx, key_arr ) : item_dict = {} if len(key_arr) <= 0 : return item_dict r = redis.Redis( connection_pool=pool ) for key in key_arr : try: raw_data = r.get( pfx+'_item_'+str(key) ) item = None if raw_data : item = util.decode( raw_data ) item_dict[key] = item except Exception as e : log.exp( e ) return item_dict
def get_item_dict( pfx, key_arr ) : item_dict = {} if len(key_arr) <= 0 : return item_dict try: mc = memcache.Client( etc.memcached_addr, debug=0 ) str_key_arr = [ pfx+'_item_'+str(i) for i in key_arr ] raw_datas = mc.get_multi( str_key_arr ) for (k,v) in raw_datas.items() : item = util.decode( v ) item_dict[long(k.replace(pfx+'_item_',''))] = item return item_dict except Exception as e : log.exp( e ) return {}
def get_item_dict( pUrlids ) : item_dict = {} try : session = Session() query = session.query( UrlItem ) items = query.filter( UrlItem.urlid.in_(pUrlids) ).all() for item in items : item.detail_url = item.detail_url.replace('\\','') tmp_imgs = eval( item.item_imgs ) for item_img in tmp_imgs['item_img'] : item_img['url'] = item_img['url'].replace('\\','') item.item_imgs = tmp_imgs item_dict[item.urlid] = item.to_item() return item_dict except Exception as e : log.exp(e) return item_dict
def Process( handler, *args ) : #请求前检验Session数据的过程 handler.get_current_user() if handler.ss_data == None : res = { 'op':False, 'msg':'使用此功能需要登录' } handler.write( json.dumps(res) ) handler.finish() return #执行原本请求的方法 try: request( handler, *args ) except Exception as e: log.exp(e) res = { 'op':False, 'msg':etc.err_500 } handler.write( json.dumps(res) ) handler.finish() return #请求完成后保存Session数据的过程 if handler.ss_data != None: handler.ss_store.replace( handler.ss_data )
def get_current_user( self ) : try: uuid = self.get_cookie( etc.cookie_uuid ) if not uuid : log.i( 'no uuid' ) expires = datetime.datetime.utcnow() + datetime.timedelta(days=365) uuid = generate_uuid() domain = util.get_domain_from_host( self.request.host ) self.set_cookie( etc.cookie_uuid, uuid, domain=domain, expires=expires ) self.uuid = uuid usr_ss_id = self.get_secure_cookie( etc.cookie_name ) usr_ss_id_hmac = self.get_secure_cookie( etc.cookie_verify ) if not usr_ss_id or not usr_ss_id_hmac : log.i( 'no cookie' ) self.clear_cookie( etc.cookie_name ) self.clear_cookie( etc.cookie_verify ) self.ss_data = None return None check_hmac = session_base.generate_hmac( usr_ss_id ) if usr_ss_id_hmac != check_hmac : log.w("evil session : %s %s"%(usr_ss_id,usr_ss_id_hmac)) self.clear_cookie( etc.cookie_name ) self.clear_cookie( etc.cookie_verify ) self.ss_data = None return None old_ss_data = self.ss_store.get( usr_ss_id ) if old_ss_data == None : log.i("session expired") self.clear_cookie( etc.cookie_name ) self.clear_cookie( etc.cookie_verify ) self.ss_data = None return None self.ss_data = old_ss_data return self.ss_data except Exception as e : log.exp(e) self.clear_cookie( etc.cookie_name ) self.clear_cookie( etc.cookie_verify ) self.ss_data = None return self.ss_data
def get( self ) : try: vid = self.get_argument( 'vid', default=None ) log.i('vid=%s' % vid) domain = util.get_domain_from_host( self.request.host ) #reset = data_user.get_reset( mongo.db_pool[domain].reset, vid ) reset = NOne if not reset: log.w('no such reset pwd') return self.redirect( etc.url['user_main'] ) else: log.i( 'verify success' ) return self.render( "user_reset_pwd.html", p_url = etc.url, p_session = self.ss_data, p_pwsecret = etc.pw_secret, p_next = None, p_email = reset['email'], p_vid = reset['vid'], ) except Exception as e : log.exp( e ) self.redirect( etc.url['user_main'] )