views.py 3.01 KB
Newer Older
Dave St.Germain committed
1 2 3 4
"""
Views for course info API
"""
from django.http import Http404
5
from rest_framework import generics
6 7
from rest_framework.response import Response

8
from courseware.courses import get_course_info_section_module
9
from static_replace import make_static_urls_absolute, replace_static_urls
10
from openedx.core.lib.xblock_utils import get_course_update_items
11

12
from ..utils import mobile_view, mobile_course_access
13

14

15
@mobile_view()
16
class CourseUpdatesList(generics.ListAPIView):
Mark Hoeber committed
17 18 19 20 21
    """
    **Use Case**

        Get the content for course updates.

22
    **Example Request**
Mark Hoeber committed
23 24 25 26

        GET /api/mobile/v0.5/course_info/{organization}/{course_number}/{course_run}/updates

    **Response Values**
27

28 29 30
        If the request is successful, the request returns an HTTP 200 "OK"
        response along with an array of course updates. Each course update
        contains the following values.
Mark Hoeber committed
31

32
            * content: The content, as an HTML string, of the course update.
33
            * date: The date of the course update.
Mark Hoeber committed
34
            * id: The unique identifier of the update.
35
            * status: Whether the update is visible or not.
36 37
    """

38 39
    @mobile_course_access()
    def list(self, request, course, *args, **kwargs):
40
        course_updates_module = get_course_info_section_module(request, course, 'updates')
41
        update_items = get_course_update_items(course_updates_module)
42

43
        updates_to_show = [
44
            update for update in update_items
45 46
            if update.get("status") != "deleted"
        ]
47 48 49 50 51

        for item in updates_to_show:
            content = item['content']
            content = replace_static_urls(
                content,
52
                course_id=course.id,
53 54 55
                static_asset_path=course.static_asset_path)
            item['content'] = make_static_urls_absolute(request, content)

56
        return Response(updates_to_show)
57 58


59
@mobile_view()
60
class CourseHandoutsList(generics.ListAPIView):
Mark Hoeber committed
61 62 63 64 65
    """
    **Use Case**

        Get the HTML for course handouts.

66
    **Example Request**
Mark Hoeber committed
67 68 69 70 71

        GET /api/mobile/v0.5/course_info/{organization}/{course_number}/{course_run}/handouts

    **Response Values**

72 73 74
        If the request is successful, the request returns an HTTP 200 "OK"
        response along with the following value.

Mark Hoeber committed
75
        * handouts_html: The HTML for course handouts.
76 77
    """

78 79
    @mobile_course_access()
    def list(self, request, course, *args, **kwargs):
80
        course_handouts_module = get_course_info_section_module(request, course, 'handouts')
Dave St.Germain committed
81
        if course_handouts_module:
82 83 84
            handouts_html = course_handouts_module.data
            handouts_html = replace_static_urls(
                handouts_html,
85
                course_id=course.id,
86 87 88
                static_asset_path=course.static_asset_path)
            handouts_html = make_static_urls_absolute(self.request, handouts_html)
            return Response({'handouts_html': handouts_html})
Dave St.Germain committed
89 90
        else:
            # course_handouts_module could be None if there are no handouts
91
            raise Http404(u"No handouts for {}".format(unicode(course.id)))