Coverage for src/comments_moderation/rights.py: 94%

40 statements  

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

1from comments_api.constants import ( 

2 PARAM_ADMIN, 

3 PARAM_BOOLEAN_VALUE, 

4 PARAM_COLLECTION, 

5 PARAM_MODERATOR, 

6) 

7from comments_views.core.rights import AbstractUserRights 

8from django.contrib.auth.models import AbstractUser 

9 

10from ptf_tools.templatetags.tools_helpers import get_authorized_collections 

11 

12 

13class ModeratorUserRights(AbstractUserRights): 

14 COMMENT_POST_URL = "" 

15 # Cache of the user's authorized collections. 

16 _admin_collections = None 

17 _staff_collections = None 

18 

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

20 if self._admin_collections is None: 

21 self._admin_collections = get_authorized_collections(self.user) 

22 return self._admin_collections 

23 

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

25 if self._staff_collections is None: 

26 self._staff_collections = [] 

27 if hasattr(self.user, "comment_moderator"): 

28 self._staff_collections = list( 

29 self.user.comment_moderator.collections.all().values_list("pid", flat=True) 

30 ) 

31 return self._staff_collections 

32 

33 def comment_rights_query_params(self) -> dict: 

34 query_params = {} 

35 if isinstance(self.user, AbstractUser): 35 ↛ 47line 35 didn't jump to line 47 because the condition on line 35 was always true

36 query_params[PARAM_MODERATOR] = self.user.pk 

37 

38 # NOT USED 

39 if self.user.is_superuser: 39 ↛ 40line 39 didn't jump to line 40 because the condition on line 39 was never true

40 query_params[PARAM_ADMIN] = PARAM_BOOLEAN_VALUE 

41 

42 # Add other query parameters according to the current user rights 

43 collections = self.get_user_admin_collections() + self.get_user_staff_collections() 

44 if collections: 

45 query_params[PARAM_COLLECTION] = ",".join(sorted(set(collections))) 

46 

47 return query_params 

48 

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

50 """ 

51 Moderators should not have the right to delete a comment. 

52 """ 

53 return False 

54 

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

56 """ 

57 Moderators do not have the right to edit a comment. 

58 Only the comment's author can. 

59 """ 

60 return False 

61 

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

63 """ 

64 The user can moderate either if: 

65 - he/she has rights over the comment's collection (admin or staff moderator). 

66 - he/she has been selected as a moderator of the comment 

67 """ 

68 return ( 

69 self.user.is_superuser 

70 or comment["site_name"].upper() in self.get_user_admin_collections() 

71 or comment["site_name"].upper() in self.get_user_staff_collections() 

72 or self.user.pk in comment["moderators"] 

73 ) 

74 

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

76 """ 

77 The user can manage the moderators if the comment is in the user's collections. 

78 """ 

79 return ( 

80 comment["site_name"].upper() in self.get_user_admin_collections() 

81 or comment["site_name"].upper() in self.get_user_staff_collections() 

82 or self.user.is_superuser 

83 ) 

84 

85 def is_admin_moderator(self) -> bool: 

86 """An user is an admin moderator if he/she has 1+ collectiongroup.""" 

87 return len(self.get_user_admin_collections()) > 0 

88 

89 def is_staff_moderator(self) -> bool: 

90 """An user is a staff moderator if he/she has 1+ commentmoderator collection.""" 

91 return len(self.get_user_staff_collections()) > 0