-
Notifications
You must be signed in to change notification settings - Fork 24
/
groupcomment_examiner.py
133 lines (117 loc) · 5.17 KB
/
groupcomment_examiner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from django.utils.translation import ugettext_lazy
from rest_framework.exceptions import PermissionDenied, ValidationError, NotFound
from rest_framework.generics import mixins
from rest_framework.response import Response
from rest_framework import status
from devilry.apps.core.models import AssignmentGroup
from devilry.devilry_api.group_comment.serializers.serializer_examiner import GroupCommentSerializerExaminer
from devilry.devilry_api.group_comment.views.groupcomment_base import BaseGroupCommentView
from devilry.devilry_api.models import APIKey
from devilry.devilry_api.permission.examiner_permission import ExaminerPermissionAPIKey
from devilry.devilry_group.models import GroupComment
class GroupCommentViewExaminer(mixins.CreateModelMixin,
mixins.DestroyModelMixin,
BaseGroupCommentView):
permission_classes = (ExaminerPermissionAPIKey, )
api_key_permissions = (APIKey.EXAMINER_PERMISSION_READ, APIKey.EXAMINER_PERMISSION_WRITE)
serializer_class = GroupCommentSerializerExaminer
def get_object(self):
"""
This is only used to get a drafted comment
Returns:
:obj:`devilry_group.GroupComment`
"""
if 'feedback_set' not in self.kwargs:
raise ValidationError(ugettext_lazy('Url path parameter feedback_set required'))
id = self.request.query_params.get('id', None)
if not id:
raise ValidationError(ugettext_lazy('Queryparam id required.'))
try:
comment = self.get_role_query_set().get(feedback_set__id=self.kwargs['feedback_set'], id=id)
except GroupComment.DoesNotExist:
raise NotFound
if comment.feedback_set.grading_published_datetime is not None:
raise PermissionDenied(ugettext_lazy('Cannot delete published comment.'))
if comment.visibility != GroupComment.VISIBILITY_PRIVATE:
raise PermissionDenied(ugettext_lazy('Cannot delete a comment that is not private.'))
if not comment.part_of_grading:
raise PermissionDenied(ugettext_lazy('Cannot delete a comment that is not a draft.'))
return comment
def get_role_query_set(self):
"""
Returns role queryset for examiner role
Returns:
:class:`~devilry_group.GroupComment` queryset
"""
assignment_group_queryset = AssignmentGroup.objects.filter_examiner_has_access(user=self.request.user)
return GroupComment.objects.filter(feedback_set__group__in=assignment_group_queryset,
comment_type=GroupComment.COMMENT_TYPE_GROUPCOMMENT) \
.select_related('feedback_set__group__parentnode', 'user')\
.exclude_private_comments_from_other_users(user=self.request.user)
def get(self, request, feedback_set, *args, **kwargs):
return super(GroupCommentViewExaminer, self).get(request, feedback_set, *args, **kwargs)
get.__doc__ = BaseGroupCommentView.get.__doc__
def create(self, feedback_set, request, *args, **kwargs):
"""
Creates a feedbackset
Args:
feedback_set: :attr:`~devilry_grup.FeedbackSet.id`
request: request object
Returns:
Returns http 201 response or exception is raised
"""
data = dict(request.data)
data['feedback_set'] = feedback_set
data['user_role'] = GroupComment.USER_ROLE_EXAMINER
serializer = self.get_serializer(data=data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
def post(self, request, feedback_set, *args, **kwargs):
"""
post a comment to a feedbackset
---
parameters:
- name: feedback_set
required: true
paramType: path
type: Int
description: feedbackset id
- name: text
paramType: form
required: true
type: String
description: comment text
- name: part_of_grading
required: false
paramType: form
type: Boolean
description: part of grading
- name: visibility
required: false
paramType: form
enum:
- visible-to-everyone
- visible-to-examiner-and-admins
- private
description: comment visibility
"""
return self.create(feedback_set, request, *args, **kwargs)
def delete(self, request, *args, **kwargs):
"""
destroy drafted comment
---
parameters:
- name: feedback_set
required: true
paramType: path
type: Int
description: feedbackset id
- name: id
required: true
paramType: query
type: Int
description: comment id to destroy
"""
return super(GroupCommentViewExaminer, self).destroy(request, *args, **kwargs)