Skip to content

Commit

Permalink
Small refactor, comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Peterson committed Aug 29, 2019
1 parent ba3d26e commit add798f
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions flask_secure_admin/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from flask_admin import Admin
from flask_admin import helpers as admin_helpers, expose
from flask_security import Security, login_required
from munch import Munch

from .security import (
SecureModelView, SecureRedirectIndex,
Expand Down Expand Up @@ -129,18 +130,19 @@ def add_layout_to_admin(self, admin, app, db, options):

for model_name, view_options_bag in zip_longest(
self.models, self.view_options, fillvalue={}):
view_options_bag = Munch(view_options_bag)

# Add default view options
if not view_options_bag.get('scaffold_list_columns'):
view_options_bag['scaffold_list_columns'] = \
view_options_bag.scaffold_list_columns = \
scaffold_list_columns_respecting_roles
if not view_options_bag.get('scaffold_form'):
view_options_bag['scaffold_form'] = \
view_options_bag.scaffold_form = \
scaffold_form_respecting_roles
if not view_options_bag.get('role_only_columns'):
view_options_bag['role_only_columns'] = dict()
view_options_bag.role_only_columns = dict()
if not view_options_bag.get('roles_accepted'):
view_options_bag['roles_accepted'] = self.admin_roles_accepted
view_options_bag.roles_accepted = self.admin_roles_accepted

# TODO: SQLSoup-specific
model = getattr(db, model_name)
Expand All @@ -155,14 +157,15 @@ def add_layout_to_admin(self, admin, app, db, options):
return admin

def bootstrap_database(self, app, db):
# TODO: This only works if the psql command is available
# and the database is a postgres database

try:
database_name = db._metadata._bind.url.database
# TODO: This only works if the psql command is available
# and the database is a postgres database
command_args = ['psql', database_name, '-c', "select * from users;"]
try:
completed = subprocess.run(command_args, capture_output=True)
except TypeError:
except TypeError: # Support py3.6
completed = subprocess.run(command_args, stdout=subprocess.PIPE)
if re.search('\(0 rows\)', str(completed.stdout)):
print('Detected first usage of admin.')
Expand All @@ -172,7 +175,7 @@ def bootstrap_database(self, app, db):
'user: [email protected], password: password')
print('Have fun!')
except Exception:
exc_type, exc_obj, exc_tb = sys.exc_info()
exc_type, _, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print('Failed to bootstrap database!')
print(exc_type, fname, exc_tb.tb_lineno)
Expand All @@ -198,5 +201,6 @@ def add_admin(self, app, db, options):

def add_security(self, app, db, options):
# Initialize flask-security
# TODO: Also sqlsoup specific
user_datastore = SQLSoupUserDataStore(db, db.users, db.roles)
return Security(app, user_datastore)

0 comments on commit add798f

Please sign in to comment.