<s> import json <EOL> from flask_admin import Admin <EOL> from flask_admin . base import MenuLink <EOL> from flask_admin . contrib . peewee import ModelView <EOL> from flask_admin . contrib . peewee . form import CustomModelConverter <EOL> from flask_admin . form . widgets import DateTimePickerWidget <EOL> from playhouse . postgres_ext import ArrayField , DateTimeTZField <EOL> from wtforms import fields <EOL> from wtforms . widgets import TextInput <EOL> from redash import models <EOL> from redash . permissions import require_super_admin <EOL> class ArrayListField ( fields . Field ) : <EOL> widget = TextInput ( ) <EOL> def _value ( self ) : <EOL> if self . data : <EOL> return u'<STR_LIT:U+002CU+0020>' . join ( self . data ) <EOL> else : <EOL> return u'<STR_LIT>' <EOL> def process_formdata ( self , valuelist ) : <EOL> if valuelist : <EOL> self . data = [ x . strip ( ) for x in valuelist [ <NUM_LIT:0> ] . split ( '<STR_LIT:U+002C>' ) ] <EOL> else : <EOL> self . data = [ ] <EOL> class JSONTextAreaField ( fields . TextAreaField ) : <EOL> def process_formdata ( self , valuelist ) : <EOL> if valuelist : <EOL> try : <EOL> json . loads ( valuelist [ <NUM_LIT:0> ] ) <EOL> except ValueError : <EOL> raise ValueError ( self . gettext ( u'<STR_LIT>' ) ) <EOL> self . data = valuelist [ <NUM_LIT:0> ] <EOL> else : <EOL> self . data = '<STR_LIT>' <EOL> class PgModelConverter ( CustomModelConverter ) : <EOL> def __init__ ( self , view , additional = None ) : <EOL> additional = { ArrayField : self . handle_array_field , <EOL> DateTimeTZField : self . handle_datetime_tz_field , <EOL> models . JSONField : self . handle_json_field , <EOL> } <EOL> super ( PgModelConverter , self ) . __init__ ( view , additional ) <EOL> self . view = view <EOL> def handle_json_field ( self , model , field , ** kwargs ) : <EOL> return field . name , JSONTextAreaField ( ** kwargs ) <EOL> def handle_array_field ( self , model , field , ** kwargs ) : <EOL> return field . name , ArrayListField ( ** kwargs ) <EOL> def handle_datetime_tz_field ( self , model , field , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = DateTimePickerWidget ( ) <EOL> return field . name , fields . DateTimeField ( ** kwargs ) <EOL> class BaseModelView ( ModelView ) : <EOL> column_display_pk = True <EOL> model_form_converter = PgModelConverter <EOL> @ require_super_admin <EOL> def is_accessible ( self ) : <EOL> return True <EOL> class QueryResultModelView ( BaseModelView ) : <EOL> column_exclude_list = ( '<STR_LIT:data>' , ) <EOL> class QueryModelView ( BaseModelView ) : <EOL> column_exclude_list = ( '<STR_LIT>' , ) <EOL> class DashboardModelView ( BaseModelView ) : <EOL> column_searchable_list = ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> def init_admin ( app ) : <EOL> admin = Admin ( app , name = '<STR_LIT>' , template_mode = '<STR_LIT>' ) <EOL> admin . add_view ( QueryModelView ( models . Query ) ) <EOL> admin . add_view ( QueryResultModelView ( models . QueryResult ) ) <EOL> admin . add_view ( DashboardModelView ( models . Dashboard ) ) <EOL> logout_link = MenuLink ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> for m in ( models . Visualization , models . Widget , models . Event , models . Organization ) : <EOL> admin . add_view ( BaseModelView ( m ) ) <EOL> admin . add_link ( logout_link ) </s>
