Ajout des post/thread/comment/etc.

This commit is contained in:
Darks 2019-08-20 17:34:00 +02:00
parent 420117f95c
commit 81c910832b
Signed by: Darks
GPG Key ID: F61F10FA138E797C
11 changed files with 197 additions and 14 deletions

2
V5.py
View File

@ -1,6 +1,6 @@
from app import app, db
from app.models.users import User, Guest, Member, Group, GroupPrivilege
# from app.models.models import Post
from app.models.topic import Topic
@app.shell_context_processor

21
app/models/comment.py Normal file
View File

@ -0,0 +1,21 @@
from app import db
from app.models.post import Post
class Comment(Post):
__tablename__ = 'comment'
__mapper_args__ = {'polymorphic_identity': __tablename__}
id = db.Column(db.Integer, db.ForeignKey('post.id'), primary_key=True)
# Standalone properties
text = db.Column(db.UnicodeText)
# Relationships
thread_id = db.Column(db.Integer, db.ForeignKey('thread.id'), nullable=False)
# attachement = db.relationship('Attachement', backref='comment')
def __init__(self, author, text, thread):
super.__init__(author, text)
if isinstance(thread, Thread):
thread = thread.id
self.thread_id = thread

24
app/models/forum.py Normal file
View File

@ -0,0 +1,24 @@
from app import db
class Forum(db.Model):
__tablename__ = 'forum'
id = db.Column(db.Integer, primary_key=True)
# Standalone properties
name = db.Column(db.Unicode(64))
slug = db.Column(db.Unicode(64))
description = db.Column(db.UnicodeText)
# Relationships
parent_id = db.Column(db.Integer, db.ForeignKey('forum.id'), nullable=True)
sub_forums = db.relationship('Forum', backref='parent', lazy=True)
topics = db.relationship('Topic', backref='forum')
def __init__(self, name, description, priv_prefix):
self.name = name
self.description = description
self.priv_prefix = priv_prefix
def __repr__(self):
return f'<Forum #{self.id}>'

View File

@ -4,26 +4,31 @@ from app.models.users import *
class Post(db.Model):
""" Content a User can create and publish """
__tablename__ = 'post'
id = db.Column(db.Integer, primary_key=True)
type = db.Column(db.String(20))
__mapper_args__ = {
'polymorphic_identity': __tablename__,
'polymorphic_on': type
}
# Standalone properties
text = db.Column(db.Text(convert_unicode=True))
date_created = db.Column(db.DateTime, default=datetime.now)
date_modified = db.Column(db.DateTime, default=datetime.now)
date_created = db.Column(db.DateTime)
date_modified = db.Column(db.DateTime)
# Relationships
author_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
def __init__(self, author, text):
""" Create a Post """
self.text = text
if type(author) == Member:
if isinstance(author, User):
author = author.id
self.author_id = author
self.date_created = datetime.now()
self.date_modified = datetime.now()
def update(self, text):
""" Update a post. Check whether the request sender has the right to do
@ -34,7 +39,7 @@ class Post(db.Model):
def change_ownership(self, new_author):
""" Change ownership of a post. Check whether the request sender has the
right to do this! """
if type(new_author) == Member:
if isinstance(new_author, User):
new_author = new_author.id
self.author_id = new_author

31
app/models/thread.py Normal file
View File

@ -0,0 +1,31 @@
from app import db
from app.models.post import Post
from app.models.comment import Comment
from config import V5Config
class Thread(Post):
""" Some thread, such as a topic, program, tutorial """
__tablename__ = 'thread'
id = db.Column(db.Integer, db.ForeignKey('post.id'), primary_key=True)
thread_type = db.Column(db.String(20))
__mapper_args__ = {
'polymorphic_identity': __tablename__,
'polymorphic_on': thread_type
}
# Standalone properties
title = db.Column(db.Unicode(V5Config.THREAD_NAME_MAXLEN))
# Relationships
top_comment = db.Column(db.Integer, db.ForeignKey('comment.id'))
comments = db.relationship('Comment', backref='thread')
def __init__(self, author, text, title):
""" Create a Thread """
super.__init__(author, text)
self.title = title
def __repr__(self):
return f'<Thread #{self.id}'

21
app/models/topic.py Normal file
View File

@ -0,0 +1,21 @@
from app import db
from app.models.thread import Thread
class Topic(Thread):
__tablename__ = 'topic'
id = db.Column(db.Integer, db.ForeignKey('thread.id'), primary_key=True)
__mapper_args__ = {'polymorphic_identity': __tablename__}
# Relationships
forum_id = db.Column(db.Integer, db.ForeignKey('forum.id'), nullable=False)
def __init__(self, author, text, title, forum):
""" Create a Topic """
super.__init__(author, text, title)
if isinstance(forum, Forum):
forum = forum.id
self.forum_id = forum
def __repr__(self):
return f'<Topic #{self.id}'

View File

@ -14,8 +14,10 @@ import re
import math
import app
# User: Website user that performs actions on the post
class User(UserMixin, db.Model):
""" Website user that performs actions on the post """
__tablename__ = 'user'
# User ID, should be used to refer to any user. Thea actual user can either
@ -25,7 +27,7 @@ class User(UserMixin, db.Model):
type = db.Column(db.String(30))
# TODO: add good relation
posts = db.relationship('Post', backref="author", lazy=False)
posts = db.relationship('Post', backref="author", lazy=True)
__mapper_args__ = {
'polymorphic_identity': __tablename__,
@ -35,8 +37,10 @@ class User(UserMixin, db.Model):
def __repr__(self):
return f'<User: #{self.id}>'
# Guest: Unregistered user with minimal privileges
class Guest(User, db.Model):
""" Unregistered user with minimal privileges """
__tablename__ = 'guest'
__mapper_args__ = {'polymorphic_identity': __tablename__}
@ -52,8 +56,9 @@ class Guest(User, db.Model):
return f'<Guest: {self.username} ({self.ip})>'
# Member: Registered user with full access to the website's services
class Member(User, db.Model):
""" Registered user with full access to the website's services """
__tablename__ = 'member'
__mapper_args__ = {'polymorphic_identity': __tablename__}
@ -225,7 +230,7 @@ class Member(User, db.Model):
if type(t) == int:
t = Trophy.query.get(t)
if type(t) == str:
t = Trophy.query.filter_by(name=name).first()
t = Trophy.query.filter_by(name=t).first()
if t in self.trophies:
self.trophies.remove(t)

View File

@ -7,7 +7,7 @@ def is_title(object):
"""
Check if an object is a title
"""
if type(object) == Title:
if isinstance(object, Title):
return "Oui"
else:
return "Non"

View File

@ -21,5 +21,5 @@ class V5Config(object):
USER_NAME_MAXLEN = 32
# Minimum password length for new users and new passwords
PASSWORD_MINLEN = 10
# Maximum topic name length
TOPIC_NAME_MAXLEN = 32
# Maximum thread name length
THREAD_NAME_MAXLEN = 32

View File

@ -0,0 +1,48 @@
"""Ajout des posts
Revision ID: 611667e86261
Revises: 87b039db71a5
Create Date: 2019-08-20 11:57:56.053453
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision = '611667e86261'
down_revision = '87b039db71a5'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('post',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('type', sa.String(length=20), nullable=True),
sa.Column('text', sa.Text(_expect_unicode=True), nullable=True),
sa.Column('date_created', sa.DateTime(), nullable=True),
sa.Column('date_modified', sa.DateTime(), nullable=True),
sa.Column('author_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['author_id'], ['user.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.drop_table('content')
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('content',
sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
sa.Column('type', sa.VARCHAR(length=20), autoincrement=False, nullable=True),
sa.Column('data', sa.TEXT(), autoincrement=False, nullable=True),
sa.Column('date_created', postgresql.TIMESTAMP(), autoincrement=False, nullable=True),
sa.Column('date_modified', postgresql.TIMESTAMP(), autoincrement=False, nullable=True),
sa.Column('author_id', sa.INTEGER(), autoincrement=False, nullable=True),
sa.ForeignKeyConstraint(['author_id'], ['user.id'], name='content_author_id_fkey'),
sa.PrimaryKeyConstraint('id', name='content_pkey')
)
op.drop_table('post')
# ### end Alembic commands ###

View File

@ -0,0 +1,28 @@
"""Ajout des topics/comments/autres
Revision ID: f3f6d7f7fa81
Revises: 611667e86261
Create Date: 2019-08-20 17:21:10.330435
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'f3f6d7f7fa81'
down_revision = '611667e86261'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('post', 'text')
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('post', sa.Column('text', sa.TEXT(), autoincrement=False, nullable=True))
# ### end Alembic commands ###