Coverage for src/comments_views/journal/rights.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.7.0, created at 2025-04-09 14:49 +0000

1from comments_api.constants import PARAM_USER, STATUS_CAN_DELETE, STATUS_CAN_EDIT, STATUS_MODERATED 

2 

3from ..core.rights import AbstractUserRights 

4from .app_settings import app_settings 

5from .models import OIDCUser 

6 

7 

8class OIDCUserRights(AbstractUserRights): 

9 """ 

10 Comment rights for a comment's author. \\ 

11 A comment's author can only perform actions related to his/her comments. 

12 """ 

13 

14 COMMENT_POST_URL = "submit_comment" 

15 

16 def get_user_admin_collections(self) -> list[str]: 

17 return [] 

18 

19 def get_user_staff_collections(self) -> list[str]: 

20 return [] 

21 

22 def comment_rights_query_params(self) -> dict: 

23 """ 

24 The available comments are limited to the user's comment for a regular user. 

25 """ 

26 query_params = {} 

27 if isinstance(self.user, OIDCUser): 

28 query_params[PARAM_USER] = self.user.get_user_id() 

29 return query_params 

30 

31 def comment_can_delete(self, comment: dict) -> bool: 

32 """ 

33 A comment's author can delete his/her comment only if the comment 

34 has not been moderated yet. \\ 

35 The behavior can be adapted with `DELETE_BEFORE_MODERATION` and 

36 `DELETE_AFTER_MODERATION` settings. 

37 DELETE_AFTER_MODERATION UPDATE: Setting disabled on comment server side. 

38 """ 

39 return ( 

40 isinstance(self.user, OIDCUser) 

41 and self.user.get_user_id() == comment.get("author_id") 

42 and ( 

43 ( 

44 comment.get("status") in STATUS_CAN_DELETE 

45 and app_settings.DELETE_BEFORE_MODERATION 

46 ) 

47 or ( 

48 comment.get("status") in STATUS_MODERATED 

49 and app_settings.DELETE_AFTER_MODERATION 

50 ) 

51 ) 

52 ) 

53 

54 def comment_can_edit(self, comment: dict) -> bool: 

55 """ 

56 A comment's author can edit his/her comment only if 

57 - it has not been validated yet. 

58 AND - no moderator have been assigned to the comment 

59 or `EDIT_BEFORE_MODERATION` is True 

60 """ 

61 return ( 

62 isinstance(self.user, OIDCUser) 

63 and self.user.get_user_id() == comment.get("author_id") 

64 and comment.get("status") in STATUS_CAN_EDIT 

65 and ( 

66 app_settings.EDIT_BEFORE_MODERATION 

67 or ( 

68 not isinstance(comment.get("moderators"), list) 

69 or len(comment["moderators"]) == 0 

70 ) 

71 ) 

72 ) 

73 

74 def comment_can_moderate(self, comment: dict) -> bool: 

75 """ 

76 Base users can't moderate comments. 

77 """ 

78 return False 

79 

80 def comment_can_manage_moderators(self, comment) -> bool: 

81 """Base users can't manage moderators""" 

82 return False