Skip to content

Commit

Permalink
Improve admin
Browse files Browse the repository at this point in the history
  • Loading branch information
Dosenpfand committed Aug 6, 2023
1 parent b70e9c5 commit c5d75a8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
15 changes: 12 additions & 3 deletions app/extended_security/admin.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
from flask import abort, redirect, request, url_for
from flask import abort, current_app
from flask_admin.form import SecureForm
from flask_admin.contrib.sqla import ModelView
from flask_admin.base import AdminIndexView, expose
from flask_admin.base import AdminIndexView
from flask_security import current_user


def user_formatter(view, context, model, name):
return f"{model.user.email}"


class AdminSecurityMixIn:
def is_accessible(self):
return (
current_user.is_active
and current_user.is_authenticated
and current_user.has_role("admin")
and current_user.has_role(current_app.config["ADMIN_ROLE_NAME"])
)

def inaccessible_callback(self, name, **kwargs):
Expand All @@ -19,6 +24,10 @@ def inaccessible_callback(self, name, **kwargs):
class AdminModelView(AdminSecurityMixIn, ModelView):
column_display_pk = True
column_hide_backrefs = False
can_view_details = True
can_export = True
form_base_class = SecureForm
column_formatters = dict(user=user_formatter)


class ExtendedAdminIndex(AdminSecurityMixIn, AdminIndexView):
Expand Down
5 changes: 4 additions & 1 deletion app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ class User(BaseModel, fsqla.FsUserMixin):
)

def __repr__(self) -> str:
return f"User({self.email}, {self.username}, {self.active}, {self.fs_uniquifier}, {self.confirmed_at})"
return f"<{self.__class__.__name__} {self.email}>"

def __str__(self) -> str:
return f"{self.email}"


class Journey(BaseModel):
Expand Down
1 change: 1 addition & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@
}
CONTENT_SECURITY_POLICY_NONCE_IN = ["script-src"]
FORCE_HTTPS = False
ADMIN_ROLE_NAME = "admin"

0 comments on commit c5d75a8

Please sign in to comment.