From 6c3563cc1a70ec6e1d8a3657acf5d301d3ee760c Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Fri, 28 Jun 2013 15:00:59 -0400 Subject: [PATCH] Simple refactoring and reformatting of the view code. --- lms/djangoapps/staticbook/views.py | 89 ++++++++++++++++-------------- 1 file changed, 47 insertions(+), 42 deletions(-) diff --git a/lms/djangoapps/staticbook/views.py b/lms/djangoapps/staticbook/views.py index fcfba9e22c..52245525e9 100644 --- a/lms/djangoapps/staticbook/views.py +++ b/lms/djangoapps/staticbook/views.py @@ -22,20 +22,37 @@ def index(request, course_id, book_index, page=None): if page is None: page = textbook.start_page - 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}) + 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, + }, + ) def index_shifted(request, course_id, page): return index(request, course_id=course_id, page=int(page) + 24) +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), + course_namespace=course.location, + ) + # strip off the quotes again... + return output_url[1:-1] + + @login_required def pdf_index(request, course_id, book_index, chapter=None, page=None): """ @@ -60,16 +77,6 @@ def pdf_index(request, course_id, book_index, chapter=None, page=None): raise Http404("Invalid book index value: {0}".format(book_index)) textbook = course.pdf_textbooks[book_index] - def remap_static_url(original_url, course): - input_url = "'" + original_url + "'" - output_url = replace_static_urls( - input_url, - getattr(course, 'data_dir', None), - course_namespace=course.location - ) - # strip off the quotes again... - return output_url[1:-1] - if 'url' in textbook: textbook['url'] = remap_static_url(textbook['url'], course) # then remap all the chapter URLs as well, if they are provided. @@ -77,13 +84,17 @@ def pdf_index(request, course_id, book_index, chapter=None, page=None): for entry in textbook['chapters']: entry['url'] = remap_static_url(entry['url'], course) - return render_to_response('static_pdfbook.html', - {'book_index': book_index, - 'course': course, - 'textbook': textbook, - 'chapter': chapter, - 'page': page, - 'staff_access': staff_access}) + return render_to_response( + 'static_pdfbook.html', + { + 'book_index': book_index, + 'course': course, + 'textbook': textbook, + 'chapter': chapter, + 'page': page, + 'staff_access': staff_access, + }, + ) @login_required @@ -109,16 +120,6 @@ def html_index(request, course_id, book_index, chapter=None): raise Http404("Invalid book index value: {0}".format(book_index)) textbook = course.html_textbooks[book_index] - def remap_static_url(original_url, course): - input_url = "'" + original_url + "'" - output_url = replace_static_urls( - input_url, - getattr(course, 'data_dir', None), - course_namespace=course.location - ) - # strip off the quotes again... - return output_url[1:-1] - if 'url' in textbook: textbook['url'] = remap_static_url(textbook['url'], course) # then remap all the chapter URLs as well, if they are provided. @@ -126,10 +127,14 @@ def html_index(request, course_id, book_index, chapter=None): for entry in textbook['chapters']: entry['url'] = remap_static_url(entry['url'], course) - return render_to_response('static_htmlbook.html', - {'book_index': book_index, - 'course': course, - 'textbook': textbook, - 'chapter': chapter, - 'staff_access': staff_access, - 'notes_enabled': notes_enabled}) + return render_to_response( + 'static_htmlbook.html', + { + 'book_index': book_index, + 'course': course, + 'textbook': textbook, + 'chapter': chapter, + 'staff_access': staff_access, + 'notes_enabled': notes_enabled, + }, + )