Coverage for demo_server/api/blog/endpoints/posts.py: 100%

45 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2026-10-07 05:12 +0000

1# Copyright (c) Microsoft Corporation. 

2# Licensed under the MIT License. 

3 

4import logging 

5 

6from flask import request 

7from flask_restplus import Resource 

8from demo_server.api.blog.business import create_blog_post, update_post, delete_post, get_post 

9from demo_server.api.blog.serializers import blog_post, blog_post_public,\ 

10 page_of_blog_posts 

11from demo_server.api.blog.parsers import pagination_arguments 

12from demo_server.api.restplus import api 

13from demo_server.database.models import Post 

14 

15log = logging.getLogger(__name__) 

16 

17ns = api.namespace('blog/posts', description='Operations related to blog posts') 

18 

19 

20@ns.route('') 

21class PostsCollection(Resource): 

22 

23 @api.expect(pagination_arguments) 

24 @api.marshal_with(page_of_blog_posts) 

25 def get(self): 

26 """ 

27 Returns a list of blog posts. 

28 """ 

29 args = pagination_arguments.parse_args(request) 

30 page = args.get('page', 1) 

31 per_page = args.get('per_page', 1) 

32 posts_query = Post.query 

33 posts_page = posts_query.paginate(page, per_page) 

34 

35 return posts_page 

36 

37 @api.expect(blog_post_public) 

38 @api.marshal_with(blog_post_public) 

39 def post(self): 

40 """ 

41 Creates a new blog post. 

42 """ 

43 post = create_blog_post() 

44 return post, 201 

45 

46 

47@ns.route('/<int:postId>') 

48@api.response(404, 'Post not found.') 

49@api.response(201, 'Post found.') 

50class PostItem(Resource): 

51 

52 @api.marshal_with(blog_post) 

53 def get(self, postId): 

54 """ 

55 Returns a blog post with matching \"postId\". 

56 """ 

57 return get_post(postId) 

58 

59 @api.expect(blog_post) 

60 @api.response(204, 'Post successfully updated.') 

61 def put(self, postId): 

62 """ 

63 Updates a blog post with matching \"postId\" and \"checksum\". 

64 """ 

65 update_post(postId) 

66 

67 @api.response(204, 'Post successfully deleted.') 

68 def delete(self, postId): 

69 """ 

70 Deletes a blog post with matching \"postId\". 

71 """ 

72 

73 delete_post(postId) 

74 

75 

76ns = api.namespace('/', description='Operations related to blog categories') 

77@ns.route('/doc') 

78class Doc(Resource): 

79 

80 def get(self): 

81 """ 

82 Returns list of blog categories. 

83 """ 

84 return api.__schema__