From c96e88537de14bc17988d33cc25b77a52d7af6b4 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Mon, 5 Feb 2024 12:53:45 -0600 Subject: Add reply model --- .../5f6c1515e239_add_approved_to_comments.py | 32 +++++++++++++++ migrations/versions/f5aac764ce5b_create_replies.py | 45 ++++++++++++++++++++++ src/database/models/__init__.py | 1 + src/database/models/comment.py | 5 ++- src/database/models/reply.py | 19 +++++++++ 5 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 migrations/versions/5f6c1515e239_add_approved_to_comments.py create mode 100644 migrations/versions/f5aac764ce5b_create_replies.py create mode 100644 src/database/models/reply.py diff --git a/migrations/versions/5f6c1515e239_add_approved_to_comments.py b/migrations/versions/5f6c1515e239_add_approved_to_comments.py new file mode 100644 index 0000000..7a0bff8 --- /dev/null +++ b/migrations/versions/5f6c1515e239_add_approved_to_comments.py @@ -0,0 +1,32 @@ +"""Add approved to comments + +Revision ID: 5f6c1515e239 +Revises: c2ca1cc5b130 +Create Date: 2024-02-05 11:13:50.341119 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '5f6c1515e239' +down_revision = 'c2ca1cc5b130' +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.add_column(sa.Column('approved', sa.Boolean(), nullable=False)) + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('comments', schema=None) as batch_op: + batch_op.drop_column('approved') + + # ### end Alembic commands ### diff --git a/migrations/versions/f5aac764ce5b_create_replies.py b/migrations/versions/f5aac764ce5b_create_replies.py new file mode 100644 index 0000000..d8244a6 --- /dev/null +++ b/migrations/versions/f5aac764ce5b_create_replies.py @@ -0,0 +1,45 @@ +"""Create replies + +Revision ID: f5aac764ce5b +Revises: 5f6c1515e239 +Create Date: 2024-02-05 12:30:38.848776 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'f5aac764ce5b' +down_revision = '5f6c1515e239' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('replies', + sa.Column('id', sa.BigInteger(), nullable=False), + sa.Column('content', sa.Text(), nullable=False), + sa.Column('email', sa.String(length=256), nullable=True), + sa.Column('author', sa.String(length=128), nullable=True), + sa.Column('comment_id', sa.BigInteger(), nullable=False), + sa.Column('approved', sa.Boolean(), nullable=False), + sa.Column('created_at', sa.DateTime(), nullable=True), + sa.Column('updated_at', sa.DateTime(), nullable=True), + sa.ForeignKeyConstraint(['comment_id'], ['comments.id'], ), + sa.PrimaryKeyConstraint('id') + ) + with op.batch_alter_table('replies', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_replies_comment_id'), ['comment_id'], unique=False) + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('replies', schema=None) as batch_op: + batch_op.drop_index(batch_op.f('ix_replies_comment_id')) + + op.drop_table('replies') + # ### end Alembic commands ### diff --git a/src/database/models/__init__.py b/src/database/models/__init__.py index 0647151..7a72ccf 100644 --- a/src/database/models/__init__.py +++ b/src/database/models/__init__.py @@ -1,2 +1,3 @@ from src.database.models.blog import Blog from src.database.models.comment import Comment +from src.database.models.reply import Reply diff --git a/src/database/models/comment.py b/src/database/models/comment.py index 9501bba..d1ca868 100644 --- a/src/database/models/comment.py +++ b/src/database/models/comment.py @@ -1,4 +1,4 @@ -from sqlalchemy import BigInteger, String, DateTime, Text, ForeignKey +from sqlalchemy import BigInteger, String, DateTime, Text, ForeignKey, Boolean from sqlalchemy.orm import mapped_column, Mapped, relationship from src.database import db @@ -13,6 +13,9 @@ class Comment(db.Model): author: Mapped[str] = mapped_column(String(128), nullable=True) post: Mapped[str] = mapped_column(String(1024), nullable=False) blog_id: Mapped[int] = mapped_column(ForeignKey('blogs.id'), index=True) + approved: Mapped[bool] = mapped_column(Boolean, nullable=False, + default=True) blog = relationship('Blog', back_populates='comments') + replies = relationship('Reply', back_populates='comment') created_at = mapped_column(DateTime) updated_at = mapped_column(DateTime) diff --git a/src/database/models/reply.py b/src/database/models/reply.py new file mode 100644 index 0000000..6324baf --- /dev/null +++ b/src/database/models/reply.py @@ -0,0 +1,19 @@ +from sqlalchemy import BigInteger, String, DateTime, Text, ForeignKey, Boolean +from sqlalchemy.orm import mapped_column, Mapped, relationship +from src.database import db + + +class Reply(db.Model): + __tablename__ = 'replies' + + id = mapped_column(BigInteger, primary_key=True) + content: Mapped[str] = mapped_column(Text) + email: Mapped[str] = mapped_column(String(256), nullable=True) + author: Mapped[str] = mapped_column(String(128), nullable=True) + comment_id: Mapped[int] = mapped_column(ForeignKey('comments.id'), + index=True) + approved: Mapped[bool] = mapped_column(Boolean, nullable=False, + default=True) + comment = relationship('Comment', back_populates='replies') + created_at = mapped_column(DateTime) + updated_at = mapped_column(DateTime) -- cgit v1.2.3