class Profile(db.Model): __tablename__ = 'profile' id = db.Column(db.String(50), primary_key=True, unique=True, nullable=False, default=uuid.uuid4()) user_public_id = db.Column(db.String(50), db.ForeignKey('user.user_public_id')) first_name = db.Column(db.String(50), nullable=True) last_name = db.Column(db.String(50), nullable=True) nickname = db.Column(db.String(50), nullable=True) url = db.Column(db.String(100), nullable=True) bio = db.Column(db.Text) location = db.Column(db.String(50)) display_name = db.Column(db.String(50), nullable=True) status = db.Column(db.Boolean) def __init__(self, first_name, last_name, nickname, url, bio, location, display_name, status=True): self.first_name = first_name self.last_name = last_name self.nickname = nickname self.url = url self.bio = bio self.location = location self.display_name = display_name self.status = status def __repr__(self): return '<Profile {}>'.format(self.id)
class Role(db.Model): __tablename__ = 'role' id = db.Column(db.Integer, primary_key=True) role_id = db.Column(db.Integer) user_public_id = db.Column(db.String(50), db.ForeignKey('user.user_public_id'))
class Category(db.Model): __tablename__ = 'category' id = db.Column(db.String(50), primary_key=True, unique=True, nullable=False) category_public_id = db.Column(db.String(50), unique=True, nullable=False) category_name = db.Column(db.String(100), unique=True, nullable=False) category_name_slug = db.Column(db.String(100), unique=True, nullable=False) created_at = db.Column(db.DateTime, nullable=False) post = db.relationship('Post', backref='post', uselist=False) def __init__(self, category_name, category_name_slug): self.id = uuid.uuid4() self.category_public_id = uuid.uuid4() self.category_name = category_name self.category_name_slug = slugify(category_name) self.created_at = datetime.utcnow() def __repr__(self): return '<Category {} {}>'.format(self.category_name, self.category_public_id)
class User(db.Model): __tablename__ = 'user' id = db.Column(db.String(50), primary_key=True, unique=True, nullable=False) user_public_id = db.Column(db.String(50), unique=True, nullable=False) username = db.Column(db.String(50), unique=True, index=True, nullable=False) email = db.Column(db.String(255), index=True, unique=True, nullable=False) password = db.Column(db.String(255), nullable=False) admin = db.Column(db.Boolean, nullable=False, default=False) last_activity = db.Column(db.DateTime, nullable=False, default=datetime.utcnow()) registered_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow()) confirmed = db.Column(db.Boolean, nullable=False, default=False) confirmed_at = db.Column(db.DateTime, default=None) role = db.relationship('Role', cascade="all,delete", backref='user') post = db.relationship('Post', backref='user', lazy=True) profile = db.relationship('Profile', backref='profile', uselist=False) comment = db.relationship('Comment', backref='parent', lazy=True) def __init__(self, username, email, password, admin, confirmed): self.id = uuid.uuid4() self.user_public_id = uuid.uuid4() self.username = username self.email = email self.password = password self.admin = admin self.confirmed = confirmed def __repr__(self): return '<User {}>'.format(self.username)
class Comment(db.Model): __tablename__ = 'comment' id = db.Column(db.String(50), primary_key=True, unique=True, nullable=False) post_public_id = db.Column(db.String(50), nullable=False) user_public_id = db.Column(db.String(50), db.ForeignKey('user.user_public_id')) score = db.Column(db.Integer, nullable=False, default=0) text = db.Column(db.Text) published_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow()) # FOREIGN KEYS # # `user_public_id` # # END FOREIGN KEYS # def __init__(self, user_public_id, post_public_id, score, text): self.id = uuid.uuid4() self.user_public_id = user_public_id self.post_public_id = post_public_id self.score = score self.text = text def __repr__(self): return '<ID {}>'.format(self.id)
class Post(db.Model): __tablename__ = 'post' id = db.Column(db.String(50), primary_key=True, unique=True, nullable=False) post_public_id = db.Column(db.String(50), unique=True, nullable=False) user_public_id = db.Column(db.String(50), db.ForeignKey('user.user_public_id')) category_public_id = db.Column( db.String(50), db.ForeignKey('category.category_public_id')) score = db.Column(db.Integer, nullable=False, default=0) title = db.Column(db.Text) title_slug = db.Column(db.String(100), unique=True, nullable=False) content = db.Column(db.Text) comment_count = db.Column(db.Integer, default=0) published_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow()) updated_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow()) def __init__(self, user_public_id, category_public_id, title, title_slug, content): self.id = uuid.uuid4() self.user_public_id = user_public_id self.post_public_id = uuid.uuid4() self.category_public_id = category_public_id self.title = title, self.title_slug = slugify(title) self.content = content def __repr__(self): return '<Post {}>'.format(self.post_public_id)