Standardize Backend Serialization (#175)

This commit is contained in:
AMStrix 2018-11-04 12:33:22 -06:00 committed by Daniel Ternyak
parent c3649d322b
commit 2e743cc5a5
6 changed files with 33 additions and 31 deletions

View File

@ -1,5 +1,5 @@
from functools import wraps
from flask import Blueprint, g, jsonify, session
from flask import Blueprint, g, session
from flask_yoloapi import endpoint, parameter
from hashlib import sha256
from uuid import uuid4

View File

@ -1,5 +1,5 @@
from flask import Blueprint, jsonify
from animal_case import animalify
from flask import Blueprint
from flask_yoloapi import endpoint
from .models import Comment, comments_schema
@ -7,7 +7,8 @@ blueprint = Blueprint("comment", __name__, url_prefix="/api/v1/comment")
@blueprint.route("/", methods=["GET"])
@endpoint.api()
def get_comments():
all_comments = Comment.query.all()
result = comments_schema.dump(all_comments)
return jsonify(animalify(result))
return result

View File

@ -1,5 +1,4 @@
from flask import Blueprint, jsonify
from animal_case import animalify
from flask import Blueprint
from flask_yoloapi import endpoint, parameter
from .models import EmailVerification, db
@ -24,7 +23,7 @@ def verify_email(code):
@endpoint.api()
def unsubscribe_email():
ev = EmailVerification.query.filter_by(code=code).first()
if ev:
if ev:
return {"message": "Not yet implemented"}, 500
else:
return {"message": "Invalid email code"}, 400

View File

@ -1,6 +1,5 @@
from flask import Blueprint, jsonify
from animal_case import animalify
from flask import Blueprint
from flask_yoloapi import endpoint
from .models import Milestone, milestones_schema
@ -8,7 +7,8 @@ blueprint = Blueprint('milestone', __name__, url_prefix='/api/v1/milestones')
@blueprint.route("/", methods=["GET"])
def get_users():
@endpoint.api()
def get_milestones():
milestones = Milestone.query.all()
result = milestones_schema.dump(milestones)
return jsonify(animalify(result))
return result

View File

@ -1,7 +1,6 @@
from datetime import datetime
from animal_case import animalify
from flask import Blueprint, jsonify
from flask import Blueprint
from flask_yoloapi import endpoint, parameter
from sqlalchemy.exc import IntegrityError
@ -14,27 +13,29 @@ blueprint = Blueprint("proposal", __name__, url_prefix="/api/v1/proposals")
@blueprint.route("/<proposal_id>", methods=["GET"])
@endpoint.api()
def get_proposal(proposal_id):
proposal = Proposal.query.filter_by(proposal_id=proposal_id).first()
if proposal:
dumped_proposal = proposal_schema.dump(proposal)
return jsonify(animalify(dumped_proposal))
return dumped_proposal
else:
return jsonify(message="No proposal matching id"), 404
return {"message": "No proposal matching id"}, 404
@blueprint.route("/<proposal_id>/comments", methods=["GET"])
@endpoint.api()
def get_proposal_comments(proposal_id):
proposal = Proposal.query.filter_by(proposal_id=proposal_id).first()
if proposal:
dumped_proposal = proposal_schema.dump(proposal)
return jsonify(animalify(
proposal_id=proposal_id,
total_comments=len(dumped_proposal["comments"]),
comments=dumped_proposal["comments"]
))
return {
"proposalId": proposal_id,
"totalComments": len(dumped_proposal["comments"]),
"comments": dumped_proposal["comments"]
}
else:
return jsonify(message="No proposal matching id", _statusCode=404)
return {"message": "No proposal matching id"}, 404
@blueprint.route("/<proposal_id>/comments", methods=["POST"])
@ -72,8 +73,8 @@ def get_proposals(stage):
if stage:
proposals = (
Proposal.query.filter_by(stage=stage)
.order_by(Proposal.date_created.desc())
.all()
.order_by(Proposal.date_created.desc())
.all()
)
else:
proposals = Proposal.query.order_by(Proposal.date_created.desc()).all()

View File

@ -1,5 +1,4 @@
from animal_case import animalify
from flask import Blueprint, g, jsonify
from flask import Blueprint, g
from flask_yoloapi import endpoint, parameter
@ -27,20 +26,22 @@ def get_users(proposal_id):
@blueprint.route("/me", methods=["GET"])
@requires_sm
@endpoint.api()
def get_me():
dumped_user = user_schema.dump(g.current_user)
return jsonify(animalify(dumped_user))
return dumped_user
@blueprint.route("/<user_identity>", methods=["GET"])
@endpoint.api()
def get_user(user_identity):
user = User.get_by_email_or_account_address(email_address=user_identity, account_address=user_identity)
if user:
result = user_schema.dump(user)
return jsonify(animalify(result))
return result
else:
return jsonify(
message="User with account_address or user_identity matching {} not found".format(user_identity)), 404
message = "User with account_address or user_identity matching {} not found".format(user_identity)
return {"message": message}, 404
@blueprint.route("/", methods=["POST"])
@ -71,7 +72,7 @@ def create_user(account_address, email_address, display_name, title):
parameter('displayName', type=str, required=False),
parameter('title', type=str, required=False),
parameter('socialMedias', type=list, required=False),
parameter('avatar', type=dict, required=False)
parameter('avatar', type=dict, required=False),
)
def update_user(user_identity, display_name, title, social_medias, avatar):
user = User.get_by_email_or_account_address(email_address=user_identity, account_address=user_identity)