From e9cf18d0e686ed5e19e901f07fb89af3761de1e9 Mon Sep 17 00:00:00 2001 From: winet Date: Sun, 25 Feb 2024 00:11:48 +1000 Subject: [PATCH] fixed --- app/blueprints/api/users/routes.py | 2 +- app/models/collection.py | 11 ++++ app/models/task.py | 3 +- app/models/user.py | 2 +- app/models/user_collection.py | 13 ----- .../versions/21750f5bbab1_user_collections.py | 51 +++++++++++++++++++ 6 files changed, 66 insertions(+), 16 deletions(-) create mode 100644 app/models/collection.py delete mode 100644 app/models/user_collection.py create mode 100644 migrations/versions/21750f5bbab1_user_collections.py diff --git a/app/blueprints/api/users/routes.py b/app/blueprints/api/users/routes.py index 3f85d93..d0a491e 100644 --- a/app/blueprints/api/users/routes.py +++ b/app/blueprints/api/users/routes.py @@ -41,7 +41,7 @@ async def parse_users(session_id: int): @users.route("/", methods=["GET"]) def get_users(session_id: int): - users = Session.query.get_or_404(session_id).users + users = [] #Session.query.get_or_404(session_id) return render_template("user_cards.j2", users=users) diff --git a/app/models/collection.py b/app/models/collection.py new file mode 100644 index 0000000..d5aa42a --- /dev/null +++ b/app/models/collection.py @@ -0,0 +1,11 @@ +from typing import List + +from sqlalchemy.orm import Mapped, mapped_column, relationship + +from app.extensions import db + +class Collection(db.Model): + id: Mapped[int] = mapped_column(primary_key=True) + + tasks: Mapped[List['Task']] = relationship("Task", back_populates="collection") + users: Mapped[List['User']] = relationship("User", cascade="all, delete-orphan", back_populates="collection") \ No newline at end of file diff --git a/app/models/task.py b/app/models/task.py index a6d15a4..c7a1363 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -4,6 +4,7 @@ from sqlalchemy import Integer, DateTime from sqlalchemy.schema import Column, ForeignKey from sqlalchemy.orm import Mapped, mapped_column, relationship from app.extensions import db +from app.models.collection import Collection class Task(db.Model): id: Mapped[int] = mapped_column(primary_key=True) @@ -15,7 +16,7 @@ class Task(db.Model): session: Mapped['Session'] = relationship("Session", back_populates="tasks") collection_id = Column(Integer, ForeignKey("collection.id")) - collection: Mapped['UsersCollection'] = relationship('UsersCollection', back_populates='task') + collection: Mapped['Collection'] = relationship('Collection', back_populates='tasks') datetime: Mapped[DateTime] = Column(DateTime, default=datetime.datetime.utcnow()) status: Mapped[str] diff --git a/app/models/user.py b/app/models/user.py index 9266261..ffaf9e6 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -16,4 +16,4 @@ class User(db.Model): username: Mapped[str] = mapped_column(nullable=True, unique=True) collection_id = Column(Integer, ForeignKey("collection.id")) - collection: Mapped["UsersCollection"] = relationship("UsersCollection", back_populates="users") \ No newline at end of file + collection: Mapped["Collection"] = relationship("Collection", back_populates="users") \ No newline at end of file diff --git a/app/models/user_collection.py b/app/models/user_collection.py deleted file mode 100644 index 4d6fc34..0000000 --- a/app/models/user_collection.py +++ /dev/null @@ -1,13 +0,0 @@ -from typing import List - -from sqlalchemy.orm import Mapped, mapped_column, relationship - -from app.extensions import db -from app.models.task import Task -from app.models.user import User - -class UsersCollection(db.Model): - id: Mapped[int] = mapped_column(primary_key=True) - - Mapped[List[Task]] = relationship("Task", back_populates="collection") - users: Mapped[List[User]] = relationship("User", cascade="all, delete-orphan", back_populates="collection") \ No newline at end of file diff --git a/migrations/versions/21750f5bbab1_user_collections.py b/migrations/versions/21750f5bbab1_user_collections.py new file mode 100644 index 0000000..b2ec230 --- /dev/null +++ b/migrations/versions/21750f5bbab1_user_collections.py @@ -0,0 +1,51 @@ +"""user collections + +Revision ID: 21750f5bbab1 +Revises: cdd471f48b0d +Create Date: 2024-02-24 22:47:05.197801 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '21750f5bbab1' +down_revision = 'cdd471f48b0d' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('collection', + sa.Column('id', sa.Integer(), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + with op.batch_alter_table('task', schema=None) as batch_op: + batch_op.add_column(sa.Column('collection_id', sa.Integer(), nullable=True)) + batch_op.create_foreign_key(None, 'collection', ['collection_id'], ['id']) + + with op.batch_alter_table('user', schema=None) as batch_op: + batch_op.add_column(sa.Column('collection_id', sa.Integer(), nullable=True)) + batch_op.drop_constraint('user_session_id_fkey', type_='foreignkey') + batch_op.create_foreign_key(None, 'collection', ['collection_id'], ['id']) + batch_op.drop_column('session_id') + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('user', schema=None) as batch_op: + batch_op.add_column(sa.Column('session_id', sa.INTEGER(), autoincrement=False, nullable=True)) + batch_op.drop_constraint(None, type_='foreignkey') + batch_op.create_foreign_key('user_session_id_fkey', 'session', ['session_id'], ['id']) + batch_op.drop_column('collection_id') + + with op.batch_alter_table('task', schema=None) as batch_op: + batch_op.drop_constraint(None, type_='foreignkey') + batch_op.drop_column('collection_id') + + op.drop_table('collection') + # ### end Alembic commands ###