def get(self, post_id): post = Post.query.filter_by(id=post_id).first() if post: post_schema = PostSchema(many=False) return post_schema.jsonify(post) else: return 404
def delete_post(id): current_post = Post.query.get_or_404(id) if current_post not in current_user.posts: abort(403) db.session.delete(current_post) db.session.commit() result = PostSchema(many=True).dump( Post.query.order_by(Post.timestamp.desc())) return jsonify(result)
def get_all_followed_posts(): """ Responsible for return user`s post data and post data of user that he follows :return: posts data """ all_posts = current_user.followed_posts() result = PostSchema(many=True).dump(all_posts) return jsonify(result)
def get_post(id): """ Responsible for return post data :param id: unique post id :return: post data """ post = Post.query.get(id) return PostSchema().jsonify(post)
def get_all_posts_explore(): """ Responsible for return all posts data in system by order :return: all posts data """ all_posts = Post.query.order_by(Post.timestamp.desc()) result = PostSchema(many=True).dump(all_posts) return jsonify(result)
def update_post(id): current_post = Post.query.get_or_404(id) data = request.get_json() or {} if current_post not in current_user.posts: abort(403) if 'body' in data: current_post.body = data['body'] if 'language' in data: current_post.language = data['language'] db.session.commit() return PostSchema().jsonify(current_post)
def post(self): current_user = get_jwt_identity() payload = request.get_json() body = payload.get('body') title = payload.get('title') try: PostSchema().load(payload) except ValidationError as vall_err: return {'message': vall_err}, 400 user = UserModel.find_by_username(current_user) post = PostModel(title=title, body=body, user_id=user.id) post.save_to_db() return {'message': 'Your post was successfully saved'}
def create_post(): """ Responsible for create new post :return: post data """ data = request.get_json() or {} if 'body' not in data: return bad_request('Body field is required') if 'language' not in data or data['language'] == 'UNKNOWN' or len( data['language']) > 5: data['language'] = '' post = Post(body=data['body'], author=current_user, language=data['language']) db.session.add(post) db.session.commit() return PostSchema().jsonify(post)
from app import db from flask_restplus import Namespace, Resource, fields from app.models import User, UserSchema, Post, Company, PostSchema from flask import jsonify, request from .dto import userDTO, postDTO api = Namespace('users', description='Users api') user_schema = UserSchema() user_schema_list = UserSchema(many=True) post_schema = PostSchema() post_schema_list = PostSchema(many=True) @api.route('/') class UsersResourceList(Resource): @api.marshal_with(userDTO, code=201) @api.expect(userDTO) def post(self): users = User.query.all() return user_schema_list.jsonify(users) @api.route('/<int:id>') class UsersResource(Resource): def get(self, id): user = User.query.get_or_404(id) return user_schema.jsonify(user) @api.route('/<int:id>/posts') class UsersResourcePosts(Resource): def get(self, id):
def getPosts(): posts = Post.query.all() post_schema = PostSchema(many=True) output = post_schema.dump(posts) return jsonify(output)
def getPosts(): posts = Post.query.all() postSchema = PostSchema() output = [postSchema.dump(i).data for i in posts] return jsonify({'data': output})
def post(self): posts = Post.query.all() post_schema = PostSchema(many=True) return post_schema.jsonify(posts)