Coverage for apps/comments_views/journal/rights.py: 100%
26 statements
« prev ^ index » next coverage.py v7.3.2, created at 2024-11-04 17:46 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2024-11-04 17:46 +0000
1from comments_api.constants import PARAM_USER
2from comments_api.constants import STATUS_CAN_DELETE
3from comments_api.constants import STATUS_CAN_EDIT
4from comments_api.constants import STATUS_MODERATED
6from ..core.rights import AbstractUserRights
7from .app_settings import app_settings
8from .models import OIDCUser
11class OIDCUserRights(AbstractUserRights):
12 """
13 Comment rights for a comment's author. \\
14 A comment's author can only perform actions related to his/her comments.
15 """
17 COMMENT_POST_URL = "submit_comment"
19 def get_user_admin_collections(self) -> list[str]:
20 return []
22 def get_user_staff_collections(self) -> list[str]:
23 return []
25 def comment_rights_query_params(self) -> dict:
26 """
27 The available comments are limited to the user's comment for a regular user.
28 """
29 query_params = {}
30 if isinstance(self.user, OIDCUser):
31 query_params[PARAM_USER] = self.user.get_user_id()
32 return query_params
34 def comment_can_delete(self, comment: dict) -> bool:
35 """
36 A comment's author can delete his/her comment only if the comment
37 has not been moderated yet. \\
38 The behavior can be adapted with `DELETE_BEFORE_MODERATION` and
39 `DELETE_AFTER_MODERATION` settings.
40 DELETE_AFTER_MODERATION UPDATE: Setting disabled on comment server side.
41 """
42 return (
43 isinstance(self.user, OIDCUser)
44 and self.user.get_user_id() == comment.get("author_id")
45 and (
46 (
47 comment.get("status") in STATUS_CAN_DELETE
48 and app_settings.DELETE_BEFORE_MODERATION
49 )
50 or (
51 comment.get("status") in STATUS_MODERATED
52 and app_settings.DELETE_AFTER_MODERATION
53 )
54 )
55 )
57 def comment_can_edit(self, comment: dict) -> bool:
58 """
59 A comment's author can edit his/her comment only if
60 - it has not been validated yet.
61 AND - no moderator have been assigned to the comment
62 or `EDIT_BEFORE_MODERATION` is True
63 """
64 return (
65 isinstance(self.user, OIDCUser)
66 and self.user.get_user_id() == comment.get("author_id")
67 and comment.get("status") in STATUS_CAN_EDIT
68 and (
69 app_settings.EDIT_BEFORE_MODERATION
70 or (
71 not isinstance(comment.get("moderators"), list)
72 or len(comment["moderators"]) == 0
73 )
74 )
75 )
77 def comment_can_moderate(self, comment: dict) -> bool:
78 """
79 Base users can't moderate comments.
80 """
81 return False
83 def comment_can_manage_moderators(self, comment) -> bool:
84 """Base users can't manage moderators"""
85 return False