# coding: utf-8 from flask import Blueprint from flask_apispec import marshal_with from flask_jwt_extended import current_user, jwt_required, jwt_optional from conduit.exceptions import InvalidUsage from conduit.user.models import User from .serializers import profile_schema blueprint = Blueprint('profiles', __name__) @blueprint.route('/api/profiles/', methods=('GET',)) @jwt_optional @marshal_with(profile_schema) def get_profile(username): user = User.query.filter_by(username=username).first() if not user: raise InvalidUsage.user_not_found() return user.profile @blueprint.route('/api/profiles//follow', methods=('POST',)) @jwt_required @marshal_with(profile_schema) def follow_user(username): user = User.query.filter_by(username=username).first() if not user: raise InvalidUsage.user_not_found() current_user.profile.follow(user.profile) current_user.profile.save() return user.profile @blueprint.route('/api/profiles//follow', methods=('DELETE',)) @jwt_required @marshal_with(profile_schema) def unfollow_user(username): user = User.query.filter_by(username=username).first() if not user: raise InvalidUsage.user_not_found() current_user.profile.unfollow(user.profile) current_user.profile.save() return user.profile