#NEWLINE# def view_func( self ) : #NEWLINE# #INDENT# "Default view function for Flask app.\n\n        This is a simple implementation for view func, you can add it to\n        your Flask app::\n\n            weixin = Weixin(app)\n            app.add_url_rule('/', view_func=weixin.view_func)\n        " #NEWLINE# if ( request is None ) : #NEWLINE# #INDENT# raise RuntimeError ( 'view_func need Flask be installed' ) #NEWLINE# #UNINDENT# signature = request . args . get ( 'signature' ) #NEWLINE# timestamp = request . args . get ( 'timestamp' ) #NEWLINE# nonce = request . args . get ( 'nonce' ) #NEWLINE# if ( not self . validate ( signature , timestamp , nonce ) ) : #NEWLINE# #INDENT# return ( 'signature failed' , 400 ) #NEWLINE# #UNINDENT# if ( request . method == 'GET' ) : #NEWLINE# #INDENT# echostr = request . args . get ( 'echostr' , '' ) #NEWLINE# return echostr #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# ret = self . parse ( request . data ) #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# return ( 'invalid' , 400 ) #NEWLINE# #UNINDENT# if ( 'type' not in ret ) : #NEWLINE# #INDENT# return ( 'invalid' , 400 ) #NEWLINE# #UNINDENT# if ( ( ret [ 'type' ] == 'text' ) and ( ret [ 'content' ] in self . _registry ) ) : #NEWLINE# #INDENT# func = self . _registry [ ret [ 'content' ] ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ret_set = frozenset ( ret . items ( ) ) #NEWLINE# matched_rules = ( _func for ( _func , _limitation ) in self . _registry_without_key if _limitation . issubset ( ret_set ) ) #NEWLINE# func = next ( matched_rules , None ) #NEWLINE# #UNINDENT# if ( func is None ) : #NEWLINE# #INDENT# if ( '*' in self . _registry ) : #NEWLINE# #INDENT# func = self . _registry [ '*' ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# func = 'failed' #NEWLINE# #UNINDENT# #UNINDENT# if callable ( func ) : #NEWLINE# #INDENT# text = func ( ** ret ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# text = self . reply ( username = ret [ 'sender' ] , sender = ret [ 'receiver' ] , content = func ) #NEWLINE# #UNINDENT# return Response ( text , content_type = 'text/xml; charset=utf-8' )