Commit 214a3bd2 by Don Mitchell

pylint/pep8 cleanups

parent ee2a3750
...@@ -51,6 +51,7 @@ def get_course_by_id(course_id, depth=0): ...@@ -51,6 +51,7 @@ def get_course_by_id(course_id, depth=0):
except InvalidLocationError: except InvalidLocationError:
raise Http404("Invalid location") raise Http404("Invalid location")
def get_course_with_access(user, course_id, action, depth=0): def get_course_with_access(user, course_id, action, depth=0):
""" """
Given a course_id, look up the corresponding course descriptor, Given a course_id, look up the corresponding course descriptor,
...@@ -85,24 +86,24 @@ def course_image_url(course): ...@@ -85,24 +86,24 @@ def course_image_url(course):
if course.static_asset_path or modulestore().get_modulestore_type(course.location.course_id) == XML_MODULESTORE_TYPE: if course.static_asset_path or modulestore().get_modulestore_type(course.location.course_id) == XML_MODULESTORE_TYPE:
return '/static/' + (course.static_asset_path or getattr(course, 'data_dir', '')) + "/images/course_image.jpg" return '/static/' + (course.static_asset_path or getattr(course, 'data_dir', '')) + "/images/course_image.jpg"
else: else:
loc = course.location._replace(tag='c4x', category='asset', name=course.course_image) loc = course.location.replace(tag='c4x', category='asset', name=course.course_image)
_path = StaticContent.get_url_path_from_location(loc) _path = StaticContent.get_url_path_from_location(loc)
return _path return _path
def find_file(fs, dirs, filename): def find_file(filesystem, dirs, filename):
""" """
Looks for a filename in a list of dirs on a filesystem, in the specified order. Looks for a filename in a list of dirs on a filesystem, in the specified order.
fs: an OSFS filesystem filesystem: an OSFS filesystem
dirs: a list of path objects dirs: a list of path objects
filename: a string filename: a string
Returns d / filename if found in dir d, else raises ResourceNotFoundError. Returns d / filename if found in dir d, else raises ResourceNotFoundError.
""" """
for d in dirs: for directory in dirs:
filepath = path(d) / filename filepath = path(directory) / filename
if fs.exists(filepath): if filesystem.exists(filepath):
return filepath return filepath
raise ResourceNotFoundError("Could not find {0}".format(filename)) raise ResourceNotFoundError("Could not find {0}".format(filename))
...@@ -146,7 +147,7 @@ def get_course_about_section(course, section_key): ...@@ -146,7 +147,7 @@ def get_course_about_section(course, section_key):
request = get_request_for_thread() request = get_request_for_thread()
loc = course.location._replace(category='about', name=section_key) loc = course.location.replace(category='about', name=section_key)
# Use an empty cache # Use an empty cache
field_data_cache = FieldDataCache([], course.id, request.user) field_data_cache = FieldDataCache([], course.id, request.user)
...@@ -182,7 +183,6 @@ def get_course_about_section(course, section_key): ...@@ -182,7 +183,6 @@ def get_course_about_section(course, section_key):
raise KeyError("Invalid about key " + str(section_key)) raise KeyError("Invalid about key " + str(section_key))
def get_course_info_section(request, course, section_key): def get_course_info_section(request, course, section_key):
""" """
This returns the snippet of html to be rendered on the course info page, This returns the snippet of html to be rendered on the course info page,
...@@ -194,8 +194,6 @@ def get_course_info_section(request, course, section_key): ...@@ -194,8 +194,6 @@ def get_course_info_section(request, course, section_key):
- updates - updates
- guest_updates - guest_updates
""" """
loc = Location(course.location.tag, course.location.org, course.location.course, 'course_info', section_key) loc = Location(course.location.tag, course.location.org, course.location.course, 'course_info', section_key)
# Use an empty cache # Use an empty cache
...@@ -237,13 +235,13 @@ def get_course_syllabus_section(course, section_key): ...@@ -237,13 +235,13 @@ def get_course_syllabus_section(course, section_key):
if section_key in ['syllabus', 'guest_syllabus']: if section_key in ['syllabus', 'guest_syllabus']:
try: try:
fs = course.system.resources_fs filesys = course.system.resources_fs
# first look for a run-specific version # first look for a run-specific version
dirs = [path("syllabus") / course.url_name, path("syllabus")] dirs = [path("syllabus") / course.url_name, path("syllabus")]
filepath = find_file(fs, dirs, section_key + ".html") filepath = find_file(filesys, dirs, section_key + ".html")
with fs.open(filepath) as htmlFile: with filesys.open(filepath) as html_file:
return replace_static_urls( return replace_static_urls(
htmlFile.read().decode('utf-8'), html_file.read().decode('utf-8'),
getattr(course, 'data_dir', None), getattr(course, 'data_dir', None),
course_id=course.location.course_id, course_id=course.location.course_id,
static_asset_path=course.static_asset_path, static_asset_path=course.static_asset_path,
......
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
"""
Tests for course access
"""
import mock import mock
from django.test import TestCase from django.test import TestCase
...@@ -9,11 +12,12 @@ from xmodule.modulestore.django import get_default_store_name_for_current_reques ...@@ -9,11 +12,12 @@ from xmodule.modulestore.django import get_default_store_name_for_current_reques
CMS_BASE_TEST = 'testcms' CMS_BASE_TEST = 'testcms'
class CoursesTest(TestCase): class CoursesTest(TestCase):
def test_get_course_by_id_invalid_chars(self): def test_get_course_by_id_invalid_chars(self):
""" """
Test that `get_course_by_id` throws a 404, rather than Test that `get_course_by_id` throws a 404, rather than
an exception, when faced with unexpected characters an exception, when faced with unexpected characters
(such as unicode characters, and symbols such as = and ' ') (such as unicode characters, and symbols such as = and ' ')
""" """
with self.assertRaises(Http404): with self.assertRaises(Http404):
...@@ -30,13 +34,12 @@ class CoursesTest(TestCase): ...@@ -30,13 +34,12 @@ class CoursesTest(TestCase):
self.assertEqual("//{}/".format(CMS_BASE_TEST), get_cms_course_link_by_id("too/too/many/slashes")) self.assertEqual("//{}/".format(CMS_BASE_TEST), get_cms_course_link_by_id("too/too/many/slashes"))
self.assertEqual("//{}/org/num/course/name".format(CMS_BASE_TEST), get_cms_course_link_by_id('org/num/name')) self.assertEqual("//{}/org/num/course/name".format(CMS_BASE_TEST), get_cms_course_link_by_id('org/num/name'))
@mock.patch('xmodule.modulestore.django.get_current_request_hostname', mock.Mock(return_value='preview.localhost')) @mock.patch('xmodule.modulestore.django.get_current_request_hostname', mock.Mock(return_value='preview.localhost'))
@override_settings(HOSTNAME_MODULESTORE_DEFAULT_MAPPINGS={'preview\.': 'draft'}) @override_settings(HOSTNAME_MODULESTORE_DEFAULT_MAPPINGS={r'preview\.': 'draft'})
def test_default_modulestore_preview_mapping(self): def test_default_modulestore_preview_mapping(self):
self.assertEqual(get_default_store_name_for_current_request(), 'draft') self.assertEqual(get_default_store_name_for_current_request(), 'draft')
@mock.patch('xmodule.modulestore.django.get_current_request_hostname', mock.Mock(return_value='localhost')) @mock.patch('xmodule.modulestore.django.get_current_request_hostname', mock.Mock(return_value='localhost'))
@override_settings(HOSTNAME_MODULESTORE_DEFAULT_MAPPINGS={'preview\.': 'draft'}) @override_settings(HOSTNAME_MODULESTORE_DEFAULT_MAPPINGS={r'preview\.': 'draft'})
def test_default_modulestore_published_mapping(self): def test_default_modulestore_published_mapping(self):
self.assertEqual(get_default_store_name_for_current_request(), 'default') self.assertEqual(get_default_store_name_for_current_request(), 'default')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment