From 11b19af199974f46ded3db8d453d451d059447e0 Mon Sep 17 00:00:00 2001 From: Lephe Date: Wed, 21 Aug 2019 16:50:23 +0200 Subject: [PATCH] forum: provide suitable migrations for the database First migrate without the foreign key to create the tables, then add the foreign key in a second migration. Also removed unneeded imports that caused dependency cycles. Minor "style" edits with the ambiguous use of super and unnecessary db.Model inheritance. --- app/__init__.py | 3 ++ app/models/comment.py | 2 +- app/models/forum.py | 2 +- app/models/post.py | 2 +- app/models/privs.py | 4 +- app/models/thread.py | 2 +- app/models/topic.py | 2 +- app/models/users.py | 5 +- app/templates/admin/groups_privileges.html | 2 +- ..._ajout_des_classes_comment_thread_forum.py | 54 +++++++++++++++++++ .../794d44c2bef8_add_foreign_key_on_thread.py | 28 ++++++++++ 11 files changed, 95 insertions(+), 11 deletions(-) create mode 100644 migrations/versions/6498631e62c5_ajout_des_classes_comment_thread_forum.py create mode 100644 migrations/versions/794d44c2bef8_add_foreign_key_on_thread.py diff --git a/app/__init__.py b/app/__init__.py index e266441..8edbf1a 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -23,6 +23,9 @@ login.login_message = "Veuillez vous authentifier avant de continuer." from app import models # IDK why this is here, but it works +from app.models.comment import Comment +from app.models.thread import Thread +from app.models.forum import Forum from app.routes import index, search, users # To load routes at initialization from app.routes.account import login, account from app.routes.admin import index, groups, account, trophies diff --git a/app/models/comment.py b/app/models/comment.py index d28f54f..0e367cf 100644 --- a/app/models/comment.py +++ b/app/models/comment.py @@ -15,7 +15,7 @@ class Comment(Post): # attachement = db.relationship('Attachement', backref='comment') def __init__(self, author, text, thread): - super.__init__(author, text) + Post.__init__(author, text) if isinstance(thread, Thread): thread = thread.id self.thread_id = thread diff --git a/app/models/forum.py b/app/models/forum.py index ad8b18a..da274a0 100644 --- a/app/models/forum.py +++ b/app/models/forum.py @@ -21,4 +21,4 @@ class Forum(db.Model): self.priv_prefix = priv_prefix def __repr__(self): - return f'' + return f'' diff --git a/app/models/post.py b/app/models/post.py index 89f1cd0..0a033c0 100644 --- a/app/models/post.py +++ b/app/models/post.py @@ -1,6 +1,6 @@ from datetime import datetime from app import db -from app.models.users import * +from app.models.users import User class Post(db.Model): diff --git a/app/models/privs.py b/app/models/privs.py index bf2113e..223629e 100644 --- a/app/models/privs.py +++ b/app/models/privs.py @@ -26,7 +26,7 @@ class SpecialPrivilege(db.Model): self.priv = priv def __repr__(self): - return f'' + return f'' # Group: User group, corresponds to a community role and a set of privileges @@ -70,7 +70,7 @@ class Group(db.Model): return sorted(gp.priv for gp in gps) def __repr__(self): - return f'' + return f'' # Many-to-many relation for users belonging to groups diff --git a/app/models/thread.py b/app/models/thread.py index f35d59c..41b0f2c 100644 --- a/app/models/thread.py +++ b/app/models/thread.py @@ -24,7 +24,7 @@ class Thread(Post): def __init__(self, author, text, title): """ Create a Thread """ - super.__init__(author, text) + Post.__init__(author, text) self.title = title def __repr__(self): diff --git a/app/models/topic.py b/app/models/topic.py index 1ac3d0b..b9fdedb 100644 --- a/app/models/topic.py +++ b/app/models/topic.py @@ -12,7 +12,7 @@ class Topic(Thread): def __init__(self, author, text, title, forum): """ Create a Topic """ - super.__init__(author, text, title) + Post.__init__(author, text, title) if isinstance(forum, Forum): forum = forum.id self.forum_id = forum diff --git a/app/models/users.py b/app/models/users.py index 4d6f80b..d55eeac 100644 --- a/app/models/users.py +++ b/app/models/users.py @@ -2,7 +2,6 @@ from datetime import date from app import db from flask import flash from flask_login import UserMixin -from app.models.post import Post from app.models.privs import SpecialPrivilege, Group, GroupMember, \ GroupPrivilege from app.models.trophies import Trophy, TrophyMember @@ -38,7 +37,7 @@ class User(UserMixin, db.Model): return f'' -class Guest(User, db.Model): +class Guest(User): """ Unregistered user with minimal privileges """ __tablename__ = 'guest' @@ -56,7 +55,7 @@ class Guest(User, db.Model): return f'' -class Member(User, db.Model): +class Member(User): """ Registered user with full access to the website's services """ __tablename__ = 'member' diff --git a/app/templates/admin/groups_privileges.html b/app/templates/admin/groups_privileges.html index 5b4900f..fce31ad 100644 --- a/app/templates/admin/groups_privileges.html +++ b/app/templates/admin/groups_privileges.html @@ -26,7 +26,7 @@ {{ priv }} {{- ', ' if not loop.last }} {% endfor %} - Modifier + Modifier {% endfor %} diff --git a/migrations/versions/6498631e62c5_ajout_des_classes_comment_thread_forum.py b/migrations/versions/6498631e62c5_ajout_des_classes_comment_thread_forum.py new file mode 100644 index 0000000..5fb4c35 --- /dev/null +++ b/migrations/versions/6498631e62c5_ajout_des_classes_comment_thread_forum.py @@ -0,0 +1,54 @@ +"""ajout des classes Comment Thread Forum + +Revision ID: 6498631e62c5 +Revises: f3f6d7f7fa81 +Create Date: 2019-08-21 16:47:15.557948 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '6498631e62c5' +down_revision = 'f3f6d7f7fa81' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('forum', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.Unicode(length=64), nullable=True), + sa.Column('slug', sa.Unicode(length=64), nullable=True), + sa.Column('description', sa.UnicodeText(), nullable=True), + sa.Column('parent_id', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['parent_id'], ['forum.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('thread', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('thread_type', sa.String(length=20), nullable=True), + sa.Column('title', sa.Unicode(length=32), nullable=True), + sa.Column('top_comment', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['id'], ['post.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('comment', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('text', sa.UnicodeText(), nullable=True), + sa.Column('thread_id', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['id'], ['post.id'], ), + sa.ForeignKeyConstraint(['thread_id'], ['thread.id'], ), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('comment') + op.drop_table('thread') + op.drop_table('forum') + # ### end Alembic commands ### diff --git a/migrations/versions/794d44c2bef8_add_foreign_key_on_thread.py b/migrations/versions/794d44c2bef8_add_foreign_key_on_thread.py new file mode 100644 index 0000000..ef22006 --- /dev/null +++ b/migrations/versions/794d44c2bef8_add_foreign_key_on_thread.py @@ -0,0 +1,28 @@ +"""add-foreign-key-on-Thread + +Revision ID: 794d44c2bef8 +Revises: 6498631e62c5 +Create Date: 2019-08-21 16:48:06.623266 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '794d44c2bef8' +down_revision = '6498631e62c5' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_foreign_key(None, 'thread', 'comment', ['top_comment'], ['id']) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint(None, 'thread', type_='foreignkey') + # ### end Alembic commands ###