Renommé la classe Content en Post, quelques ajouts en plus

This commit is contained in:
Darks 2019-08-19 22:36:55 +02:00
parent 4cefe39c36
commit 420117f95c
Signed by untrusted user: Darks
GPG Key ID: F61F10FA138E797C
4 changed files with 47 additions and 24 deletions

View File

@ -1,20 +0,0 @@
from datetime import datetime
from app import db
from app.models.users import *
class Content(db.Model):
__tablename__ = 'content'
id = db.Column(db.Integer, primary_key=True)
type = db.Column(db.String(20))
__mapper_args__ = {
'polymorphic_identity': __tablename__,
'polymorphic_on': type
}
# Standalone properties
data = 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)
# Relationships
author_id = db.Column(db.ForeignKey('user.id'))
author = db.relationship("User", back_populates="contents")

42
app/models/post.py Normal file
View File

@ -0,0 +1,42 @@
from datetime import datetime
from app import db
from app.models.users import *
class Post(db.Model):
__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)
# 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:
author = author.id
self.author_id = author
def update(self, text):
""" Update a post. Check whether the request sender has the right to do
this! """
self.text = text
self.date_modified = datetime.now()
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:
new_author = new_author.id
self.author_id = new_author
def __repr__(self):
return f'<Post: #{self.id}>'

View File

@ -2,7 +2,7 @@ from datetime import date
from app import db
from flask import flash
from flask_login import UserMixin
from app.models.contents import Content
from app.models.post import Post
from app.models.privs import SpecialPrivilege, Group, GroupMember, \
GroupPrivilege
from app.models.trophies import Trophy, TrophyMember
@ -14,7 +14,7 @@ import re
import math
import app
# User: Website user that performs actions on the content
# User: Website user that performs actions on the post
class User(UserMixin, db.Model):
__tablename__ = 'user'
@ -25,7 +25,7 @@ class User(UserMixin, db.Model):
type = db.Column(db.String(30))
# TODO: add good relation
contents = db.relationship('Content', back_populates="author")
posts = db.relationship('Post', backref="author", lazy=False)
__mapper_args__ = {
'polymorphic_identity': __tablename__,
@ -94,7 +94,6 @@ class Member(User, db.Model):
# Relations
trophies = db.relationship('Trophy', secondary=TrophyMember,
back_populates='owners')
# tests = db.relationship('Test', back_populates='author')
def __init__(self, name, email, password):
"""Register a new user."""

View File

@ -21,3 +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