views.py 22 KB
Newer Older
1 2 3 4 5
"""
Views handling read (GET) requests for the Discussion tab and inline discussions.
"""

from functools import wraps
Arjun Singh committed
6
import json
7
import logging
8
import xml.sax.saxutils as saxutils
Arjun Singh committed
9

10
from django.contrib.auth.decorators import login_required
11
from django.conf import settings
12
from django.core.context_processors import csrf
13
from django.core.urlresolvers import reverse
14
from django.contrib.auth.models import User
15
from django.http import Http404, HttpResponseBadRequest
16
from django.utils.translation import ugettext_noop
17
from django.views.decorators.http import require_GET
18
import newrelic.agent
19

David Baumgold committed
20
from edxmako.shortcuts import render_to_response
21
from courseware.courses import get_course_with_access
22 23 24 25
from openedx.core.djangoapps.course_groups.cohorts import (
    is_course_cohorted,
    get_cohort_id,
    get_course_cohorts,
26 27
    is_commentable_cohorted,
    get_cohorted_commentables)
28
from courseware.tabs import EnrolledTab
29
from courseware.access import has_access
30
from xmodule.modulestore.django import modulestore
31
from ccx.overrides import get_current_ccx
32

33
from django_comment_client.permissions import has_permission
34 35 36 37 38 39 40
from django_comment_client.utils import (
    merge_dict,
    extract,
    strip_none,
    add_courseware_context,
    get_group_id_for_comments_service
)
Rocky Duan committed
41
import django_comment_client.utils as utils
42
import lms.lib.comment_client as cc
Rocky Duan committed
43

44
from opaque_keys.edx.keys import CourseKey
45

46
THREADS_PER_PAGE = 20
47
INLINE_THREADS_PER_PAGE = 20
Rocky Duan committed
48
PAGES_NEARBY_DELTA = 2
Arjun Singh committed
49
log = logging.getLogger("edx.discussions")
50

Calen Pennington committed
51

52
class DiscussionTab(EnrolledTab):
53 54 55 56
    """
    A tab for the cs_comments_service forums.
    """

57
    type = 'discussion'
58
    title = ugettext_noop('Discussion')
59 60
    priority = None
    view_name = 'django_comment_client.forum.views.forum_form_discussion'
61
    is_hideable = settings.FEATURES.get('ALLOW_HIDING_DISCUSSION_TAB', False)
62
    is_default = False
63 64 65

    @classmethod
    def is_enabled(cls, course, user=None):
66
        if not super(DiscussionTab, cls).is_enabled(course, user):
67 68 69
            return False

        if settings.FEATURES.get('CUSTOM_COURSES_EDX', False):
cewing committed
70
            if get_current_ccx(course.id):
71 72 73 74
                return False
        return settings.FEATURES.get('ENABLE_DISCUSSION_SERVICE')


75 76 77 78 79 80
def _attr_safe_json(obj):
    """
    return a JSON string for obj which is safe to embed as the value of an attribute in a DOM node
    """
    return saxutils.escape(json.dumps(obj), {'"': '"'})

81

82
@newrelic.agent.function_trace()
83
def make_course_settings(course, user):
84 85 86 87 88 89 90 91 92
    """
    Generate a JSON-serializable model for course settings, which will be used to initialize a
    DiscussionCourseSettings object on the client.
    """

    obj = {
        'is_cohorted': is_course_cohorted(course.id),
        'allow_anonymous': course.allow_anonymous,
        'allow_anonymous_to_peers': course.allow_anonymous_to_peers,
93
        'cohorts': [{"id": str(g.id), "name": g.name} for g in get_course_cohorts(course)],
94
        'category_map': utils.get_discussion_category_map(course, user)
95 96 97 98
    }

    return obj

99

100
@newrelic.agent.function_trace()
101
def get_threads(request, course, discussion_id=None, per_page=THREADS_PER_PAGE):
102
    """
103
    This may raise an appropriate subclass of cc.utils.CommentClientError
104
    if something goes wrong, or ValueError if the group_id is invalid.
105
    """
Rocky Duan committed
106 107
    default_query_params = {
        'page': 1,
108
        'per_page': per_page,
109
        'sort_key': 'date',
Rocky Duan committed
110 111
        'sort_order': 'desc',
        'text': '',
112
        'course_id': unicode(course.id),
Rocky Duan committed
113
        'user_id': request.user.id,
114
        'group_id': get_group_id_for_comments_service(request, course.id, discussion_id),  # may raise ValueError
Rocky Duan committed
115
    }
Your Name committed
116

117 118 119 120 121
    # If provided with a discussion id, filter by discussion id in the
    # comments_service.
    if discussion_id is not None:
        default_query_params['commentable_id'] = discussion_id

122 123
    if not request.GET.get('sort_key'):
        # If the user did not select a sort key, use their last used sort key
124 125
        cc_user = cc.User.from_django_user(request.user)
        cc_user.retrieve()
126
        # TODO: After the comment service is updated this can just be user.default_sort_key because the service returns the default value
127
        default_query_params['sort_key'] = cc_user.get('default_sort_key') or default_query_params['sort_key']
128 129
    else:
        # If the user clicked a sort key, update their default sort key
130 131 132
        cc_user = cc.User.from_django_user(request.user)
        cc_user.default_sort_key = request.GET.get('sort_key')
        cc_user.save()
133

Your Name committed
134 135 136
    #there are 2 dimensions to consider when executing a search with respect to group id
    #is user a moderator
    #did the user request a group
137

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    query_params = merge_dict(
        default_query_params,
        strip_none(
            extract(
                request.GET,
                [
                    'page',
                    'sort_key',
                    'sort_order',
                    'text',
                    'commentable_ids',
                    'flagged',
                    'unread',
                    'unanswered',
                ]
            )
        )
    )
Rocky Duan committed
156

157
    threads, page, num_pages, corrected_text = cc.Thread.search(query_params)
158

159 160 161 162 163 164 165 166 167
    # If not provided with a discussion id, filter threads by commentable ids
    # which are accessible to the current user.
    if discussion_id is None:
        discussion_category_ids = set(utils.get_discussion_categories_ids(course, request.user))
        threads = [
            thread for thread in threads
            if thread.get('commentable_id') in discussion_category_ids
        ]

168
    for thread in threads:
David Baumgold committed
169 170
        # patch for backward compatibility to comments service
        if 'pinned' not in thread:
171
            thread['pinned'] = False
Your Name committed
172

Rocky Duan committed
173 174
    query_params['page'] = page
    query_params['num_pages'] = num_pages
175
    query_params['corrected_text'] = corrected_text
Rocky Duan committed
176 177 178

    return threads, query_params

Calen Pennington committed
179

180 181 182 183 184 185 186 187 188 189 190 191 192 193
def use_bulk_ops(view_func):
    """
    Wraps internal request handling inside a modulestore bulk op, significantly
    reducing redundant database calls.  Also converts the course_id parsed from
    the request uri to a CourseKey before passing to the view.
    """
    @wraps(view_func)
    def wrapped_view(request, course_id, *args, **kwargs):  # pylint: disable=missing-docstring
        course_key = CourseKey.from_string(course_id)
        with modulestore().bulk_operations(course_key):
            return view_func(request, course_key, *args, **kwargs)
    return wrapped_view


194
@login_required
195 196
@use_bulk_ops
def inline_discussion(request, course_key, discussion_id):
197 198 199
    """
    Renders JSON for DiscussionModules
    """
200 201
    nr_transaction = newrelic.agent.current_transaction()

202
    course = get_course_with_access(request.user, 'load', course_key, check_if_enrolled=True)
203 204
    cc_user = cc.User.from_django_user(request.user)
    user_info = cc_user.to_dict()
205

206
    try:
207
        threads, query_params = get_threads(request, course, discussion_id, per_page=INLINE_THREADS_PER_PAGE)
208 209 210
    except ValueError:
        return HttpResponseBadRequest("Invalid group_id")

211
    with newrelic.agent.FunctionTrace(nr_transaction, "get_metadata_for_threads"):
212
        annotated_content_info = utils.get_metadata_for_threads(course_key, threads, request.user, user_info)
213
    is_staff = has_permission(request.user, 'openclose_thread', course.id)
214 215
    threads = [utils.prepare_content(thread, course_key, is_staff) for thread in threads]
    with newrelic.agent.FunctionTrace(nr_transaction, "add_courseware_context"):
216
        add_courseware_context(threads, course, request.user)
Rocky Duan committed
217
    return utils.JsonResponse({
218
        'is_commentable_cohorted': is_commentable_cohorted(course_key, discussion_id),
219
        'discussion_data': threads,
220
        'user_info': user_info,
221 222
        'annotated_content_info': annotated_content_info,
        'page': query_params['page'],
223
        'num_pages': query_params['num_pages'],
224
        'roles': utils.get_role_ids(course_key),
225
        'course_settings': make_course_settings(course, request.user)
Rocky Duan committed
226
    })
227

228

229
@login_required
230 231
@use_bulk_ops
def forum_form_discussion(request, course_key):
232
    """
233
    Renders the main Discussion page, potentially filtered by a search query
234
    """
235
    nr_transaction = newrelic.agent.current_transaction()
236

237
    course = get_course_with_access(request.user, 'load', course_key, check_if_enrolled=True)
238
    course_settings = make_course_settings(course, request.user)
Arjun Singh committed
239

240 241 242
    user = cc.User.from_django_user(request.user)
    user_info = user.to_dict()

243
    try:
244
        unsafethreads, query_params = get_threads(request, course)   # This might process a search query
245
        is_staff = has_permission(request.user, 'openclose_thread', course.id)
246
        threads = [utils.prepare_content(thread, course_key, is_staff) for thread in unsafethreads]
Sarina Canelake committed
247
    except cc.utils.CommentClientMaintenanceError:
248 249
        log.warning("Forum is in maintenance mode")
        return render_to_response('discussion/maintenance.html', {})
250 251
    except ValueError:
        return HttpResponseBadRequest("Invalid group_id")
252

253
    with newrelic.agent.FunctionTrace(nr_transaction, "get_metadata_for_threads"):
254
        annotated_content_info = utils.get_metadata_for_threads(course_key, threads, request.user, user_info)
255

256
    with newrelic.agent.FunctionTrace(nr_transaction, "add_courseware_context"):
257
        add_courseware_context(threads, course, request.user)
258

259
    if request.is_ajax():
260
        return utils.JsonResponse({
Calen Pennington committed
261
            'discussion_data': threads,   # TODO: Standardize on 'discussion_data' vs 'threads'
262
            'annotated_content_info': annotated_content_info,
263 264
            'num_pages': query_params['num_pages'],
            'page': query_params['page'],
265
            'corrected_text': query_params['corrected_text'],
266
        })
267
    else:
268
        with newrelic.agent.FunctionTrace(nr_transaction, "get_cohort_info"):
269
            user_cohort_id = get_cohort_id(request.user, course_key)
270

271 272 273
        context = {
            'csrf': csrf(request)['csrf_token'],
            'course': course,
Arjun Singh committed
274
            #'recent_active_threads': recent_active_threads,
275
            'staff_access': has_access(request.user, 'staff', course),
276
            'threads': _attr_safe_json(threads),
Matthew Mongeau committed
277
            'thread_pages': query_params['num_pages'],
278
            'user_info': _attr_safe_json(user_info),
279 280 281 282
            'flag_moderator': (
                has_permission(request.user, 'openclose_thread', course.id) or
                has_access(request.user, 'staff', course)
            ),
283
            'annotated_content_info': _attr_safe_json(annotated_content_info),
284
            'course_id': course.id.to_deprecated_string(),
285
            'roles': _attr_safe_json(utils.get_role_ids(course_key)),
286
            'is_moderator': has_permission(request.user, "see_all_cohorts", course_key),
287
            'cohorts': course_settings["cohorts"],  # still needed to render _thread_list_template
288
            'is_course_cohorted': is_course_cohorted(course_key),  # still needed to render _thread_list_template
289 290
            'user_cohort': user_cohort_id, # read from container in NewPostView
            'cohorted_commentables': (get_cohorted_commentables(course_key)),
291
            'sort_preference': user.default_sort_key,
292 293
            'category_map': course_settings["category_map"],
            'course_settings': _attr_safe_json(course_settings)
294
        }
295
        # print "start rendering.."
296
        return render_to_response('discussion/index.html', context)
297

298

299
@require_GET
300
@login_required
301 302 303 304 305
@use_bulk_ops
def single_thread(request, course_key, discussion_id, thread_id):
    """
    Renders a response to display a single discussion thread.
    """
306 307
    nr_transaction = newrelic.agent.current_transaction()

308
    course = get_course_with_access(request.user, 'load', course_key, check_if_enrolled=True)
309
    course_settings = make_course_settings(course, request.user)
310 311
    cc_user = cc.User.from_django_user(request.user)
    user_info = cc_user.to_dict()
312
    is_moderator = has_permission(request.user, "see_all_cohorts", course_key)
Rocky Duan committed
313

314 315 316 317
    # Verify that the student has access to this thread if belongs to a discussion module
    if discussion_id not in utils.get_discussion_categories_ids(course, request.user):
        raise Http404

318 319 320
    # Currently, the front end always loads responses via AJAX, even for this
    # page; it would be a nice optimization to avoid that extra round trip to
    # the comments service.
321 322 323 324 325 326 327 328 329 330 331
    try:
        thread = cc.Thread.find(thread_id).retrieve(
            recursive=request.is_ajax(),
            user_id=request.user.id,
            response_skip=request.GET.get("resp_skip"),
            response_limit=request.GET.get("resp_limit")
        )
    except cc.utils.CommentClientRequestError as e:
        if e.status_code == 404:
            raise Http404
        raise
Arjun Singh committed
332

333 334 335
    # verify that the thread belongs to the requesting student's cohort
    if is_commentable_cohorted(course_key, discussion_id) and not is_moderator:
        user_group_id = get_cohort_id(request.user, course_key)
336
        if getattr(thread, "group_id", None) is not None and user_group_id != thread.group_id:
337 338
            raise Http404

339
    is_staff = has_permission(request.user, 'openclose_thread', course.id)
Rocky Duan committed
340
    if request.is_ajax():
341
        with newrelic.agent.FunctionTrace(nr_transaction, "get_annotated_content_infos"):
342
            annotated_content_info = utils.get_annotated_content_infos(course_key, thread, request.user, user_info=user_info)
343
        content = utils.prepare_content(thread.to_dict(), course_key, is_staff)
344
        with newrelic.agent.FunctionTrace(nr_transaction, "add_courseware_context"):
345
            add_courseware_context([content], course, request.user)
346
        return utils.JsonResponse({
347
            'content': content,
Rocky Duan committed
348 349
            'annotated_content_info': annotated_content_info,
        })
350

Rocky Duan committed
351
    else:
352
        try:
353
            threads, query_params = get_threads(request, course)
354 355
        except ValueError:
            return HttpResponseBadRequest("Invalid group_id")
356
        threads.append(thread.to_dict())
Rocky Duan committed
357

358
        with newrelic.agent.FunctionTrace(nr_transaction, "add_courseware_context"):
359
            add_courseware_context(threads, course, request.user)
360 361

        for thread in threads:
David Baumgold committed
362 363
            # patch for backward compatibility with comments service
            if "pinned" not in thread:
364
                thread["pinned"] = False
365

366
        threads = [utils.prepare_content(thread, course_key, is_staff) for thread in threads]
367

368
        with newrelic.agent.FunctionTrace(nr_transaction, "get_metadata_for_threads"):
369
            annotated_content_info = utils.get_metadata_for_threads(course_key, threads, request.user, user_info)
370

371
        with newrelic.agent.FunctionTrace(nr_transaction, "get_cohort_info"):
372
            user_cohort = get_cohort_id(request.user, course_key)
373

Rocky Duan committed
374
        context = {
375
            'discussion_id': discussion_id,
Rocky Duan committed
376
            'csrf': csrf(request)['csrf_token'],
Calen Pennington committed
377
            'init': '',   # TODO: What is this?
378 379
            'user_info': _attr_safe_json(user_info),
            'annotated_content_info': _attr_safe_json(annotated_content_info),
Rocky Duan committed
380
            'course': course,
381
            #'recent_active_threads': recent_active_threads,
382
            'course_id': course.id.to_deprecated_string(),   # TODO: Why pass both course and course.id to template?
383
            'thread_id': thread_id,
384
            'threads': _attr_safe_json(threads),
385
            'roles': _attr_safe_json(utils.get_role_ids(course_key)),
386
            'is_moderator': is_moderator,
387
            'thread_pages': query_params['num_pages'],
388
            'is_course_cohorted': is_course_cohorted(course_key),
389 390 391 392
            'flag_moderator': (
                has_permission(request.user, 'openclose_thread', course.id) or
                has_access(request.user, 'staff', course)
            ),
393 394
            'cohorts': course_settings["cohorts"],
            'user_cohort': user_cohort,
395
            'sort_preference': cc_user.default_sort_key,
396
            'category_map': course_settings["category_map"],
397 398 399 400 401 402
            'course_settings': _attr_safe_json(course_settings),
            'cohorted_commentables': (get_cohorted_commentables(course_id)),
            'has_permission_to_create_thread': cached_has_permission(request.user, "create_thread", course_id),
            'has_permission_to_create_comment': cached_has_permission(request.user, "create_comment", course_id),
            'has_permission_to_create_subcomment': cached_has_permission(request.user, "create_subcomment", course_id),
            'has_permission_to_openclose_thread': cached_has_permission(request.user, "openclose_thread", course_id)
Rocky Duan committed
403
        }
404
        return render_to_response('discussion/index.html', context)
405

406

407
@require_GET
408
@login_required
409 410 411 412 413 414 415
@use_bulk_ops
def user_profile(request, course_key, user_id):
    """
    Renders a response to display the user profile page (shown after clicking
    on a post author's username).
    """

416 417
    nr_transaction = newrelic.agent.current_transaction()

418
    #TODO: Allow sorting?
419
    course = get_course_with_access(request.user, 'load', course_key, check_if_enrolled=True)
420
    try:
421 422
        query_params = {
            'page': request.GET.get('page', 1),
Calen Pennington committed
423
            'per_page': THREADS_PER_PAGE,   # more than threads_per_page to show more activities
424
        }
425

426
        try:
427
            group_id = get_group_id_for_comments_service(request, course_key)
428 429 430 431
        except ValueError:
            return HttpResponseBadRequest("Invalid group_id")
        if group_id is not None:
            query_params['group_id'] = group_id
432 433 434
            profiled_user = cc.User(id=user_id, course_id=course_key, group_id=group_id)
        else:
            profiled_user = cc.User(id=user_id, course_id=course_key)
435

436
        threads, page, num_pages = profiled_user.active_threads(query_params)
437 438
        query_params['page'] = page
        query_params['num_pages'] = num_pages
439 440
        user_info = cc.User.from_django_user(request.user).to_dict()

441
        with newrelic.agent.FunctionTrace(nr_transaction, "get_metadata_for_threads"):
442
            annotated_content_info = utils.get_metadata_for_threads(course_key, threads, request.user, user_info)
443

444
        is_staff = has_permission(request.user, 'openclose_thread', course.id)
445
        threads = [utils.prepare_content(thread, course_key, is_staff) for thread in threads]
446 447
        if request.is_ajax():
            return utils.JsonResponse({
448
                'discussion_data': threads,
449 450
                'page': query_params['page'],
                'num_pages': query_params['num_pages'],
451
                'annotated_content_info': _attr_safe_json(annotated_content_info),
452 453
            })
        else:
454
            django_user = User.objects.get(id=user_id)
455 456 457
            context = {
                'course': course,
                'user': request.user,
458
                'django_user': django_user,
459
                'profiled_user': profiled_user.to_dict(),
460 461 462
                'threads': _attr_safe_json(threads),
                'user_info': _attr_safe_json(user_info),
                'annotated_content_info': _attr_safe_json(annotated_content_info),
463 464
                'page': query_params['page'],
                'num_pages': query_params['num_pages'],
465
                'learner_profile_page_url': reverse('learner_profile', kwargs={'username': django_user.username})
466 467 468
            }

            return render_to_response('discussion/user_profile.html', context)
469
    except User.DoesNotExist:
470
        raise Http404
471 472


473
@login_required
474 475 476 477 478 479
@use_bulk_ops
def followed_threads(request, course_key, user_id):
    """
    Ajax-only endpoint retrieving the threads followed by a specific user.
    """

480 481
    nr_transaction = newrelic.agent.current_transaction()

482
    course = get_course_with_access(request.user, 'load', course_key, check_if_enrolled=True)
483
    try:
484
        profiled_user = cc.User(id=user_id, course_id=course_key)
485

486 487
        default_query_params = {
            'page': 1,
Calen Pennington committed
488
            'per_page': THREADS_PER_PAGE,   # more than threads_per_page to show more activities
489 490
            'sort_key': 'date',
            'sort_order': 'desc',
491
        }
492

493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
        query_params = merge_dict(
            default_query_params,
            strip_none(
                extract(
                    request.GET,
                    [
                        'page',
                        'sort_key',
                        'sort_order',
                        'flagged',
                        'unread',
                        'unanswered',
                    ]
                )
            )
        )

510
        try:
511
            group_id = get_group_id_for_comments_service(request, course_key)
512 513 514 515 516
        except ValueError:
            return HttpResponseBadRequest("Invalid group_id")
        if group_id is not None:
            query_params['group_id'] = group_id

517 518 519 520 521
        threads, page, num_pages = profiled_user.subscribed_threads(query_params)
        query_params['page'] = page
        query_params['num_pages'] = num_pages
        user_info = cc.User.from_django_user(request.user).to_dict()

522
        with newrelic.agent.FunctionTrace(nr_transaction, "get_metadata_for_threads"):
523
            annotated_content_info = utils.get_metadata_for_threads(course_key, threads, request.user, user_info)
524
        if request.is_ajax():
525
            is_staff = has_permission(request.user, 'openclose_thread', course.id)
526 527
            return utils.JsonResponse({
                'annotated_content_info': annotated_content_info,
528
                'discussion_data': [utils.prepare_content(thread, course_key, is_staff) for thread in threads],
529 530
                'page': query_params['page'],
                'num_pages': query_params['num_pages'],
531
            })
532
        #TODO remove non-AJAX support, it does not appear to be used and does not appear to work.
533 534 535 536 537 538
        else:
            context = {
                'course': course,
                'user': request.user,
                'django_user': User.objects.get(id=user_id),
                'profiled_user': profiled_user.to_dict(),
539 540 541
                'threads': _attr_safe_json(threads),
                'user_info': _attr_safe_json(user_info),
                'annotated_content_info': _attr_safe_json(annotated_content_info),
542
                #                'content': content,
543
            }
544 545

            return render_to_response('discussion/user_profile.html', context)
546
    except User.DoesNotExist:
547
        raise Http404