Browse Source

User: add and track banned_time

merge-requests/65/head
Deimos 6 years ago
parent
commit
0c93a741f2
  1. 48
      tildes/alembic/versions/380a76d4a722_user_add_banned_time.py
  2. 16
      tildes/sql/init/triggers/users/users.sql
  3. 2
      tildes/tildes/models/user/user.py

48
tildes/alembic/versions/380a76d4a722_user_add_banned_time.py

@ -0,0 +1,48 @@
"""User: add banned_time
Revision ID: 380a76d4a722
Revises: 3f83028d1673
Create Date: 2019-03-28 04:04:49.089292
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "380a76d4a722"
down_revision = "3f83028d1673"
branch_labels = None
depends_on = None
def upgrade():
op.add_column(
"users", sa.Column("banned_time", sa.TIMESTAMP(timezone=True), nullable=True)
)
op.execute(
"""
CREATE OR REPLACE FUNCTION set_user_banned_time() RETURNS TRIGGER AS $$
BEGIN
NEW.banned_time := current_timestamp;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
"""
)
op.execute(
"""
CREATE TRIGGER ban_user_set_banned_time_update
BEFORE UPDATE ON users
FOR EACH ROW
WHEN (OLD.is_banned = false AND NEW.is_banned = true)
EXECUTE PROCEDURE set_user_banned_time();
"""
)
def downgrade():
op.execute("DROP TRIGGER ban_user_set_banned_time_update ON users")
op.execute("DROP FUNCTION set_user_banned_time")
op.drop_column("users", "banned_time")

16
tildes/sql/init/triggers/users/users.sql

@ -15,3 +15,19 @@ CREATE TRIGGER delete_user_set_deleted_time_update
FOR EACH ROW FOR EACH ROW
WHEN (OLD.is_deleted = false AND NEW.is_deleted = true) WHEN (OLD.is_deleted = false AND NEW.is_deleted = true)
EXECUTE PROCEDURE set_user_deleted_time(); EXECUTE PROCEDURE set_user_deleted_time();
-- set user.banned_time when it's banned
CREATE OR REPLACE FUNCTION set_user_banned_time() RETURNS TRIGGER AS $$
BEGIN
NEW.banned_time := current_timestamp;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER ban_user_set_banned_time_update
BEFORE UPDATE ON users
FOR EACH ROW
WHEN (OLD.is_banned = false AND NEW.is_banned = true)
EXECUTE PROCEDURE set_user_banned_time();

2
tildes/tildes/models/user/user.py

@ -58,6 +58,7 @@ class User(DatabaseModel):
inserted into comment_labels. inserted into comment_labels.
Internal: Internal:
- deleted_time will be set when is_deleted is set to true - deleted_time will be set when is_deleted is set to true
- banned_time will be set when is_banned is set to true
""" """
schema_class = UserSchema schema_class = UserSchema
@ -108,6 +109,7 @@ class User(DatabaseModel):
) )
deleted_time: Optional[datetime] = Column(TIMESTAMP(timezone=True)) deleted_time: Optional[datetime] = Column(TIMESTAMP(timezone=True))
is_banned: bool = Column(Boolean, nullable=False, server_default="false") is_banned: bool = Column(Boolean, nullable=False, server_default="false")
banned_time: Optional[datetime] = Column(TIMESTAMP(timezone=True))
permissions: Any = Column(JSONB(none_as_null=True)) permissions: Any = Column(JSONB(none_as_null=True))
home_default_order: Optional[TopicSortOption] = Column(ENUM(TopicSortOption)) home_default_order: Optional[TopicSortOption] = Column(ENUM(TopicSortOption))
home_default_period: Optional[str] = Column(Text) home_default_period: Optional[str] = Column(Text)

Loading…
Cancel
Save