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.
This commit is contained in:
Lephe 2019-08-21 16:50:23 +02:00
parent 201e961ba2
commit 11b19af199
11 changed files with 95 additions and 11 deletions

View File

@ -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

View File

@ -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

View File

@ -21,4 +21,4 @@ class Forum(db.Model):
self.priv_prefix = priv_prefix
def __repr__(self):
return f'<Forum #{self.id}>'
return f'<Forum: {self.name}>'

View File

@ -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):

View File

@ -26,7 +26,7 @@ class SpecialPrivilege(db.Model):
self.priv = priv
def __repr__(self):
return f'<Privilege "{self.priv}" of member #{self.mid}>'
return f'<Privilege: {self.priv} of member #{self.mid}>'
# 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'<Group "{self.name}">'
return f'<Group: {self.name}>'
# Many-to-many relation for users belonging to groups

View File

@ -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):

View File

@ -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

View File

@ -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'<User: #{self.id}>'
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'<Guest: {self.username} ({self.ip})>'
class Member(User, db.Model):
class Member(User):
""" Registered user with full access to the website's services """
__tablename__ = 'member'

View File

@ -26,7 +26,7 @@
<code>{{ priv }}</code>
{{- ', ' if not loop.last }}
{% endfor %}</td>
<td><a href="{{ url_for('adm_edit_account', user_id=user.id) }}">Modifier</a></td>
<td style="text-align: center"><a href="{{ url_for('adm_edit_account', user_id=user.id) }}">Modifier</a></td>
</tr>
{% endfor %}
</table>

View File

@ -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 ###

View File

@ -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 ###