Add marshmallow

This commit is contained in:
Andros Fenollosa 2018-02-05 23:00:30 +01:00
parent 479a987b75
commit 267b9b0a02
3 changed files with 47 additions and 17 deletions

View File

@ -21,3 +21,5 @@ flask-script = "*"
flask-migrate = "*" flask-migrate = "*"
faker = "*" faker = "*"
flask-restplus-patched = "*" flask-restplus-patched = "*"
flask-marshmallow = "*"
marshmallow-sqlalchemy = "*"

48
app.py
View File

@ -7,11 +7,13 @@ from flask import Flask, jsonify, 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, News, Comment
from flask_marshmallow import Marshmallow
# Extensions initialization # Extensions initialization
# ========================= # =========================
load_dotenv(find_dotenv()) load_dotenv(find_dotenv())
app = Flask(__name__) app = Flask(__name__)
ma = Marshmallow(app)
api = Api(app) api = Api(app)
@ -24,27 +26,60 @@ db.init_app(app)
PRE_URL = '/api/v1/' PRE_URL = '/api/v1/'
# Schemas
# =========================
# User
class UserSchema(ma.Schema):
class Meta:
# Fields to expose
fields = ('username', 'mail')
user_schema = UserSchema()
users_schema = UserSchema(many=True)
# Routes
# =========================
# Signup
@api.route(PRE_URL + 'signup') @api.route(PRE_URL + 'signup')
class Signup(Resource): class Signup(Resource):
def post(self): def post(self):
return {'hello': 'world'} return {'hello': 'world'}
# Login
@api.route(PRE_URL + 'login') @api.route(PRE_URL + 'login')
class Login(Resource): class Login(Resource):
def post(self): def post(self):
return {'hello': 'world'} return {'hello': 'world'}
# Logout
@api.route(PRE_URL + 'login') @api.route(PRE_URL + 'login')
class Logout(Resource): class Logout(Resource):
def get(self): def get(self):
return {'hello': 'world'} return {'hello': 'world'}
# User
@api.route(PRE_URL + 'user')
class UserList(Resource):
def get(self):
all_users = User.query.all()
return users_schema.jsonify(all_users)
@api.route(PRE_URL + 'user/<int:id>')
class UserSingle(Resource):
def get(self, id):
all_users = User.query.get(id)
return user_schema.jsonify(all_users)
# News
@api.route(PRE_URL + 'news') @api.route(PRE_URL + 'news')
class NewsList(Resource): class NewsList(Resource):
@ -63,6 +98,7 @@ class News_single(Resource):
return {'hello': id} return {'hello': id}
# Comment
@api.route(PRE_URL + 'news/<int:id>/comments') @api.route(PRE_URL + 'news/<int:id>/comments')
class Comments(Resource): class Comments(Resource):
@ -73,10 +109,8 @@ class Comments(Resource):
return {'hello': 'world'} return {'hello': 'world'}
def serializeQuery(query): # Run
return jsonify([i.serialize for i in query]) # =========================
if __name__ == '__main__': if __name__ == '__main__':
app.run(debug=os.environ.get('DEBUG') == 'True' if True else False) app.run(debug=True if os.environ.get('DEBUG') == 'True' else False)

View File

@ -45,7 +45,9 @@ class News(db.Model):
''' '''
Table News Table News
''' '''
__tablename__ = 'news' __tablename__ = 'news'
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100)) title = db.Column(db.String(100))
link = db.Column(db.String(500)) link = db.Column(db.String(500))
@ -58,16 +60,6 @@ class News(db.Model):
user = db.relationship( user = db.relationship(
'User', backref=db.backref('News', lazy=True)) 'User', backref=db.backref('News', lazy=True))
@property
def serialize(self):
"""Return object data in easily serializeable format"""
return {
'id': self.id,
'title': self.title,
'link': self.link,
'user_id': self.user_id
}
def __repr__(self): def __repr__(self):
return '<News Table {0}>'.format(self.title) return '<News Table {0}>'.format(self.title)
@ -76,7 +68,9 @@ class Comment(db.Model):
''' '''
Table Comment Table Comment
''' '''
__tablename__ = 'comments' __tablename__ = 'comments'
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( news_id = db.Column(