<s> from re import sub <EOL> from jinja2 import contextfunction <EOL> from flask import g , request , url_for , flash <EOL> from wtforms . validators import DataRequired , InputRequired <EOL> from flask . ext . admin . _compat import urljoin , urlparse , iteritems <EOL> from . _compat import string_types <EOL> def set_current_view ( view ) : <EOL> g . _admin_view = view <EOL> def get_current_view ( ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( g , '<STR_LIT>' , None ) <EOL> def get_url ( endpoint , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> view = get_current_view ( ) <EOL> if not view : <EOL> return url_for ( endpoint , ** kwargs ) <EOL> return view . get_url ( endpoint , ** kwargs ) <EOL> def is_required_form_field ( field ) : <EOL> """<STR_LIT>""" <EOL> for validator in field . validators : <EOL> if isinstance ( validator , ( DataRequired , InputRequired ) ) : <EOL> return True <EOL> return False <EOL> def is_form_submitted ( ) : <EOL> """<STR_LIT>""" <EOL> return request and request . method in ( '<STR_LIT>' , '<STR_LIT:POST>' ) <EOL> def validate_form_on_submit ( form ) : <EOL> """<STR_LIT>""" <EOL> return is_form_submitted ( ) and form . validate ( ) <EOL> def get_form_data ( ) : <EOL> """<STR_LIT>""" <EOL> if is_form_submitted ( ) : <EOL> formdata = request . form <EOL> if request . files : <EOL> formdata = formdata . copy ( ) <EOL> formdata . update ( request . files ) <EOL> return formdata <EOL> return None <EOL> def is_field_error ( errors ) : <EOL> """<STR_LIT>""" <EOL> for e in errors : <EOL> if isinstance ( e , string_types ) : <EOL> return True <EOL> return False <EOL> def flash_errors ( form , message ) : <EOL> from flask . ext . admin . babel import gettext <EOL> for field_name , errors in iteritems ( form . errors ) : <EOL> errors = form [ field_name ] . label . text + u"<STR_LIT>" + u"<STR_LIT:U+002CU+0020>" . join ( errors ) <EOL> flash ( gettext ( message , error = str ( errors ) ) , '<STR_LIT:error>' ) <EOL> @ contextfunction <EOL> def resolve_ctx ( context ) : <EOL> """<STR_LIT>""" <EOL> g . _admin_render_ctx = context <EOL> def get_render_ctx ( ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( g , '<STR_LIT>' , None ) <EOL> def prettify_class_name ( name ) : <EOL> """<STR_LIT>""" <EOL> return sub ( r'<STR_LIT>' , r'<STR_LIT>' , name ) <EOL> def is_safe_url ( target ) : <EOL> ref_url = urlparse ( request . host_url ) <EOL> test_url = urlparse ( urljoin ( request . host_url , target ) ) <EOL> return ( test_url . scheme in ( '<STR_LIT:http>' , '<STR_LIT>' ) and <EOL> ref_url . netloc == test_url . netloc ) <EOL> def get_redirect_target ( param_name = '<STR_LIT:url>' ) : <EOL> target = request . values . get ( param_name ) <EOL> if target and is_safe_url ( target ) : <EOL> return target </s>
