Skip to content

Commit

Permalink
Generated from rocket-launcher da7cb2c88a07004622181fe38069c11fe0ab3d2c
Browse files Browse the repository at this point in the history
  • Loading branch information
fangpenlin committed Sep 21, 2020
1 parent e04d3bd commit 05a243f
Show file tree
Hide file tree
Showing 11 changed files with 585 additions and 507 deletions.
78 changes: 78 additions & 0 deletions migrations/versions/0fd4cc96df12_000_init_database.py
@@ -0,0 +1,78 @@
"""000 init database
Revision ID: 0fd4cc96df12
Revises:
Create Date: 2018-05-02 01:03:13.481883
"""
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "0fd4cc96df12"
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.execute("CREATE EXTENSION IF NOT EXISTS pgcrypto")
op.create_table(
"roles",
sa.Column(
"id",
postgresql.UUID(as_uuid=True),
server_default=sa.text("gen_random_uuid()"),
nullable=False,
),
sa.Column("name", sa.String(length=80), nullable=True),
sa.Column("description", sa.String(length=255), nullable=True),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("name"),
)
op.create_table(
"users",
sa.Column(
"id",
postgresql.UUID(as_uuid=True),
server_default=sa.text("gen_random_uuid()"),
nullable=False,
),
sa.Column("email", sa.String(length=255), nullable=True),
sa.Column("username", sa.String(length=255), nullable=True),
sa.Column("password", sa.String(length=255), nullable=True),
sa.Column("last_login_at", sa.DateTime(), nullable=True),
sa.Column("current_login_at", sa.DateTime(), nullable=True),
sa.Column("last_login_ip", sa.String(length=100), nullable=True),
sa.Column("current_login_ip", sa.String(length=100), nullable=True),
sa.Column("login_count", sa.Integer(), nullable=True),
sa.Column("active", sa.Boolean(), nullable=True),
sa.Column("confirmed_at", sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("email"),
)
op.create_table(
"roles_users",
sa.Column(
"id",
postgresql.UUID(as_uuid=True),
server_default=sa.text("gen_random_uuid()"),
nullable=False,
),
sa.Column("user_id", postgresql.UUID(as_uuid=True), nullable=False),
sa.Column("role_id", postgresql.UUID(as_uuid=True), nullable=False),
sa.ForeignKeyConstraint(["role_id"], ["roles.id"], onupdate="CASCADE"),
sa.ForeignKeyConstraint(["user_id"], ["users.id"], onupdate="CASCADE"),
sa.PrimaryKeyConstraint("id"),
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table("roles_users")
op.drop_table("users")
op.drop_table("roles")
# ### end Alembic commands ###
182 changes: 91 additions & 91 deletions sampleapp/admin.py
@@ -1,91 +1,91 @@
from flask import current_app
from flask import flash
from flask import redirect
from flask import request
from flask import url_for
from flask_admin import AdminIndexView
from flask_admin import expose
from flask_admin.contrib.sqla import ModelView
from flask_admin.model.template import EndpointLinkRowAction
from flask_login import login_user
from flask_principal import Identity
from flask_principal import identity_changed

from .permissions import admin_permission


class SecureViewMixin:
def is_accessible(self):
return admin_permission.can()

def inaccessible_callback(self, name, **kwargs):
return redirect(url_for("public.login", next=request.url))


class ProtectedAdminIndexView(SecureViewMixin, AdminIndexView):
pass


class BaseModelView(SecureViewMixin, ModelView):
extra_css = ["/static/vendor/fontawesome-free-5.14.0-web/css/all.min.css"]


class UserModelView(BaseModelView):
can_delete = False # disable model deletion
can_create = False
can_view_details = True
can_export = True
column_searchable_list = ["email"]
form_excluded_columns = [
"password",
"subscriptions",
"export_requests",
"created_at",
"bulk_search_requests",
"api_keys",
"features",
]
column_exclude_list = [
"password",
"first_name",
"last_name",
]
column_details_list = [
"id",
"plan_slug",
"feature_slugs",
"created_at",
"first_name",
"last_name",
"is_active",
"is_admin",
]
column_export_exclude_list = [
"password",
]
column_default_sort = ("created_at", True)
column_list = [
"email",
"plan_slug",
"created_at",
"is_active",
"is_admin",
]
column_extra_row_actions = [
EndpointLinkRowAction(
"fa fa-sign-in-alt", "admin.user.login_as", id_arg="user_id",
)
]

@expose("/login-as/<user_id>")
def login_as(self, user_id):
from .models.accounts import User

user = User.query.filter_by(id=user_id).first_or_404()
login_user(user, remember=False)
identity_changed.send(
current_app._get_current_object(), identity=Identity(user.id)
)
flash(f"You are logged in as {user.email}.", "success")
redirect_url = url_for("public.home")
return redirect(redirect_url)
from flask import current_app
from flask import flash
from flask import redirect
from flask import request
from flask import url_for
from flask_admin import AdminIndexView
from flask_admin import expose
from flask_admin.contrib.sqla import ModelView
from flask_admin.model.template import EndpointLinkRowAction
from flask_login import login_user
from flask_principal import Identity
from flask_principal import identity_changed

from .permissions import admin_permission


class SecureViewMixin:
def is_accessible(self):
return admin_permission.can()

def inaccessible_callback(self, name, **kwargs):
return redirect(url_for("public.login", next=request.url))


class ProtectedAdminIndexView(SecureViewMixin, AdminIndexView):
pass


class BaseModelView(SecureViewMixin, ModelView):
extra_css = ["/static/vendor/fontawesome-free-5.14.0-web/css/all.min.css"]


class UserModelView(BaseModelView):
can_delete = False # disable model deletion
can_create = False
can_view_details = True
can_export = True
column_searchable_list = ["email"]
form_excluded_columns = [
"password",
"subscriptions",
"export_requests",
"created_at",
"bulk_search_requests",
"api_keys",
"features",
]
column_exclude_list = [
"password",
"first_name",
"last_name",
]
column_details_list = [
"id",
"plan_slug",
"feature_slugs",
"created_at",
"first_name",
"last_name",
"is_active",
"is_admin",
]
column_export_exclude_list = [
"password",
]
column_default_sort = ("created_at", True)
column_list = [
"email",
"plan_slug",
"created_at",
"is_active",
"is_admin",
]
column_extra_row_actions = [
EndpointLinkRowAction(
"fa fa-sign-in-alt", "admin.user.login_as", id_arg="user_id",
)
]

@expose("/login-as/<user_id>")
def login_as(self, user_id):
from .models.accounts import User

user = User.query.filter_by(id=user_id).first_or_404()
login_user(user, remember=False)
identity_changed.send(
current_app._get_current_object(), identity=Identity(user.id)
)
flash(f"You are logged in as {user.email}.", "success")
redirect_url = url_for("public.home")
return redirect(redirect_url)

0 comments on commit 05a243f

Please sign in to comment.