<s> import werkzeug . datastructures <EOL> from jinja2 import Markup <EOL> from flask import request , session , current_app <EOL> from wtforms . fields import HiddenField <EOL> from wtforms . ext . csrf . session import SessionSecureForm <EOL> class _Auto ( ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> class Form ( SessionSecureForm ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , formdata = _Auto , obj = None , prefix = '<STR_LIT>' , csrf_context = None , <EOL> secret_key = None , csrf_enabled = None , * args , ** kwargs ) : <EOL> if csrf_enabled is None : <EOL> csrf_enabled = current_app . config . get ( '<STR_LIT>' , True ) <EOL> self . csrf_enabled = csrf_enabled <EOL> if formdata is _Auto : <EOL> if self . is_submitted ( ) : <EOL> formdata = request . form <EOL> if request . files : <EOL> formdata = formdata . copy ( ) <EOL> formdata . update ( request . files ) <EOL> elif request . json : <EOL> formdata = werkzeug . datastructures . MultiDict ( request . json ) ; <EOL> else : <EOL> formdata = None <EOL> if self . csrf_enabled : <EOL> if csrf_context is None : <EOL> csrf_context = session <EOL> if secret_key is None : <EOL> secret_key = getattr ( self , "<STR_LIT>" , None ) <EOL> if secret_key is None : <EOL> secret_key = current_app . config . get ( "<STR_LIT>" ) <EOL> if secret_key is None and session : <EOL> secret_key = session . secret_key <EOL> if secret_key is None : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> self . SECRET_KEY = secret_key <EOL> else : <EOL> csrf_context = { } <EOL> self . SECRET_KEY = "<STR_LIT>" <EOL> super ( Form , self ) . __init__ ( formdata , obj , prefix , csrf_context = csrf_context , * args , ** kwargs ) <EOL> def generate_csrf_token ( self , csrf_context = None ) : <EOL> if not self . csrf_enabled : <EOL> return None <EOL> return super ( Form , self ) . generate_csrf_token ( csrf_context ) <EOL> def validate_csrf_token ( self , field ) : <EOL> if not self . csrf_enabled : <EOL> return <EOL> super ( Form , self ) . validate_csrf_token ( field ) <EOL> def is_submitted ( self ) : <EOL> """<STR_LIT>""" <EOL> return request and request . method in ( "<STR_LIT>" , "<STR_LIT:POST>" ) <EOL> def hidden_tag ( self , * fields ) : <EOL> """<STR_LIT>""" <EOL> if not fields : <EOL> fields = [ f for f in self if isinstance ( f , HiddenField ) ] <EOL> rv = [ u'<STR_LIT>' ] <EOL> for field in fields : <EOL> if isinstance ( field , basestring ) : <EOL> field = getattr ( self , field ) <EOL> rv . append ( unicode ( field ) ) <EOL> rv . append ( u"<STR_LIT>" ) <EOL> return Markup ( u"<STR_LIT>" . join ( rv ) ) <EOL> def validate_on_submit ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . is_submitted ( ) and self . validate ( ) </s>
