generated from Blog/Python-Flask-Template
Initial commit
This commit is contained in:
23
Models/Post.py
Normal file
23
Models/Post.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from app import db, ma
|
||||
from Models.User import User
|
||||
from Models.Tag import Tag
|
||||
|
||||
tags_posts = db.Table('tags_posts_mapping',
|
||||
db.Column('tag_id', db.Integer, db.ForeignKey('tags.id'), primary_key=True),
|
||||
db.Column('post_id', db.Integer, db.ForeignKey('post.id'), primary_key=True)
|
||||
)
|
||||
|
||||
|
||||
class Post(db.Model):
|
||||
__tablename__ = "post"
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
title = db.Column(db.String(50))
|
||||
content = db.Column(db.String(255))
|
||||
tags = db.relationship('Tag', secondary=tags_posts, lazy='subquery',
|
||||
backref=db.backref('posts', lazy=True))
|
||||
author_id = db.Column(db.Integer, db.ForeignKey("user.id"))
|
||||
author = db.relationship("User", backref="posts")
|
||||
|
||||
def __repr__(self):
|
||||
return '<Post %s>' % self.title
|
||||
|
33
Models/Schema.py
Normal file
33
Models/Schema.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from marshmallow_sqlalchemy import SQLAlchemyAutoSchema
|
||||
from Models.User import User
|
||||
from Models.Post import Post
|
||||
from Models.Tag import Tag
|
||||
|
||||
class UserSchema(SQLAlchemyAutoSchema):
|
||||
class Meta:
|
||||
model = User
|
||||
exclude = ("password",)
|
||||
include_fk = True
|
||||
include_relationships = True
|
||||
load_instance = True
|
||||
|
||||
class PostSchema(SQLAlchemyAutoSchema):
|
||||
class Meta:
|
||||
model= Post
|
||||
include_fk = True
|
||||
include_relationships = True
|
||||
load_instance = True
|
||||
|
||||
class TagSchema(SQLAlchemyAutoSchema):
|
||||
class Meta:
|
||||
model= Tag
|
||||
include_fk = True
|
||||
include_relationships = True
|
||||
load_instance = True
|
||||
|
||||
user_schema = UserSchema()
|
||||
users_schema = UserSchema(many=True)
|
||||
post_schema = PostSchema()
|
||||
posts_schema = PostSchema(many=True)
|
||||
tag_schema = TagSchema()
|
||||
tags_schema = TagSchema(many=True)
|
12
Models/Tag.py
Normal file
12
Models/Tag.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from app import db, ma
|
||||
|
||||
class Tag(db.Model):
|
||||
__tablename__ = "tags"
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(50))
|
||||
|
||||
|
||||
def __repr__(self):
|
||||
return '<Tag %s>' % self.name
|
||||
|
||||
|
16
Models/User.py
Normal file
16
Models/User.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from app import db, ma
|
||||
from werkzeug.security import check_password_hash
|
||||
|
||||
class User(db.Model):
|
||||
__tablename__ = "user"
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(50))
|
||||
email = db.Column(db.String(255))
|
||||
password = db.Column(db.String(255))
|
||||
|
||||
def __repr__(self):
|
||||
return '<User %s>' % self.name
|
||||
|
||||
def check_password(self, password):
|
||||
return check_password_hash(self.password, password)
|
||||
|
Reference in New Issue
Block a user