|
1 | 1 | from rest_framework import serializers as ser |
2 | 2 | from framework.auth.core import Auth |
| 3 | +from framework.exceptions import PermissionsError |
3 | 4 | from website.project.model import Comment, Node |
4 | 5 | from rest_framework.exceptions import ValidationError, PermissionDenied |
5 | 6 | from api.base.exceptions import InvalidModelValueError, Conflict |
@@ -78,11 +79,20 @@ def update(self, comment, validated_data): |
78 | 79 | if validated_data: |
79 | 80 | if 'get_content' in validated_data: |
80 | 81 | content = validated_data.pop('get_content') |
81 | | - comment.edit(content, auth=auth, save=True) |
| 82 | + try: |
| 83 | + comment.edit(content, auth=auth, save=True) |
| 84 | + except PermissionsError: |
| 85 | + raise PermissionDenied('Not authorized to edit this comment.') |
82 | 86 | if validated_data.get('is_deleted', None) is True: |
83 | | - comment.delete(auth, save=True) |
| 87 | + try: |
| 88 | + comment.delete(auth, save=True) |
| 89 | + except PermissionsError: |
| 90 | + raise PermissionDenied('Not authorized to delete this comment.') |
84 | 91 | elif comment.is_deleted: |
85 | | - comment.undelete(auth, save=True) |
| 92 | + try: |
| 93 | + comment.undelete(auth, save=True) |
| 94 | + except PermissionsError: |
| 95 | + raise PermissionDenied('Not authorized to undelete this comment.') |
86 | 96 | return comment |
87 | 97 |
|
88 | 98 | def get_target_type(self, obj): |
@@ -136,10 +146,11 @@ def create(self, validated_data): |
136 | 146 | detail='Invalid comment target \'{}\'.'.format(target_id) |
137 | 147 | ) |
138 | 148 | validated_data['target'] = target |
139 | | - if node and node.can_comment(auth): |
| 149 | + validated_data['content'] = validated_data.pop('get_content') |
| 150 | + try: |
140 | 151 | comment = Comment.create(auth=auth, **validated_data) |
141 | | - else: |
142 | | - raise PermissionDenied("Not authorized to comment on this project.") |
| 152 | + except PermissionsError: |
| 153 | + raise PermissionDenied('Not authorized to comment on this project.') |
143 | 154 | return comment |
144 | 155 |
|
145 | 156 |
|
|
0 commit comments