Update
This commit is contained in:
parent
267b9b0a02
commit
40a9963eee
83
app.py
83
app.py
@ -1,14 +1,16 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# =========================
|
||||||
# Librarys
|
# Librarys
|
||||||
# =========================
|
# =========================
|
||||||
import os
|
import os
|
||||||
from flask import Flask, jsonify, request
|
from flask import Flask, request
|
||||||
from flask_restplus import Resource, Api
|
from flask_restplus import Resource, Api
|
||||||
from dotenv import load_dotenv, find_dotenv
|
from dotenv import load_dotenv, find_dotenv
|
||||||
from models import db, User, News, Comment
|
from models import db, User, Notice, Comment
|
||||||
from flask_marshmallow import Marshmallow
|
from flask_marshmallow import Marshmallow
|
||||||
|
|
||||||
|
# =========================
|
||||||
# Extensions initialization
|
# Extensions initialization
|
||||||
# =========================
|
# =========================
|
||||||
load_dotenv(find_dotenv())
|
load_dotenv(find_dotenv())
|
||||||
@ -16,7 +18,7 @@ app = Flask(__name__)
|
|||||||
ma = Marshmallow(app)
|
ma = Marshmallow(app)
|
||||||
api = Api(app)
|
api = Api(app)
|
||||||
|
|
||||||
|
# =========================
|
||||||
# Configurations
|
# Configurations
|
||||||
# =========================
|
# =========================
|
||||||
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
|
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
|
||||||
@ -25,10 +27,11 @@ app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
|||||||
db.init_app(app)
|
db.init_app(app)
|
||||||
PRE_URL = '/api/v1/'
|
PRE_URL = '/api/v1/'
|
||||||
|
|
||||||
|
# =========================
|
||||||
# Schemas
|
# Schemas
|
||||||
# =========================
|
# =========================
|
||||||
|
|
||||||
|
|
||||||
# User
|
# User
|
||||||
class UserSchema(ma.Schema):
|
class UserSchema(ma.Schema):
|
||||||
class Meta:
|
class Meta:
|
||||||
@ -39,9 +42,44 @@ class UserSchema(ma.Schema):
|
|||||||
user_schema = UserSchema()
|
user_schema = UserSchema()
|
||||||
users_schema = UserSchema(many=True)
|
users_schema = UserSchema(many=True)
|
||||||
|
|
||||||
|
|
||||||
|
# Notice
|
||||||
|
class NoticeSchema(ma.Schema):
|
||||||
|
class Meta:
|
||||||
|
# Fields to expose
|
||||||
|
fields = ('title', 'link', 'user_id', '_links')
|
||||||
|
|
||||||
|
_links = ma.Hyperlinks({
|
||||||
|
'comments': ma.URLFor('comments', id='<id>'),
|
||||||
|
'user': ma.URLFor('user_single', id='<user_id>')
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
notice_schema = NoticeSchema()
|
||||||
|
news_schema = NoticeSchema(many=True)
|
||||||
|
|
||||||
|
|
||||||
|
# Comment
|
||||||
|
class CommentSchema(ma.Schema):
|
||||||
|
class Meta:
|
||||||
|
# Fields to expose
|
||||||
|
fields = ('text', '_links')
|
||||||
|
|
||||||
|
_links = ma.Hyperlinks({
|
||||||
|
'notice': ma.URLFor('news_single', id='<id>'),
|
||||||
|
'user': ma.URLFor('user_single', id='<user_id>')
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
comment_schema = CommentSchema()
|
||||||
|
comments_schema = CommentSchema(many=True)
|
||||||
|
|
||||||
|
|
||||||
|
# =========================
|
||||||
# Routes
|
# Routes
|
||||||
# =========================
|
# =========================
|
||||||
|
|
||||||
|
|
||||||
# Signup
|
# Signup
|
||||||
@api.route(PRE_URL + 'signup')
|
@api.route(PRE_URL + 'signup')
|
||||||
class Signup(Resource):
|
class Signup(Resource):
|
||||||
@ -49,6 +87,7 @@ class Signup(Resource):
|
|||||||
def post(self):
|
def post(self):
|
||||||
return {'hello': 'world'}
|
return {'hello': 'world'}
|
||||||
|
|
||||||
|
|
||||||
# Login
|
# Login
|
||||||
@api.route(PRE_URL + 'login')
|
@api.route(PRE_URL + 'login')
|
||||||
class Login(Resource):
|
class Login(Resource):
|
||||||
@ -56,13 +95,15 @@ class Login(Resource):
|
|||||||
def post(self):
|
def post(self):
|
||||||
return {'hello': 'world'}
|
return {'hello': 'world'}
|
||||||
|
|
||||||
|
|
||||||
# Logout
|
# Logout
|
||||||
@api.route(PRE_URL + 'login')
|
@api.route(PRE_URL + 'logout')
|
||||||
class Logout(Resource):
|
class Logout(Resource):
|
||||||
|
|
||||||
def get(self):
|
def get(self):
|
||||||
return {'hello': 'world'}
|
return {'hello': 'world'}
|
||||||
|
|
||||||
|
|
||||||
# User
|
# User
|
||||||
@api.route(PRE_URL + 'user')
|
@api.route(PRE_URL + 'user')
|
||||||
class UserList(Resource):
|
class UserList(Resource):
|
||||||
@ -79,36 +120,48 @@ class UserSingle(Resource):
|
|||||||
all_users = User.query.get(id)
|
all_users = User.query.get(id)
|
||||||
return user_schema.jsonify(all_users)
|
return user_schema.jsonify(all_users)
|
||||||
|
|
||||||
# News
|
|
||||||
@api.route(PRE_URL + 'news')
|
# Notice
|
||||||
class NewsList(Resource):
|
@api.route(PRE_URL + 'notice')
|
||||||
|
class NoticeList(Resource):
|
||||||
|
|
||||||
def get(self):
|
def get(self):
|
||||||
my_news = News.query.all()
|
my_news = Notice.query.all()
|
||||||
return serializeQuery(my_news)
|
return news_schema.jsonify(my_news)
|
||||||
|
|
||||||
def post(self):
|
def post(self):
|
||||||
return request.form
|
return request.form
|
||||||
|
|
||||||
|
|
||||||
@api.route(PRE_URL + 'news/<int:id>')
|
@api.route(PRE_URL + 'notice/<int:id>')
|
||||||
class News_single(Resource):
|
class NewsSingle(Resource):
|
||||||
|
|
||||||
def get(self, id):
|
def get(self, id):
|
||||||
return {'hello': id}
|
my_notice = Notice.query.get(id)
|
||||||
|
return notice_schema.jsonify(my_notice)
|
||||||
|
|
||||||
|
def path(self, id):
|
||||||
|
my_notice = Notice.query.get(id)
|
||||||
|
return notice_schema.jsonify(my_notice)
|
||||||
|
|
||||||
|
def delete(self, id):
|
||||||
|
my_notice = Notice.query.get(id)
|
||||||
|
return notice_schema.jsonify(my_notice)
|
||||||
|
|
||||||
|
|
||||||
# Comment
|
# Comment
|
||||||
@api.route(PRE_URL + 'news/<int:id>/comments')
|
@api.route(PRE_URL + 'notice/<int:id>/comments')
|
||||||
class Comments(Resource):
|
class Comments(Resource):
|
||||||
|
|
||||||
def get(self, id):
|
def get(self, id):
|
||||||
return {'hello': 'world'}
|
my_comments = Comment.query.filter_by(notice_id=id).all()
|
||||||
|
return comments_schema.jsonify(my_comments)
|
||||||
|
|
||||||
def post(self, id):
|
def post(self, id):
|
||||||
return {'hello': 'world'}
|
return {'hello': 'world'}
|
||||||
|
|
||||||
|
|
||||||
|
# =========================
|
||||||
# Run
|
# Run
|
||||||
# =========================
|
# =========================
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
10
fake_data.py
10
fake_data.py
@ -1,5 +1,5 @@
|
|||||||
from werkzeug.security import generate_password_hash
|
from werkzeug.security import generate_password_hash
|
||||||
from models import db, User, News, Comment
|
from models import db, User, Notice, Comment
|
||||||
from faker import Factory
|
from faker import Factory
|
||||||
from random import randint
|
from random import randint
|
||||||
|
|
||||||
@ -28,18 +28,18 @@ for num in range(1000):
|
|||||||
link = fake.uri()
|
link = fake.uri()
|
||||||
user_id = randint(1, 100)
|
user_id = randint(1, 100)
|
||||||
# Save in database
|
# Save in database
|
||||||
my_news = News(title=title, link=link, user_id=user_id)
|
my_notice = Notice(title=title, link=link, user_id=user_id)
|
||||||
db.session.add(my_news)
|
db.session.add(my_notice)
|
||||||
|
|
||||||
print('News created')
|
print('News created')
|
||||||
|
|
||||||
# Make 10000 fake comments
|
# Make 10000 fake comments
|
||||||
for num in range(10000):
|
for num in range(10000):
|
||||||
text = fake.text()
|
text = fake.text()
|
||||||
news_id = randint(1, 1000)
|
notice_id = randint(1, 1000)
|
||||||
user_id = randint(1, 100)
|
user_id = randint(1, 100)
|
||||||
# Save in database
|
# Save in database
|
||||||
my_comment = Comment(text=text, news_id=news_id, user_id=user_id)
|
my_comment = Comment(text=text, notice_id=notice_id, user_id=user_id)
|
||||||
db.session.add(my_comment)
|
db.session.add(my_comment)
|
||||||
|
|
||||||
print('Comments created')
|
print('Comments created')
|
||||||
|
14
models.py
14
models.py
@ -41,9 +41,9 @@ class User(db.Model):
|
|||||||
return '<User Table {0}>'.format(self.username)
|
return '<User Table {0}>'.format(self.username)
|
||||||
|
|
||||||
|
|
||||||
class News(db.Model):
|
class Notice(db.Model):
|
||||||
'''
|
'''
|
||||||
Table News
|
Table Notice
|
||||||
'''
|
'''
|
||||||
|
|
||||||
__tablename__ = 'news'
|
__tablename__ = 'news'
|
||||||
@ -58,10 +58,10 @@ class News(db.Model):
|
|||||||
|
|
||||||
# Relations
|
# Relations
|
||||||
user = db.relationship(
|
user = db.relationship(
|
||||||
'User', backref=db.backref('News', lazy=True))
|
'User', backref=db.backref('Notice', lazy=True))
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<News Table {0}>'.format(self.title)
|
return '<Notice Table {0}>'.format(self.title)
|
||||||
|
|
||||||
|
|
||||||
class Comment(db.Model):
|
class Comment(db.Model):
|
||||||
@ -73,7 +73,7 @@ class Comment(db.Model):
|
|||||||
|
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
text = db.Column(db.String(1000))
|
text = db.Column(db.String(1000))
|
||||||
news_id = db.Column(
|
notice_id = db.Column(
|
||||||
db.Integer, db.ForeignKey('news.id'), nullable=False)
|
db.Integer, db.ForeignKey('news.id'), nullable=False)
|
||||||
user_id = db.Column(
|
user_id = db.Column(
|
||||||
db.Integer, db.ForeignKey('users.id'), nullable=False)
|
db.Integer, db.ForeignKey('users.id'), nullable=False)
|
||||||
@ -83,8 +83,8 @@ class Comment(db.Model):
|
|||||||
# Relations
|
# Relations
|
||||||
user = db.relationship(
|
user = db.relationship(
|
||||||
'User', backref=db.backref('Comment', lazy=True))
|
'User', backref=db.backref('Comment', lazy=True))
|
||||||
news = db.relationship(
|
notice = db.relationship(
|
||||||
'News', backref=db.backref('Comment', lazy=True))
|
'Notice', backref=db.backref('Comment', lazy=True))
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<Comment Table {0}>'.format(self.id)
|
return '<Comment Table {0}>'.format(self.id)
|
||||||
|
Loading…
Reference in New Issue
Block a user