Browse Source

feat: 添加数据库模型

SongZihuan 2 years ago
parent
commit
7ad0ddf713
5 changed files with 155 additions and 3 deletions
  1. 9 1
      README.md
  2. 1 1
      app/__init__.py
  3. 95 1
      app/db.py
  4. 50 0
      migrations/versions/f0c2c737b69b_.py
  5. BIN
      requirements.txt

+ 9 - 1
README.md

@@ -1,2 +1,10 @@
 # HTalk
-基于Python-Flask的沟通交流平台。
+基于Python-Flask的沟通交流平台。
+
+## 初始化数据库
+```shell
+$ flask shell
+$ >>> from app.db import create_all  
+$ >>> create_all()
+$ >>> quit()
+```

+ 1 - 1
app/__init__.py

@@ -27,7 +27,7 @@ class HTalkFlask(Flask):
         db.init_app(self)
         moment.init_app(self)
         mail.init_app(self)
-        migrate.init_app(self)
+        migrate.init_app(self, db)
 
     def profile_setting(self):
         if conf["DEBUG_PROFILE"]:

+ 95 - 1
app/db.py

@@ -1,6 +1,100 @@
 from flask_sqlalchemy import SQLAlchemy
+from flask_login import UserMixin, AnonymousUserMixin
+from datetime import datetime
 
 db = SQLAlchemy()
 
-if __name__ == '__main__':
+
+class Follow(db.Model):
+    time = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
+    follower_id = db.Column(db.Integer, db.ForeignKey("user.id"), primary_key=True, nullable=True)
+    followed_id = db.Column(db.Integer, db.ForeignKey("user.id"), primary_key=True, nullable=True)
+    follower = db.relationship("User", primaryjoin="Follow.follower_id==User.id", back_populates="follower")
+    followed = db.relationship("User", primaryjoin="Follow.followed_id==User.id", back_populates="followed")
+
+
+
+class AnonymousUser(AnonymousUserMixin):
+    pass
+
+
+class User(db.Model, UserMixin):
+    __tablename__ = "user"
+
+    id = db.Column(db.Integer, autoincrement=True, primary_key=True, nullable=False)
+    email = db.Column(db.String(32), nullable=False, unique=True)
+    passwd_hash = db.Column(db.String(128), nullable=False)
+    role_id = db.Column(db.Integer, db.ForeignKey("role.id"), default=3)
+    role = db.relationship("Role", back_populates="user")
+    comment = db.relationship("Comment", back_populates="auth")
+
+    follower = db.relationship("Follow", primaryjoin="Follow.follower_id==User.id", back_populates="follower",
+                               lazy="dynamic")
+    followed = db.relationship("Follow", primaryjoin="Follow.followed_id==User.id", back_populates="followed",
+                               lazy="dynamic")
+
+
+class Role(db.Model):
+    __tablename__ = "role"
+
+    USABLE = 1  # 账号可使用
+    CHECK_COMMENT = 2
+    CHECK_ARCHIVE = 4
+    CHECK_FOLLOW = 8
+    CREATE_COMMENT = 16
+    CREATE_ARCHIVE = 32  # 系统权限
+    FOLLOW = 64
+    BLOCK_USER = 128  # 系统权限
+    DELETE_COMMENT = 256  # 系统权限
+    DELETE_ARCHIVE = 512  # 系统权限
+    SYSTEM = 1024  # 系统权限
+
+    id = db.Column(db.Integer, autoincrement=True, primary_key=True, nullable=False)
+    name = db.Column(db.String(32), nullable=False, unique=True)
+    permission = db.Column(db.Integer, nullable=False, default=95)  # 非系统权限
+    user = db.relationship("User", back_populates="role")
+
+
+StudentClass = db.Table("archive_comment",
+                        db.Column("archive_id", db.Integer, db.ForeignKey("archive.id"),
+                                  nullable=False, primary_key=True),
+                        db.Column("comment_id", db.Integer, db.ForeignKey("comment.id"),
+                                  nullable=False, primary_key=True))
+
+
+class Comment(db.Model):
+    __tablename__ = "comment"
+
+    id = db.Column(db.Integer, autoincrement=True, primary_key=True, nullable=False)
+    title = db.Column(db.String(32), nullable=True)  # 允许为空
+    content = db.Column(db.Text, nullable=False)
+    create_time = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
+    update_time = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
+    auth_id = db.Column(db.Integer, db.ForeignKey("user.id"), primary_key=True, nullable=True)
+    father_id = db.Column(db.Integer, db.ForeignKey("comment.id"), primary_key=True, nullable=True)
+    auth = db.relationship("User", back_populates="comment")
+    father = db.relationship("Comment", foreign_keys="[Comment.father_id]", remote_side="[Comment.id]",
+                             back_populates="son")
+    son = db.relationship("Comment", foreign_keys="[Comment.father_id]", remote_side="[Comment.father_id]",
+                          back_populates="father")
+    archive = db.relationship("Archive", back_populates="comment", secondary="archive_comment")
+
+
+class Archive(db.Model):
+    __tablename__ = "archive"
+
+    id = db.Column(db.Integer, autoincrement=True, primary_key=True, nullable=False)
+    name = db.Column(db.String(32), nullable=False, unique=True)
+    describe = db.Column(db.String(100), nullable=False)
+    comment = db.relationship("Comment", back_populates="archive", secondary="archive_comment")
+
+
+def create_all():
     db.create_all()
+
+    admin = Role(name="admin", permission=2047)
+    coordinator = Role(name="coordinator", permission=1023)
+    default = Role(name="default")
+
+    db.session.add_all([admin, coordinator, default])
+    db.session.commit()

+ 50 - 0
migrations/versions/f0c2c737b69b_.py

@@ -0,0 +1,50 @@
+"""empty message
+
+Revision ID: f0c2c737b69b
+Revises: 
+Create Date: 2022-10-15 18:44:21.972709
+
+"""
+from alembic import op
+import sqlalchemy as sa
+from sqlalchemy.dialects import mysql
+
+# revision identifiers, used by Alembic.
+revision = 'f0c2c737b69b'
+down_revision = None
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+    # ### commands auto generated by Alembic - please adjust! ###
+    op.alter_column('comment', 'auth_id',
+               existing_type=mysql.INTEGER(),
+               nullable=True)
+    op.alter_column('comment', 'father_id',
+               existing_type=mysql.INTEGER(),
+               nullable=True)
+    op.alter_column('follow', 'follower_id',
+               existing_type=mysql.INTEGER(),
+               nullable=True)
+    op.alter_column('follow', 'followed_id',
+               existing_type=mysql.INTEGER(),
+               nullable=True)
+    # ### end Alembic commands ###
+
+
+def downgrade():
+    # ### commands auto generated by Alembic - please adjust! ###
+    op.alter_column('follow', 'followed_id',
+               existing_type=mysql.INTEGER(),
+               nullable=False)
+    op.alter_column('follow', 'follower_id',
+               existing_type=mysql.INTEGER(),
+               nullable=False)
+    op.alter_column('comment', 'father_id',
+               existing_type=mysql.INTEGER(),
+               nullable=False)
+    op.alter_column('comment', 'auth_id',
+               existing_type=mysql.INTEGER(),
+               nullable=False)
+    # ### end Alembic commands ###

BIN
requirements.txt