mirror of
https://github.com/logos-co/open-law.git
synced 2025-02-08 13:03:39 +00:00
flask admin
This commit is contained in:
parent
33812b83ee
commit
3700d89d30
@ -5,6 +5,7 @@ from flask_sqlalchemy import SQLAlchemy
|
||||
from flask_login import LoginManager
|
||||
from werkzeug.exceptions import HTTPException
|
||||
from flask_migrate import Migrate
|
||||
from flask_admin import Admin
|
||||
|
||||
from app.logger import log
|
||||
|
||||
@ -28,10 +29,7 @@ def create_app(environment="development"):
|
||||
star_blueprint,
|
||||
search_blueprint,
|
||||
)
|
||||
from app.models import (
|
||||
User,
|
||||
AnonymousUser,
|
||||
)
|
||||
from app import models as m
|
||||
|
||||
# Instantiate app.
|
||||
app = Flask(__name__)
|
||||
@ -63,11 +61,11 @@ def create_app(environment="development"):
|
||||
# Set up flask login.
|
||||
@login_manager.user_loader
|
||||
def get_user(id):
|
||||
return User.query.get(int(id))
|
||||
return m.User.query.get(int(id))
|
||||
|
||||
login_manager.login_view = "auth.login"
|
||||
login_manager.login_message_category = "info"
|
||||
login_manager.anonymous_user = AnonymousUser
|
||||
login_manager.anonymous_user = m.AnonymousUser
|
||||
|
||||
# Jinja globals
|
||||
from app.controllers.jinja_globals import (
|
||||
@ -85,4 +83,35 @@ def create_app(environment="development"):
|
||||
def handle_http_error(exc):
|
||||
return render_template("error.html", error=exc), exc.code
|
||||
|
||||
# flask admin
|
||||
from app.controllers.flask_admin_customization import (
|
||||
CustomAdminIndexView,
|
||||
ProtectedModelView,
|
||||
)
|
||||
|
||||
app.config["FLASK_ADMIN_SWATCH"] = "Flatly"
|
||||
admin = Admin(
|
||||
app,
|
||||
name="Open Law Admin",
|
||||
template_mode="bootstrap3",
|
||||
index_view=CustomAdminIndexView(),
|
||||
)
|
||||
|
||||
for view in [
|
||||
ProtectedModelView(m.Book, db.session, name="Book", endpoint="/book_"),
|
||||
ProtectedModelView(
|
||||
m.Collection, db.session, name="Collection", endpoint="/collection_"
|
||||
),
|
||||
ProtectedModelView(m.Section, db.session, name="Section", endpoint="/section_"),
|
||||
ProtectedModelView(
|
||||
m.Interpretation,
|
||||
db.session,
|
||||
name="Interpretation",
|
||||
endpoint="/interpretation_",
|
||||
),
|
||||
ProtectedModelView(m.Comment, db.session, name="Comment", endpoint="/comment_"),
|
||||
ProtectedModelView(m.Tag, db.session, name="Tag", endpoint="/tag_"),
|
||||
]:
|
||||
admin.add_view(view)
|
||||
|
||||
return app
|
||||
|
3
app/controllers/flask_admin_customization/__init__.py
Normal file
3
app/controllers/flask_admin_customization/__init__.py
Normal file
@ -0,0 +1,3 @@
|
||||
# flake8: noqa F401
|
||||
from .custom_admin_index_view import CustomAdminIndexView
|
||||
from .protected_model_view import ProtectedModelView
|
@ -0,0 +1,16 @@
|
||||
from flask_admin import AdminIndexView
|
||||
from flask_login import current_user
|
||||
from flask import redirect, url_for
|
||||
|
||||
|
||||
class CustomAdminIndexView(AdminIndexView):
|
||||
def is_accessible(self):
|
||||
return current_user.is_super_user
|
||||
|
||||
def is_visible(self):
|
||||
# This view won't appear in the menu structure
|
||||
return False
|
||||
|
||||
def inaccessible_callback(self, name, **kwargs):
|
||||
# redirect to login page if user doesn't have access
|
||||
return redirect(url_for("main.index"))
|
@ -0,0 +1,12 @@
|
||||
from flask_admin.contrib.sqla import ModelView
|
||||
from flask_login import current_user
|
||||
from flask import redirect, url_for
|
||||
|
||||
|
||||
class ProtectedModelView(ModelView):
|
||||
def is_accessible(self):
|
||||
return current_user.is_super_user
|
||||
|
||||
def inaccessible_callback(self, name, **kwargs):
|
||||
# redirect to login page if user doesn't have access
|
||||
return redirect(url_for("main.index"))
|
@ -11,7 +11,6 @@ class Comment(BaseModel):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
text = db.Column(db.Text, unique=False, nullable=False)
|
||||
approved = db.Column(db.Boolean, default=False)
|
||||
marked = db.Column(db.Boolean, default=False)
|
||||
edited = db.Column(db.Boolean, default=False)
|
||||
|
||||
# Foreign keys
|
||||
|
@ -10,7 +10,6 @@ class Interpretation(BaseModel):
|
||||
text = db.Column(db.Text, unique=False, nullable=False)
|
||||
plain_text = db.Column(db.Text, unique=False)
|
||||
approved = db.Column(db.Boolean, default=False)
|
||||
marked = db.Column(db.Boolean, default=False)
|
||||
|
||||
# Foreign keys
|
||||
user_id = db.Column(db.ForeignKey("users.id"))
|
||||
|
@ -23,6 +23,7 @@ class User(BaseModel, UserMixin):
|
||||
is_activated = db.Column(db.Boolean, default=False)
|
||||
wallet_id = db.Column(db.String(64), nullable=True)
|
||||
avatar_img = db.Column(db.Text, nullable=True)
|
||||
is_super_user = db.Column(db.Boolean, default=False)
|
||||
# Relationships
|
||||
stars = db.relationship("Book", secondary="books_stars", back_populates="stars")
|
||||
books = db.relationship("Book")
|
||||
|
42
migrations/versions/7c8a5aefe801_remove_mark_fields.py
Normal file
42
migrations/versions/7c8a5aefe801_remove_mark_fields.py
Normal file
@ -0,0 +1,42 @@
|
||||
"""remove mark fields
|
||||
|
||||
Revision ID: 7c8a5aefe801
|
||||
Revises: a41f004cad1a
|
||||
Create Date: 2023-05-25 15:44:06.072076
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "7c8a5aefe801"
|
||||
down_revision = "a41f004cad1a"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table("comments", schema=None) as batch_op:
|
||||
batch_op.drop_column("marked")
|
||||
|
||||
with op.batch_alter_table("interpretations", schema=None) as batch_op:
|
||||
batch_op.drop_column("marked")
|
||||
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table("interpretations", schema=None) as batch_op:
|
||||
batch_op.add_column(
|
||||
sa.Column("marked", sa.BOOLEAN(), autoincrement=False, nullable=True)
|
||||
)
|
||||
|
||||
with op.batch_alter_table("comments", schema=None) as batch_op:
|
||||
batch_op.add_column(
|
||||
sa.Column("marked", sa.BOOLEAN(), autoincrement=False, nullable=True)
|
||||
)
|
||||
|
||||
# ### end Alembic commands ###
|
32
migrations/versions/a41f004cad1a_user_is_super_user.py
Normal file
32
migrations/versions/a41f004cad1a_user_is_super_user.py
Normal file
@ -0,0 +1,32 @@
|
||||
"""user.is_super_user
|
||||
|
||||
Revision ID: a41f004cad1a
|
||||
Revises: 7baa732e01c6
|
||||
Create Date: 2023-05-25 14:31:14.046066
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'a41f004cad1a'
|
||||
down_revision = '7baa732e01c6'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table('users', schema=None) as batch_op:
|
||||
batch_op.add_column(sa.Column('is_super_user', sa.Boolean(), nullable=True))
|
||||
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table('users', schema=None) as batch_op:
|
||||
batch_op.drop_column('is_super_user')
|
||||
|
||||
# ### end Alembic commands ###
|
2120
poetry.lock
generated
2120
poetry.lock
generated
File diff suppressed because it is too large
Load Diff
@ -17,6 +17,7 @@ email-validator = "^1.3.1"
|
||||
psycopg2-binary = "^2.9.5"
|
||||
pydantic = "^1.10.7"
|
||||
siwe = "^2.2.0"
|
||||
flask-admin = "^1.6.1"
|
||||
|
||||
[tool.poetry.dev-dependencies]
|
||||
pytest = "^7.1.1"
|
||||
|
Loading…
x
Reference in New Issue
Block a user