def setUp(self): from flask import Flask from flask_appbuilder import AppBuilder from flask_appbuilder.models.sqla.interface import SQLAInterface from flask_appbuilder.views import ModelView self.app = Flask(__name__) self.basedir = os.path.abspath(os.path.dirname(__file__)) self.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' self.app.config['CSRF_ENABLED'] = False self.app.config['SECRET_KEY'] = 'thisismyscretkey' self.app.config['WTF_CSRF_ENABLED'] = False self.db = SQLA(self.app) self.appbuilder = AppBuilder(self.app, self.db.session) sess = PSSession() class PSView(ModelView): datamodel = GenericInterface(PSModel, sess) base_permissions = ['can_list', 'can_show'] list_columns = ['UID', 'C', 'CMD', 'TIME'] search_columns = ['UID', 'C', 'CMD'] class Model2View(ModelView): datamodel = SQLAInterface(Model2) list_columns = [ 'field_integer', 'field_float', 'field_string', 'field_method', 'group.field_string' ] edit_form_query_rel_fields = { 'group': [['field_string', FilterEqual, 'G2']] } add_form_query_rel_fields = { 'group': [['field_string', FilterEqual, 'G1']] } class Model22View(ModelView): datamodel = SQLAInterface(Model2) list_columns = [ 'field_integer', 'field_float', 'field_string', 'field_method', 'group.field_string' ] add_exclude_columns = ['excluded_string'] edit_exclude_columns = ['excluded_string'] show_exclude_columns = ['excluded_string'] class Model1View(ModelView): datamodel = SQLAInterface(Model1) related_views = [Model2View] list_columns = ['field_string', 'field_file'] class Model1CompactView(CompactCRUDMixin, ModelView): datamodel = SQLAInterface(Model1) class Model1ViewWithRedirects(ModelView): datamodel = SQLAInterface(Model1) obj_id = 1 def post_add_redirect(self): return redirect( 'model1viewwithredirects/show/{0}'.format(REDIRECT_OBJ_ID)) def post_edit_redirect(self): return redirect( 'model1viewwithredirects/show/{0}'.format(REDIRECT_OBJ_ID)) def post_delete_redirect(self): return redirect( 'model1viewwithredirects/show/{0}'.format(REDIRECT_OBJ_ID)) class Model1Filtered1View(ModelView): datamodel = SQLAInterface(Model1) base_filters = [['field_string', FilterStartsWith, 'a']] class Model1MasterView(MasterDetailView): datamodel = SQLAInterface(Model1) related_views = [Model2View] class Model1Filtered2View(ModelView): datamodel = SQLAInterface(Model1) base_filters = [['field_integer', FilterEqual, 0]] class Model2ChartView(ChartView): datamodel = SQLAInterface(Model2) chart_title = 'Test Model1 Chart' group_by_columns = ['field_string'] class Model2GroupByChartView(GroupByChartView): datamodel = SQLAInterface(Model2) chart_title = 'Test Model1 Chart' definitions = [{ 'group': 'field_string', 'series': [(aggregate_sum, 'field_integer', aggregate_avg, 'field_integer', aggregate_count, 'field_integer')] }] class Model2DirectByChartView(DirectByChartView): datamodel = SQLAInterface(Model2) chart_title = 'Test Model1 Chart' definitions = [{ 'group': 'field_string', 'series': ['field_integer', 'field_float'] }] class Model2TimeChartView(TimeChartView): datamodel = SQLAInterface(Model2) chart_title = 'Test Model1 Chart' group_by_columns = ['field_date'] class Model2DirectChartView(DirectChartView): datamodel = SQLAInterface(Model2) chart_title = 'Test Model1 Chart' direct_columns = {'stat1': ('group', 'field_integer')} class Model1MasterView(MasterDetailView): datamodel = SQLAInterface(Model1) related_views = [Model2View] class Model1MasterChartView(MasterDetailView): datamodel = SQLAInterface(Model1) related_views = [Model2DirectByChartView] class Model1FormattedView(ModelView): datamodel = SQLAInterface(Model1) list_columns = ['field_string'] show_columns = ['field_string'] formatters_columns = { 'field_string': lambda x: 'FORMATTED_STRING', } self.appbuilder.add_view(Model1View, "Model1", category='Model1') self.appbuilder.add_view(Model1ViewWithRedirects, "Model1ViewWithRedirects", category='Model1') self.appbuilder.add_view(Model1CompactView, "Model1Compact", category='Model1') self.appbuilder.add_view(Model1MasterView, "Model1Master", category='Model1') self.appbuilder.add_view(Model1MasterChartView, "Model1MasterChart", category='Model1') self.appbuilder.add_view(Model1Filtered1View, "Model1Filtered1", category='Model1') self.appbuilder.add_view(Model1Filtered2View, "Model1Filtered2", category='Model1') self.appbuilder.add_view(Model1FormattedView, "Model1FormattedView", category='Model1FormattedView') self.appbuilder.add_view(Model2View, "Model2") self.appbuilder.add_view(Model22View, "Model22") self.appbuilder.add_view(Model2View, "Model2 Add", href='/model2view/add') self.appbuilder.add_view(Model2ChartView, "Model2 Chart") self.appbuilder.add_view(Model2GroupByChartView, "Model2 Group By Chart") self.appbuilder.add_view(Model2DirectByChartView, "Model2 Direct By Chart") self.appbuilder.add_view(Model2TimeChartView, "Model2 Time Chart") self.appbuilder.add_view(Model2DirectChartView, "Model2 Direct Chart") self.appbuilder.add_view(PSView, "Generic DS PS View", category='PSView') role_admin = self.appbuilder.sm.find_role('Admin') self.appbuilder.sm.add_user('admin', 'admin', 'user', '*****@*****.**', role_admin, 'general')
def setUp(self): from flask import Flask from flask_appbuilder import AppBuilder from flask_appbuilder.models.sqla.interface import SQLAInterface from flask_appbuilder.views import ModelView from sqlalchemy.engine import Engine from sqlalchemy import event self.app = Flask(__name__) self.app.jinja_env.undefined = jinja2.StrictUndefined self.basedir = os.path.abspath(os.path.dirname(__file__)) self.app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" self.app.config["CSRF_ENABLED"] = False self.app.config["SECRET_KEY"] = "thisismyscretkey" self.app.config["WTF_CSRF_ENABLED"] = False self.app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False self.app.config["FAB_ROLES"] = { "ReadOnly": [ [".*", "can_list"], [".*", "can_show"] ] } logging.basicConfig(level=logging.ERROR) @event.listens_for(Engine, "connect") def set_sqlite_pragma(dbapi_connection, connection_record): # Will force sqllite contraint foreign keys cursor = dbapi_connection.cursor() cursor.execute("PRAGMA foreign_keys=ON") cursor.close() self.db = SQLA(self.app) self.appbuilder = AppBuilder(self.app, self.db.session) sess = PSSession() class PSView(ModelView): datamodel = GenericInterface(PSModel, sess) base_permissions = ["can_list", "can_show"] list_columns = ["UID", "C", "CMD", "TIME"] search_columns = ["UID", "C", "CMD"] class Model2View(ModelView): datamodel = SQLAInterface(Model2) list_columns = [ "field_integer", "field_float", "field_string", "field_method", "group.field_string", ] edit_form_query_rel_fields = { "group": [["field_string", FilterEqual, "G2"]] } add_form_query_rel_fields = {"group": [["field_string", FilterEqual, "G1"]]} class Model22View(ModelView): datamodel = SQLAInterface(Model2) list_columns = [ "field_integer", "field_float", "field_string", "field_method", "group.field_string", ] add_exclude_columns = ["excluded_string"] edit_exclude_columns = ["excluded_string"] show_exclude_columns = ["excluded_string"] class Model1View(ModelView): datamodel = SQLAInterface(Model1) related_views = [Model2View] list_columns = ["field_string", "field_file"] class Model3View(ModelView): datamodel = SQLAInterface(Model3) list_columns = ["pk1", "pk2", "field_string"] add_columns = ["pk1", "pk2", "field_string"] edit_columns = ["pk1", "pk2", "field_string"] class Model1CompactView(CompactCRUDMixin, ModelView): datamodel = SQLAInterface(Model1) class Model3CompactView(CompactCRUDMixin, ModelView): datamodel = SQLAInterface(Model3) class Model1ViewWithRedirects(ModelView): datamodel = SQLAInterface(Model1) obj_id = 1 def post_add_redirect(self): return redirect( "/model1viewwithredirects/show/{0}".format(REDIRECT_OBJ_ID) ) def post_edit_redirect(self): return redirect( "/model1viewwithredirects/show/{0}".format(REDIRECT_OBJ_ID) ) def post_delete_redirect(self): return redirect( "/model1viewwithredirects/show/{0}".format(REDIRECT_OBJ_ID) ) class Model1Filtered1View(ModelView): datamodel = SQLAInterface(Model1) base_filters = [["field_string", FilterStartsWith, "a"]] class Model1MasterView(MasterDetailView): datamodel = SQLAInterface(Model1) related_views = [Model2View] class Model1Filtered2View(ModelView): datamodel = SQLAInterface(Model1) base_filters = [["field_integer", FilterEqual, 0]] class Model2ChartView(ChartView): datamodel = SQLAInterface(Model2) chart_title = "Test Model1 Chart" group_by_columns = ["field_string"] class Model2GroupByChartView(GroupByChartView): datamodel = SQLAInterface(Model2) chart_title = "Test Model1 Chart" definitions = [ { "group": "field_string", "series": [ ( aggregate_sum, "field_integer", aggregate_avg, "field_integer", aggregate_count, "field_integer", ) ], } ] class Model2DirectByChartView(DirectByChartView): datamodel = SQLAInterface(Model2) chart_title = "Test Model1 Chart" list_title = "" definitions = [ {"group": "field_string", "series": ["field_integer", "field_float"]} ] class Model2TimeChartView(TimeChartView): datamodel = SQLAInterface(Model2) chart_title = "Test Model1 Chart" group_by_columns = ["field_date"] class Model2DirectChartView(DirectChartView): datamodel = SQLAInterface(Model2) chart_title = "Test Model1 Chart" direct_columns = {"stat1": ("group", "field_integer")} class Model1MasterChartView(MasterDetailView): datamodel = SQLAInterface(Model1) related_views = [Model2DirectByChartView] class Model1FormattedView(ModelView): datamodel = SQLAInterface(Model1) list_columns = ["field_string"] show_columns = ["field_string"] formatters_columns = {"field_string": lambda x: "FORMATTED_STRING"} class ModelWithEnumsView(ModelView): datamodel = SQLAInterface(ModelWithEnums) self.appbuilder.add_view(Model1View, "Model1", category="Model1") self.appbuilder.add_view( Model1ViewWithRedirects, "Model1ViewWithRedirects", category="Model1" ) self.appbuilder.add_view(Model1CompactView, "Model1Compact", category="Model1") self.appbuilder.add_view(Model1MasterView, "Model1Master", category="Model1") self.appbuilder.add_view( Model1MasterChartView, "Model1MasterChart", category="Model1" ) self.appbuilder.add_view( Model1Filtered1View, "Model1Filtered1", category="Model1" ) self.appbuilder.add_view( Model1Filtered2View, "Model1Filtered2", category="Model1" ) self.appbuilder.add_view( Model1FormattedView, "Model1FormattedView", category="Model1FormattedView" ) self.appbuilder.add_view(Model2View, "Model2") self.appbuilder.add_view(Model22View, "Model22") self.appbuilder.add_view(Model2View, "Model2 Add", href="/model2view/add") self.appbuilder.add_view(Model2ChartView, "Model2 Chart") self.appbuilder.add_view(Model2GroupByChartView, "Model2 Group By Chart") self.appbuilder.add_view(Model2DirectByChartView, "Model2 Direct By Chart") self.appbuilder.add_view(Model2TimeChartView, "Model2 Time Chart") self.appbuilder.add_view(Model2DirectChartView, "Model2 Direct Chart") self.appbuilder.add_view(Model3View, "Model3") self.appbuilder.add_view(Model3CompactView, "Model3Compact") self.appbuilder.add_view(ModelWithEnumsView, "ModelWithEnums") self.appbuilder.add_view(PSView, "Generic DS PS View", category="PSView") role_admin = self.appbuilder.sm.find_role("Admin") self.appbuilder.sm.add_user( "admin", "admin", "user", "*****@*****.**", role_admin, "general" ) role_read_only = self.appbuilder.sm.find_role("ReadOnly") self.appbuilder.sm.add_user( USERNAME_READONLY, "readonly", "readonly", "*****@*****.**", role_read_only, PASSWORD_READONLY )
def setUp(self): from flask import Flask from flask_appbuilder import AppBuilder from flask_appbuilder.models.sqla.interface import SQLAInterface from flask_appbuilder.views import ModelView self.app = Flask(__name__) self.app.jinja_env.undefined = jinja2.StrictUndefined self.app.config.from_object("flask_appbuilder.tests.config_api") logging.basicConfig(level=logging.ERROR) self.db = SQLA(self.app) self.appbuilder = AppBuilder(self.app, self.db.session) sess = PSSession() class PSView(ModelView): datamodel = GenericInterface(PSModel, sess) base_permissions = ["can_list", "can_show"] list_columns = ["UID", "C", "CMD", "TIME"] search_columns = ["UID", "C", "CMD"] class Model2View(ModelView): datamodel = SQLAInterface(Model2) list_columns = [ "field_integer", "field_float", "field_string", "field_method", "group.field_string", ] edit_form_query_rel_fields = { "group": [["field_string", FilterEqual, "test1"]] } add_form_query_rel_fields = { "group": [["field_string", FilterEqual, "test0"]] } class Model22View(ModelView): datamodel = SQLAInterface(Model2) list_columns = [ "field_integer", "field_float", "field_string", "field_method", "group.field_string", ] add_exclude_columns = ["excluded_string"] edit_exclude_columns = ["excluded_string"] show_exclude_columns = ["excluded_string"] class Model1View(ModelView): datamodel = SQLAInterface(Model1) related_views = [Model2View] list_columns = ["field_string", "field_file"] class Model3View(ModelView): datamodel = SQLAInterface(Model3) list_columns = ["pk1", "pk2", "field_string"] add_columns = ["pk1", "pk2", "field_string"] edit_columns = ["pk1", "pk2", "field_string"] class Model1CompactView(CompactCRUDMixin, ModelView): datamodel = SQLAInterface(Model1) class Model3CompactView(CompactCRUDMixin, ModelView): datamodel = SQLAInterface(Model3) class Model1ViewWithRedirects(ModelView): datamodel = SQLAInterface(Model1) def post_add_redirect(self): return redirect("/") def post_edit_redirect(self): return redirect("/") def post_delete_redirect(self): return redirect("/") class Model1Filtered1View(ModelView): datamodel = SQLAInterface(Model1) base_filters = [["field_string", FilterStartsWith, "test2"]] class Model1MasterView(MasterDetailView): datamodel = SQLAInterface(Model1) related_views = [Model2View] class Model1Filtered2View(ModelView): datamodel = SQLAInterface(Model1) base_filters = [["field_integer", FilterEqual, 0]] class Model2ChartView(ChartView): datamodel = SQLAInterface(Model2) chart_title = "Test Model1 Chart" group_by_columns = ["field_string"] class Model2GroupByChartView(GroupByChartView): datamodel = SQLAInterface(Model2) chart_title = "Test Model1 Chart" definitions = [{ "group": "field_string", "series": [( aggregate_sum, "field_integer", aggregate_avg, "field_integer", aggregate_count, "field_integer", )], }] class Model2DirectByChartView(DirectByChartView): datamodel = SQLAInterface(Model2) chart_title = "Test Model1 Chart" list_title = "" definitions = [{ "group": "field_string", "series": ["field_integer", "field_float"] }] class Model2TimeChartView(TimeChartView): datamodel = SQLAInterface(Model2) chart_title = "Test Model1 Chart" group_by_columns = ["field_date"] class Model2DirectChartView(DirectChartView): datamodel = SQLAInterface(Model2) chart_title = "Test Model1 Chart" direct_columns = {"stat1": ("group", "field_integer")} class Model1MasterChartView(MasterDetailView): datamodel = SQLAInterface(Model1) related_views = [Model2DirectByChartView] class Model1FormattedView(ModelView): datamodel = SQLAInterface(Model1) list_columns = ["field_string"] show_columns = ["field_string"] formatters_columns = {"field_string": lambda x: "FORMATTED_STRING"} class ModelWithEnumsView(ModelView): datamodel = SQLAInterface(ModelWithEnums) self.appbuilder.add_view(Model1View, "Model1", category="Model1") self.appbuilder.add_view(Model1ViewWithRedirects, "Model1ViewWithRedirects", category="Model1") self.appbuilder.add_view(Model1CompactView, "Model1Compact", category="Model1") self.appbuilder.add_view(Model1MasterView, "Model1Master", category="Model1") self.appbuilder.add_view(Model1MasterChartView, "Model1MasterChart", category="Model1") self.appbuilder.add_view(Model1Filtered1View, "Model1Filtered1", category="Model1") self.appbuilder.add_view(Model1Filtered2View, "Model1Filtered2", category="Model1") self.appbuilder.add_view(Model1FormattedView, "Model1FormattedView", category="Model1FormattedView") self.appbuilder.add_view(Model2View, "Model2") self.appbuilder.add_view(Model22View, "Model22") self.appbuilder.add_view(Model2View, "Model2 Add", href="/model2view/add") self.appbuilder.add_view(Model2ChartView, "Model2 Chart") self.appbuilder.add_view(Model2GroupByChartView, "Model2 Group By Chart") self.appbuilder.add_view(Model2DirectByChartView, "Model2 Direct By Chart") self.appbuilder.add_view(Model2TimeChartView, "Model2 Time Chart") self.appbuilder.add_view(Model2DirectChartView, "Model2 Direct Chart") self.appbuilder.add_view(Model3View, "Model3") self.appbuilder.add_view(Model3CompactView, "Model3Compact") self.appbuilder.add_view(ModelWithEnumsView, "ModelWithEnums") self.appbuilder.add_view(PSView, "Generic DS PS View", category="PSView") role_admin = self.appbuilder.sm.find_role("Admin") self.appbuilder.sm.add_user("admin", "admin", "user", "*****@*****.**", role_admin, "general") role_read_only = self.appbuilder.sm.find_role("ReadOnly") self.appbuilder.sm.add_user( USERNAME_READONLY, "readonly", "readonly", "*****@*****.**", role_read_only, PASSWORD_READONLY, )