views.py 6.25 KB
Newer Older
Ned Batchelder committed
1 2 3 4
"""
Views for serving static textbooks.
"""

Calen Pennington committed
5
from django.contrib.auth.decorators import login_required
6
from django.http import Http404
David Baumgold committed
7
from edxmako.shortcuts import render_to_response
8

9
from opaque_keys.edx.locations import SlashSeparatedCourseKey
10 11
from xmodule.annotator_token import retrieve_token

12
from courseware.access import has_access
13
from courseware.courses import get_course_with_access
14
from notes.utils import notes_enabled_for_course
15
from static_replace import replace_static_urls
16

Calen Pennington committed
17

18
@login_required
19
def index(request, course_id, book_index, page=None):
Ned Batchelder committed
20 21 22
    """
    Serve static image-based textbooks.
    """
23 24
    course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)
    course = get_course_with_access(request.user, 'load', course_key)
25
    staff_access = bool(has_access(request.user, 'staff', course))
26

Victor Shnayder committed
27
    book_index = int(book_index)
28 29
    if book_index < 0 or book_index >= len(course.textbooks):
        raise Http404("Invalid book index value: {0}".format(book_index))
Victor Shnayder committed
30
    textbook = course.textbooks[book_index]
31
    table_of_contents = textbook.table_of_contents
32

33 34 35
    if page is None:
        page = textbook.start_page

36 37 38 39 40 41 42 43 44 45 46 47
    return render_to_response(
        'staticbook.html',
        {
            'book_index': book_index, 'page': int(page),
            'course': course,
            'book_url': textbook.book_url,
            'table_of_contents': table_of_contents,
            'start_page': textbook.start_page,
            'end_page': textbook.end_page,
            'staff_access': staff_access,
        },
    )
48

Calen Pennington committed
49

50 51 52 53 54 55 56
def remap_static_url(original_url, course):
    """Remap a URL in the ways the course requires."""
    # Ick: this should be possible without having to quote and unquote the URL...
    input_url = "'" + original_url + "'"
    output_url = replace_static_urls(
        input_url,
        getattr(course, 'data_dir', None),
57
        course_id=course.id,
58
        static_asset_path=course.static_asset_path
59 60 61 62 63
    )
    # strip off the quotes again...
    return output_url[1:-1]


Brian Wilson committed
64 65
@login_required
def pdf_index(request, course_id, book_index, chapter=None, page=None):
66 67 68 69 70 71 72 73 74 75 76 77 78 79
    """
    Display a PDF textbook.

    course_id: course for which to display text.  The course should have
      "pdf_textbooks" property defined.

    book index:  zero-based index of which PDF textbook to display.

    chapter:  (optional) one-based index into the chapter array of textbook PDFs to display.
        Defaults to first chapter.  Specifying this assumes that there are separate PDFs for
        each chapter in a textbook.

    page:  (optional) one-based page number to display within the PDF.  Defaults to first page.
    """
80 81
    course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)
    course = get_course_with_access(request.user, 'load', course_key)
82
    staff_access = bool(has_access(request.user, 'staff', course))
Brian Wilson committed
83 84

    book_index = int(book_index)
85 86
    if book_index < 0 or book_index >= len(course.pdf_textbooks):
        raise Http404("Invalid book index value: {0}".format(book_index))
Brian Wilson committed
87 88
    textbook = course.pdf_textbooks[book_index]

89 90
    viewer_params = '&file='
    current_url = ''
Dave St.Germain committed
91

92 93
    if 'url' in textbook:
        textbook['url'] = remap_static_url(textbook['url'], course)
94 95 96
        viewer_params += textbook['url']
        current_url = textbook['url']

97
    # then remap all the chapter URLs as well, if they are provided.
98
    current_chapter = None
99 100
    if 'chapters' in textbook:
        for entry in textbook['chapters']:
101
            entry['url'] = remap_static_url(entry['url'], course)
102
        if chapter is not None and int(chapter) <= (len(textbook['chapters'])):
103 104 105 106 107 108
            current_chapter = textbook['chapters'][int(chapter) - 1]
        else:
            current_chapter = textbook['chapters'][0]
        viewer_params += current_chapter['url']
        current_url = current_chapter['url']

109
    viewer_params += '#zoom=page-fit&disableRange=true'
110
    if page is not None:
111
        viewer_params += '&page={}'.format(page)
112

113
    if request.GET.get('viewer', '') == 'true':
114 115 116
        template = 'pdf_viewer.html'
    else:
        template = 'static_pdfbook.html'
117

118
    return render_to_response(
119
        template,
120 121 122 123 124 125
        {
            'book_index': book_index,
            'course': course,
            'textbook': textbook,
            'chapter': chapter,
            'page': page,
126 127
            'viewer_params': viewer_params,
            'current_chapter': current_chapter,
128
            'staff_access': staff_access,
129
            'current_url': current_url,
130 131
        },
    )
132

133

134
@login_required
135
def html_index(request, course_id, book_index, chapter=None):
136 137 138 139 140 141 142 143 144 145 146 147
    """
    Display an HTML textbook.

    course_id: course for which to display text.  The course should have
      "html_textbooks" property defined.

    book index:  zero-based index of which HTML textbook to display.

    chapter:  (optional) one-based index into the chapter array of textbook HTML files to display.
        Defaults to first chapter.  Specifying this assumes that there are separate HTML files for
        each chapter in a textbook.
    """
148 149
    course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)
    course = get_course_with_access(request.user, 'load', course_key)
150
    staff_access = bool(has_access(request.user, 'staff', course))
151
    notes_enabled = notes_enabled_for_course(course)
152 153

    book_index = int(book_index)
154 155
    if book_index < 0 or book_index >= len(course.html_textbooks):
        raise Http404("Invalid book index value: {0}".format(book_index))
156 157 158 159 160 161 162 163 164
    textbook = course.html_textbooks[book_index]

    if 'url' in textbook:
        textbook['url'] = remap_static_url(textbook['url'], course)
    # then remap all the chapter URLs as well, if they are provided.
    if 'chapters' in textbook:
        for entry in textbook['chapters']:
            entry['url'] = remap_static_url(entry['url'], course)

daniel cebrian committed
165
    student = request.user
166 167 168 169 170 171 172
    return render_to_response(
        'static_htmlbook.html',
        {
            'book_index': book_index,
            'course': course,
            'textbook': textbook,
            'chapter': chapter,
daniel cebrian committed
173
            'student': student,
174 175
            'staff_access': staff_access,
            'notes_enabled': notes_enabled,
176 177
            'storage': course.annotation_storage_url,
            'token': retrieve_token(student.email, course.annotation_token_secret),
178 179
        },
    )