Coverage for sites/ptf_tools/comments_moderation/rights.py: 95%

43 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-05-19 19:20 +0000

1from django.contrib.auth.models import AbstractUser 

2 

3from comments_api.constants import PARAM_ADMIN 

4from comments_api.constants import PARAM_BOOLEAN_VALUE 

5from comments_api.constants import PARAM_COLLECTION 

6from comments_api.constants import PARAM_MODERATOR 

7from comments_views.core.rights import AbstractUserRights 

8from ptf_tools.templatetags.tools_helpers import get_authorized_collections 

9 

10 

11class ModeratorUserRights(AbstractUserRights): 

12 COMMENT_POST_URL = "" 

13 # Cache of the user's authorized collections. 

14 _admin_collections = None 

15 _staff_collections = None 

16 

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

18 if self._admin_collections is None: 

19 self._admin_collections = get_authorized_collections(self.user) 

20 return self._admin_collections 

21 

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

23 if self._staff_collections is None: 

24 self._staff_collections = [] 

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

26 self._staff_collections = list( 

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

28 ) 

29 return self._staff_collections 

30 

31 def comment_rights_query_params(self) -> dict: 

32 query_params = {} 

33 if isinstance(self.user, AbstractUser): 33 ↛ 45line 33 didn't jump to line 45, because the condition on line 33 was never false

34 query_params[PARAM_MODERATOR] = self.user.pk 

35 

36 # NOT USED 

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

38 query_params[PARAM_ADMIN] = PARAM_BOOLEAN_VALUE 

39 

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

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

42 if collections: 

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

44 

45 return query_params 

46 

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

48 """ 

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

50 """ 

51 return False 

52 

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

54 """ 

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

56 Only the comment's author can. 

57 """ 

58 return False 

59 

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

61 """ 

62 The user can moderate either if: 

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

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

65 """ 

66 return ( 

67 self.user.is_superuser 

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

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

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

71 ) 

72 

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

74 """ 

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

76 """ 

77 return ( 

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

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

80 or self.user.is_superuser 

81 ) 

82 

83 def is_admin_moderator(self) -> bool: 

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

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

86 

87 def is_staff_moderator(self) -> bool: 

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

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