Coverage for mongo/post.py: 100%

76 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2024-11-05 04:22 +0000

1from mongo import engine 

2from mongo.course import * 

3from datetime import datetime 

4from .user import * 

5from .utils import * 

6 

7__all__ = ['Post'] 

8 

9 

10class Post(): 

11 

12 @classmethod 

13 def found_thread(cls, target_thread): 

14 reply_thread = [] 

15 if target_thread.reply: 

16 for reply in target_thread.reply: 

17 reply_thread.append(Post.found_thread(reply)) 

18 thread = { 

19 'id': str(target_thread.id), 

20 'content': target_thread.markdown, 

21 'author': target_thread.author.info, 

22 'status': target_thread.status, 

23 'created': target_thread.created.timestamp(), 

24 'updated': target_thread.updated.timestamp(), 

25 'reply': reply_thread 

26 } 

27 return thread 

28 

29 @classmethod 

30 def found_post(cls, course_obj, target_id=None): 

31 data = [] 

32 for x in course_obj.posts: # target_threads 

33 if (target_id is not None and str(x.thread.id) != target_id): 

34 continue 

35 post = { 

36 'thread': Post.found_thread(x.thread), 

37 'title': x.post_name, 

38 } 

39 data.append(post) 

40 return data 

41 

42 @classmethod 

43 def add_post(cls, course, user, content, title): 

44 course_obj = Course(course).obj 

45 created_time = datetime.now() 

46 created_time.timestamp() 

47 updated_time = created_time 

48 new_thread = engine.PostThread(markdown=content, 

49 course_id=course_obj, 

50 author=user.obj, 

51 created=created_time, 

52 updated=updated_time) 

53 new_thread.save() 

54 new_post = engine.Post(post_name=title, thread=new_thread) 

55 new_post.save() 

56 course_obj.posts.append(new_post) 

57 course_obj.save() 

58 

59 @classmethod 

60 def add_reply(cls, target_thread, user, content): 

61 created_time = datetime.now() 

62 created_time.timestamp() 

63 updated_time = created_time 

64 new_depth = target_thread.depth + 1 

65 ''' not open this feature ,reply to reply''' 

66 if new_depth > 2: 

67 return 'Forbidden,you can not reply too deap (not open).' 

68 origin_course = target_thread.course_id 

69 new_thread = engine.PostThread(markdown=content, 

70 course_id=origin_course, 

71 depth=new_depth, 

72 created=created_time, 

73 updated=updated_time, 

74 author=user.obj) 

75 new_thread.save() 

76 target_thread.reply.append(new_thread) 

77 target_thread.save() 

78 

79 @classmethod 

80 def edit_post(cls, 

81 target_thread, 

82 user, 

83 content, 

84 title, 

85 capability, 

86 delete=0): 

87 # permission 

88 author = target_thread.author 

89 # Permission check (use by edit or delete) 

90 if delete == 1: # delete 

91 # teacher, ta, author can delete 

92 if user != author and not (capability & Course.Permission.GRADE): 

93 return 'Forbidden, you don\'t have enough permission to delete it.' 

94 target_thread.status = 1 

95 else: # edit 

96 # only author or admin can edit 

97 if user != author and not (capability & Course.Permission.MODIFY): 

98 return 'Forbidden, you don\'t have enough permission to edit it.' 

99 # update thread 

100 updated_time = datetime.now() 

101 updated_time.timestamp() 

102 target_thread.updated = updated_time 

103 target_thread.markdown = content 

104 target_thread.save() 

105 # check it is post, true to update 

106 try: 

107 target_post = engine.Post.objects.get(thread=target_thread) 

108 except engine.DoesNotExist: 

109 target_post = None 

110 if target_post is not None: # edit post 

111 target_post.post_name = title 

112 target_post.save() 

113 

114 @classmethod 

115 def delete_post(cls, target_thread, user, capability): 

116 content = '*Content was deleted.*' 

117 title = '*The Post was deleted*' 

118 return Post.edit_post(target_thread, user, content, title, capability, 

119 1)