From 267b9b0a0232d0d977f33fa9e85b096ee1862e8d Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Mon, 5 Feb 2018 23:00:30 +0100 Subject: [PATCH] Add marshmallow --- Pipfile | 2 ++ app.py | 48 +++++++++++++++++++++++++++++++++++++++++------- models.py | 14 ++++---------- 3 files changed, 47 insertions(+), 17 deletions(-) diff --git a/Pipfile b/Pipfile index 19f4e5a..91f86e2 100644 --- a/Pipfile +++ b/Pipfile @@ -21,3 +21,5 @@ flask-script = "*" flask-migrate = "*" faker = "*" flask-restplus-patched = "*" +flask-marshmallow = "*" +marshmallow-sqlalchemy = "*" diff --git a/app.py b/app.py index 52b228d..277e5e3 100644 --- a/app.py +++ b/app.py @@ -7,11 +7,13 @@ from flask import Flask, jsonify, request from flask_restplus import Resource, Api from dotenv import load_dotenv, find_dotenv from models import db, User, News, Comment +from flask_marshmallow import Marshmallow # Extensions initialization # ========================= load_dotenv(find_dotenv()) app = Flask(__name__) +ma = Marshmallow(app) api = Api(app) @@ -24,27 +26,60 @@ db.init_app(app) 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') class Signup(Resource): def post(self): return {'hello': 'world'} - +# Login @api.route(PRE_URL + 'login') class Login(Resource): def post(self): return {'hello': 'world'} - +# Logout @api.route(PRE_URL + 'login') class Logout(Resource): def get(self): 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/') +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') class NewsList(Resource): @@ -63,6 +98,7 @@ class News_single(Resource): return {'hello': id} +# Comment @api.route(PRE_URL + 'news//comments') class Comments(Resource): @@ -73,10 +109,8 @@ class Comments(Resource): return {'hello': 'world'} -def serializeQuery(query): - return jsonify([i.serialize for i in query]) - - +# Run +# ========================= 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) diff --git a/models.py b/models.py index 5ad80b0..7e516c2 100644 --- a/models.py +++ b/models.py @@ -45,7 +45,9 @@ class News(db.Model): ''' Table News ''' + __tablename__ = 'news' + id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100)) link = db.Column(db.String(500)) @@ -58,16 +60,6 @@ class News(db.Model): user = db.relationship( '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): return ''.format(self.title) @@ -76,7 +68,9 @@ class Comment(db.Model): ''' Table Comment ''' + __tablename__ = 'comments' + id = db.Column(db.Integer, primary_key=True) text = db.Column(db.String(1000)) news_id = db.Column(