from flask import redirect from flask_login import login_required, current_user from flask_restplus import Resource from flask_restplus.namespace import Namespace from restplus.api.v1.helpers import check_id_availability from restplus.api.v1.posts.single_post import SinglePost from restplus.api.v1.users.helpers import extract_post_data, patch_post, generate_post_output from restplus.api.v1.users.single_user_all_posts import post_model from restplus.models import users_list, posts_list, Post, User users_ns = Namespace('users') class SingleUserSinglePost(Resource): def get(self, user_id: int, post_id: int): """ View a single post from a specific user """ check_id_availability(self, user_id, users_list, str(User.__name__)) check_id_availability(self, post_id, posts_list, str(Post.__name__)) for post in posts_list: if post.user_id == user_id and post.id == post_id: return redirect(self.api.url_for(SinglePost, post_id=post_id)) else: users_ns.abort(400, 'the requested user does not own this post') @login_required @users_ns.expect(post_model) @users_ns.response(200, 'Post modified successfully') @users_ns.response(400, 'Bad request')
from flask_restplus.namespace import Namespace from davepostAPI.api.v1.users.all_users import AllUsers from davepostAPI.api.v1.users.single_user import SingleUser from davepostAPI.api.v1.users.single_user_all_posts import SingleUserAllPosts, post_model from davepostAPI.api.v1.users.single_user_single_post import SingleUserSinglePost users_ns = Namespace('users', description='Operations related to users') users_ns.add_resource(AllUsers, '/') users_ns.add_resource(SingleUser, '/<int:user_id>') users_ns.add_resource(SingleUserAllPosts, '/<int:user_id>/posts') users_ns.add_model('post_model', post_model) users_ns.add_resource(SingleUserSinglePost, '/<int:user_id>/posts/<int:post_id>')
from flask_login import login_user, LoginManager, current_user from flask_restplus import Resource, fields from flask_restplus.namespace import Namespace from restplus.api.v1.auth.helpers import extract_auth_data, generate_auth_output from restplus.models import users_list auth_ns = Namespace('auth') login_manager = LoginManager() user_login_model = auth_ns.model( 'user_login', { 'email': fields.String(title='Your email address', required=True, example='*****@*****.**'), 'password': fields.String(title='Your email address', required=True, example='password.Pa55word') }) @login_manager.user_loader def load_user(user_id): for a_user in users_list: # In the session, user_id is stored as a unicode character # The chr() converts the int id of the user found to unicode for comparing equality if chr(a_user.id) == user_id: return a_user
from flask_restplus import Resource from flask_restplus.namespace import Namespace from restplus.api.v1.helpers import safe_post_output from restplus.models import posts_list posts_ns = Namespace('posts') class AllPosts(Resource): def get(self): """ View all posts :return: a list of all posts under 'posts' :rtype dict """ posts = [] for post in posts_list: posts.append(safe_post_output(self, post)) return dict(posts=posts)
from flask_restplus.namespace import Namespace from restplus.api.v1.auth.login import Login, user_login_model from restplus.api.v1.auth.logout import Logout from restplus.api.v1.auth.register import Register, user_register_model auth_ns = Namespace('auth', description='Operations related to authentication') auth_ns.add_resource(Register, '/register', endpoint='auth_register') auth_ns.add_model('user_register', user_register_model) auth_ns.add_resource(Login, '/login') auth_ns.add_model('user_login', user_login_model) auth_ns.add_resource(Logout, '/logout')
class Namespaces: articles = Namespace(name='/articles', description='Operations related to articles')
from flask_restplus.namespace import Namespace ns_auth = Namespace('auth', description='Auth operations') ns_blog = Namespace('blog', description='Blog operations') ns_me = Namespace('me', description='Me operations') ns_user = Namespace('users', description='User operations') namespaces = [ns_auth, ns_blog, ns_me, ns_user]
from flask_restplus.namespace import Namespace from davepostAPI.api.v1.posts.all_posts import AllPosts from davepostAPI.api.v1.posts.single_post import SinglePost posts_ns = Namespace('posts', description='Operations on posts only') posts_ns.add_resource(AllPosts, '/') posts_ns.add_resource(SinglePost, '/<int:post_id>')