This commit is contained in:
Andros Fenollosa
2018-02-05 23:59:51 +01:00
parent 267b9b0a02
commit 40a9963eee
3 changed files with 80 additions and 27 deletions

83
app.py
View File

@ -1,14 +1,16 @@
# -*- coding: utf-8 -*-
# =========================
# Librarys
# =========================
import os
from flask import Flask, jsonify, request
from flask import Flask, request
from flask_restplus import Resource, Api
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
# =========================
# Extensions initialization
# =========================
load_dotenv(find_dotenv())
@ -16,7 +18,7 @@ app = Flask(__name__)
ma = Marshmallow(app)
api = Api(app)
# =========================
# Configurations
# =========================
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
@ -25,10 +27,11 @@ app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
PRE_URL = '/api/v1/'
# =========================
# Schemas
# =========================
# User
class UserSchema(ma.Schema):
class Meta:
@ -39,9 +42,44 @@ class UserSchema(ma.Schema):
user_schema = UserSchema()
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
# =========================
# Signup
@api.route(PRE_URL + 'signup')
class Signup(Resource):
@ -49,6 +87,7 @@ class Signup(Resource):
def post(self):
return {'hello': 'world'}
# Login
@api.route(PRE_URL + 'login')
class Login(Resource):
@ -56,13 +95,15 @@ class Login(Resource):
def post(self):
return {'hello': 'world'}
# Logout
@api.route(PRE_URL + 'login')
@api.route(PRE_URL + 'logout')
class Logout(Resource):
def get(self):
return {'hello': 'world'}
# User
@api.route(PRE_URL + 'user')
class UserList(Resource):
@ -79,36 +120,48 @@ class UserSingle(Resource):
all_users = User.query.get(id)
return user_schema.jsonify(all_users)
# News
@api.route(PRE_URL + 'news')
class NewsList(Resource):
# Notice
@api.route(PRE_URL + 'notice')
class NoticeList(Resource):
def get(self):
my_news = News.query.all()
return serializeQuery(my_news)
my_news = Notice.query.all()
return news_schema.jsonify(my_news)
def post(self):
return request.form
@api.route(PRE_URL + 'news/<int:id>')
class News_single(Resource):
@api.route(PRE_URL + 'notice/<int:id>')
class NewsSingle(Resource):
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
@api.route(PRE_URL + 'news/<int:id>/comments')
@api.route(PRE_URL + 'notice/<int:id>/comments')
class Comments(Resource):
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):
return {'hello': 'world'}
# =========================
# Run
# =========================
if __name__ == '__main__':