views.py 17 KB
Newer Older
Greg Price committed
1 2 3
"""
Discussion API views
"""
4
from django.core.exceptions import ValidationError
5
from opaque_keys.edx.keys import CourseKey
6 7
from rest_framework.exceptions import UnsupportedMediaType
from rest_framework.parsers import JSONParser
Greg Price committed
8 9
from rest_framework.response import Response
from rest_framework.views import APIView
10
from rest_framework.viewsets import ViewSet
Greg Price committed
11

12 13 14
from discussion_api.api import (
    create_comment,
    create_thread,
15
    delete_comment,
16
    delete_thread,
17
    get_comment_list,
18
    get_course,
19
    get_course_topics,
20
    get_response_comments,
21
    get_thread,
22
    get_thread_list,
23
    update_comment,
24
    update_thread
25
)
26
from discussion_api.forms import CommentGetForm, CommentListGetForm, ThreadListGetForm
27
from openedx.core.lib.api.parsers import MergePatchParser
28
from openedx.core.lib.api.view_utils import DeveloperErrorViewMixin, view_auth_classes
29
from xmodule.modulestore.django import modulestore
Greg Price committed
30 31


32 33
@view_auth_classes()
class CourseView(DeveloperErrorViewMixin, APIView):
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
    """
    **Use Cases**

        Retrieve general discussion metadata for a course.

    **Example Requests**:

        GET /api/discussion/v1/courses/course-v1:ExampleX+Subject101+2015

    **Response Values**:

        * id: The identifier of the course

        * blackouts: A list of objects representing blackout periods (during
            which discussions are read-only except for privileged users). Each
            item in the list includes:

            * start: The ISO 8601 timestamp for the start of the blackout period

            * end: The ISO 8601 timestamp for the end of the blackout period

        * thread_list_url: The URL of the list of all threads in the course.

        * topics_url: The URL of the topic listing for the course.
    """
    def get(self, request, course_id):
        """Implements the GET method as described in the class docstring."""
        course_key = CourseKey.from_string(course_id)  # TODO: which class is right?
        return Response(get_course(request, course_key))


65 66
@view_auth_classes()
class CourseTopicsView(DeveloperErrorViewMixin, APIView):
Greg Price committed
67 68 69 70 71 72 73 74
    """
    **Use Cases**

        Retrieve the topic listing for a course. Only topics accessible to the
        authenticated user are included.

    **Example Requests**:

75
        GET /api/discussion/v1/course_topics/course-v1:ExampleX+Subject101+2015
76
            ?topic_id={topic_id_1, topid_id_2}
Greg Price committed
77 78 79

    **Response Values**:
        * courseware_topics: The list of topic trees for courseware-linked
80
            topics. Each item in the list includes:
Greg Price committed
81 82 83 84 85 86 87 88 89 90 91 92

            * id: The id of the discussion topic (null for a topic that only
              has children but cannot contain threads itself).

            * name: The display name of the topic.

            * children: A list of child subtrees of the same format.

        * non_courseware_topics: The list of topic trees that are not linked to
              courseware. Items are of the same format as in courseware_topics.
    """
    def get(self, request, course_id):
93 94 95
        """
        Implements the GET method as described in the class docstring.
        """
96
        course_key = CourseKey.from_string(course_id)
97
        topic_ids = self.request.GET.get('topic_id')
98
        with modulestore().bulk_operations(course_key):
99 100 101 102 103
            response = get_course_topics(
                request,
                course_key,
                set(topic_ids.strip(',').split(',')) if topic_ids else None,
            )
104
        return Response(response)
105 106


107 108
@view_auth_classes()
class ThreadViewSet(DeveloperErrorViewMixin, ViewSet):
109 110 111
    """
    **Use Cases**

112 113
        Retrieve the list of threads for a course, retrieve thread details,
        post a new thread, or modify or delete an existing thread.
114 115 116 117 118

    **Example Requests**:

        GET /api/discussion/v1/threads/?course_id=ExampleX/Demo/2015

119
        GET /api/discussion/v1/threads/{thread_id}
120

121 122 123 124 125 126
        POST /api/discussion/v1/threads
        {
          "course_id": "foo/bar/baz",
          "topic_id": "quux",
          "type": "discussion",
          "title": "Title text",
127
          "raw_body": "Body text"
128 129
        }

130 131
        PATCH /api/discussion/v1/threads/thread_id
        {"raw_body": "Edited text"}
132
        Content Type: "application/merge-patch+json"
133

134 135
        DELETE /api/discussion/v1/threads/thread_id

136
    **GET Thread List Parameters**:
137 138 139 140 141 142 143

        * course_id (required): The course to retrieve threads for

        * page: The (1-indexed) page to retrieve (default is 1)

        * page_size: The number of items per page (default is 10, max is 100)

144 145 146 147
        * topic_id: The id of the topic to retrieve the threads. There can be
            multiple topic_id queries to retrieve threads from multiple topics
            at once.

148 149 150 151
        * text_search: A search string to match. Any thread whose content
            (including the bodies of comments in the thread) matches the search
            string will be returned.

152 153 154 155
        * order_by: Must be "last_activity_at", "comment_count", or
            "vote_count". The key to sort the threads by. The default is
            "last_activity_at".

156 157 158
        * order_direction: Must be "desc". The direction in which to sort the
            threads by. The default and only value is "desc". This will be
            removed in a future major version.
159

160 161 162
        * following: If true, retrieve only threads the requesting user is
            following

163 164 165
        * view: "unread" for threads the requesting user has not read, or
            "unanswered" for question threads with no marked answer. Only one
            can be selected.
166

167 168
        * requested_fields: (list) Indicates which additional fields to return
          for each thread. (supports 'profile_image')
169

170 171 172
        The topic_id, text_search, and following parameters are mutually
        exclusive (i.e. only one may be specified in a request)

173 174 175 176 177 178 179
    **GET Thread Parameters**:

        * thread_id (required): The id of the thread

        * requested_fields (optional parameter): (list) Indicates which additional
         fields to return for each thread. (supports 'profile_image')

180
    **POST Parameters**:
181

182
        * course_id (required): The course to create the thread in
183

184
        * topic_id (required): The topic to create the thread in
185

186
        * type (required): The thread's type (either "question" or "discussion")
187

188
        * title (required): The thread's title
189

190
        * raw_body (required): The thread's raw body text
191

192 193 194
        * following (optional): A boolean indicating whether the user should
            follow the thread upon its creation; defaults to false

195 196
    **PATCH Parameters**:

197 198 199 200 201 202 203
        * abuse_flagged (optional): A boolean to mark thread as abusive

        * voted (optional): A boolean to vote for thread

        * read (optional): A boolean to mark thread as read

        * topic_id, type, title, and raw_body are accepted with the same meaning
204 205
        as in a POST request

206 207 208
        If "application/merge-patch+json" is not the specified content type,
        a 415 error is returned.

209
    **GET Thread List Response Values**:
210

211
        * results: The list of threads; each item in the list has the same
212
            fields as the POST/PATCH response below
213

214
        * next: The URL of the next page (or null if first page)
215

216
        * previous: The URL of the previous page (or null if last page)
217

218 219 220 221
        * text_search_rewrite: The search string to which the text_search
            parameter was rewritten in order to match threads (e.g. for spelling
            correction)

222 223 224 225
    **GET Thread Details Response Values**:

        Same response fields as the POST/PATCH response below

226
    **POST/PATCH response values**:
227

228
        * id: The id of the thread
229

230
        * course_id: The id of the thread's course
231

232
        * topic_id: The id of the thread's topic
233

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
        * created_at: The ISO 8601 timestamp for the creation of the thread

        * updated_at: The ISO 8601 timestamp for the last modification of
            the thread, which may not have been an update of the title/body

        * type: The thread's type (either "question" or "discussion")

        * title: The thread's title

        * raw_body: The thread's raw body text without any rendering applied

        * pinned: Boolean indicating whether the thread has been pinned

        * closed: Boolean indicating whether the thread has been closed

        * comment_count: The number of comments within the thread

        * unread_comment_count: The number of comments within the thread
            that were created or updated since the last time the user read
            the thread
254

255 256 257
        * editable_fields: The fields that the requesting user is allowed to
            modify with a PATCH request

258 259 260 261
        * read: Boolean indicating whether the user has read this thread

        * has_endorsed: Boolean indicating whether this thread has been answered

262 263
        * response_count: The number of direct responses for a thread

264 265 266 267
    **DELETE response values:

        No content is returned for a DELETE request

268
    """
269
    lookup_field = "thread_id"
270
    parser_classes = (JSONParser, MergePatchParser,)
271

272 273 274 275 276 277 278 279
    def list(self, request):
        """
        Implements the GET method for the list endpoint as described in the
        class docstring.
        """
        form = ThreadListGetForm(request.GET)
        if not form.is_valid():
            raise ValidationError(form.errors)
280 281 282 283 284 285 286 287 288 289 290
        return get_thread_list(
            request,
            form.cleaned_data["course_id"],
            form.cleaned_data["page"],
            form.cleaned_data["page_size"],
            form.cleaned_data["topic_id"],
            form.cleaned_data["text_search"],
            form.cleaned_data["following"],
            form.cleaned_data["view"],
            form.cleaned_data["order_by"],
            form.cleaned_data["order_direction"],
291
            form.cleaned_data["requested_fields"]
292
        )
293

294 295 296 297
    def retrieve(self, request, thread_id=None):
        """
        Implements the GET method for thread ID
        """
298 299
        requested_fields = request.GET.get('requested_fields')
        return Response(get_thread(request, thread_id, requested_fields))
300

301 302 303 304 305
    def create(self, request):
        """
        Implements the POST method for the list endpoint as described in the
        class docstring.
        """
306
        return Response(create_thread(request, request.data))
307

308 309 310 311 312
    def partial_update(self, request, thread_id):
        """
        Implements the PATCH method for the instance endpoint as described in
        the class docstring.
        """
313 314
        if request.content_type != MergePatchParser.media_type:
            raise UnsupportedMediaType(request.content_type)
315
        return Response(update_thread(request, thread_id, request.data))
316

317 318 319 320 321 322 323 324
    def destroy(self, request, thread_id):
        """
        Implements the DELETE method for the instance endpoint as described in
        the class docstring
        """
        delete_thread(request, thread_id)
        return Response(status=204)

325

326 327
@view_auth_classes()
class CommentViewSet(DeveloperErrorViewMixin, ViewSet):
328 329 330
    """
    **Use Cases**

331 332
        Retrieve the list of comments in a thread, retrieve the list of
        child comments for a response comment, create a comment, or modify
333
        or delete an existing comment.
334 335 336 337 338

    **Example Requests**:

        GET /api/discussion/v1/comments/?thread_id=0123456789abcdef01234567

339 340
        GET /api/discussion/v1/comments/2123456789abcdef01234555

341 342 343 344 345 346
        POST /api/discussion/v1/comments/
        {
            "thread_id": "0123456789abcdef01234567",
            "raw_body": "Body text"
        }

347 348
        PATCH /api/discussion/v1/comments/comment_id
        {"raw_body": "Edited text"}
349
        Content Type: "application/merge-patch+json"
350

351 352
        DELETE /api/discussion/v1/comments/comment_id

353
    **GET Comment List Parameters**:
354 355 356 357 358 359 360 361 362 363 364

        * thread_id (required): The thread to retrieve comments for

        * endorsed: If specified, only retrieve the endorsed or non-endorsed
          comments accordingly. Required for a question thread, must be absent
          for a discussion thread.

        * page: The (1-indexed) page to retrieve (default is 1)

        * page_size: The number of items per page (default is 10, max is 100)

365 366 367
        * requested_fields: (list) Indicates which additional fields to return
          for each thread. (supports 'profile_image')

368 369 370 371 372 373 374 375
    **GET Child Comment List Parameters**:

        * comment_id (required): The comment to retrieve child comments for

        * page: The (1-indexed) page to retrieve (default is 1)

        * page_size: The number of items per page (default is 10, max is 100)

376 377 378
        * requested_fields: (list) Indicates which additional fields to return
          for each thread. (supports 'profile_image')

379

380
    **POST Parameters**:
381

382
        * thread_id (required): The thread to post the comment in
383

384 385
        * parent_id: The parent comment of the new comment. Can be null or
          omitted for a comment that should be directly under the thread
386

387
        * raw_body: The comment's raw body text
388

389 390 391 392
    **PATCH Parameters**:

        raw_body is accepted with the same meaning as in a POST request

393 394 395
        If "application/merge-patch+json" is not the specified content type,
        a 415 error is returned.

396
    **GET Response Values**:
397

398 399
        * results: The list of comments; each item in the list has the same
            fields as the POST response below
400

401
        * next: The URL of the next page (or null if first page)
402

403
        * previous: The URL of the previous page (or null if last page)
404

405
    **POST/PATCH Response Values**:
406

407
        * id: The id of the comment
408

409
        * thread_id: The id of the comment's thread
410

411
        * parent_id: The id of the comment's parent
412

413 414
        * author: The username of the comment's author, or None if the
          comment is anonymous
415

416
        * author_label: A label indicating whether the author has a special
417 418
          role in the course, either "Staff" for moderators and
          administrators or "Community TA" for community TAs
419

420
        * created_at: The ISO 8601 timestamp for the creation of the comment
421

422 423
        * updated_at: The ISO 8601 timestamp for the last modification of
            the comment, which may not have been an update of the body
424

425
        * raw_body: The comment's raw body text without any rendering applied
426

427 428 429
        * endorsed: Boolean indicating whether the comment has been endorsed
            (by a privileged user or, for a question thread, the thread
            author)
430

431
        * endorsed_by: The username of the endorsing user, if available
432

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
        * endorsed_by_label: A label indicating whether the endorsing user
            has a special role in the course (see author_label)

        * endorsed_at: The ISO 8601 timestamp for the endorsement, if
            available

        * abuse_flagged: Boolean indicating whether the requesting user has
          flagged the comment for abuse

        * voted: Boolean indicating whether the requesting user has voted
          for the comment

        * vote_count: The number of votes for the comment

        * children: The list of child comments (with the same format)
448

449 450 451
        * editable_fields: The fields that the requesting user is allowed to
            modify with a PATCH request

452 453 454 455
    **DELETE Response Value**

        No content is returned for a DELETE request

456
    """
457
    lookup_field = "comment_id"
458
    parser_classes = (JSONParser, MergePatchParser,)
459

460 461 462 463 464 465 466 467
    def list(self, request):
        """
        Implements the GET method for the list endpoint as described in the
        class docstring.
        """
        form = CommentListGetForm(request.GET)
        if not form.is_valid():
            raise ValidationError(form.errors)
468 469 470 471 472
        return get_comment_list(
            request,
            form.cleaned_data["thread_id"],
            form.cleaned_data["endorsed"],
            form.cleaned_data["page"],
473 474
            form.cleaned_data["page_size"],
            form.cleaned_data["requested_fields"],
475
        )
476

477 478 479 480
    def retrieve(self, request, comment_id=None):
        """
        Implements the GET method for comments against response ID
        """
481
        form = CommentGetForm(request.GET)
482 483
        if not form.is_valid():
            raise ValidationError(form.errors)
484 485 486 487
        return get_response_comments(
            request,
            comment_id,
            form.cleaned_data["page"],
488 489
            form.cleaned_data["page_size"],
            form.cleaned_data["requested_fields"],
490 491
        )

492 493 494 495 496
    def create(self, request):
        """
        Implements the POST method for the list endpoint as described in the
        class docstring.
        """
497
        return Response(create_comment(request, request.data))
498

499 500 501 502 503 504 505 506
    def destroy(self, request, comment_id):
        """
        Implements the DELETE method for the instance endpoint as described in
        the class docstring
        """
        delete_comment(request, comment_id)
        return Response(status=204)

507 508 509 510 511
    def partial_update(self, request, comment_id):
        """
        Implements the PATCH method for the instance endpoint as described in
        the class docstring.
        """
512 513
        if request.content_type != MergePatchParser.media_type:
            raise UnsupportedMediaType(request.content_type)
514
        return Response(update_comment(request, comment_id, request.data))