Skip to content

Commit dfc9136

Browse files
Merge PR #324: fix: deprecated edx-platform import references
2 parents 3cb02ec + 76ebd37 commit dfc9136

File tree

7 files changed

+68
-29
lines changed

7 files changed

+68
-29
lines changed

problem_builder/mentoring.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,13 +349,11 @@ def allowed_nested_blocks(self):
349349
except ImportError:
350350
pass
351351

352-
try:
353-
from xblock_django.models import XBlockConfiguration
352+
from .platform_dependencies import XBlockConfiguration
353+
if XBlockConfiguration:
354354
opt = XBlockConfiguration.objects.filter(name="pb-swipe")
355355
if opt.count() and opt.first().enabled:
356356
additional_blocks.append(SwipeBlock)
357-
except ImportError:
358-
pass
359357

360358
try:
361359
from ooyala_player.ooyala_player import OoyalaPlayerBlock

problem_builder/mixins.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,9 @@ def expand_static_url(self, text):
287287
elif hasattr(self.runtime, 'course_id'):
288288
# edX Studio uses a different runtime for 'studio_view' than 'student_view',
289289
# and the 'studio_view' runtime doesn't provide the replace_urls API.
290-
try:
291-
from static_replace import replace_static_urls # pylint: disable=import-error
290+
from .platform_dependencies import replace_static_urls
291+
if replace_static_urls:
292292
text = replace_static_urls(text, course_id=self.runtime.course_id)
293-
except ImportError:
294-
pass
295293
return text
296294

297295

problem_builder/models.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,7 @@
2424
from django.db import models
2525
from django.db.models.signals import pre_delete
2626

27-
try:
28-
# Koa and earlier: use shortened import path.
29-
# This will raise a warning in Koa, but that's OK.
30-
from student.models import AnonymousUserId
31-
except Exception: # pylint: disable=broad-except
32-
# (catch broadly, since the exception could manifest as either an ImportError
33-
# or an EdxPlatformDeprecatedImportError, the latter of which is not a subclass
34-
# of the former, and only exists on edx-platform master between Koa and Lilac).
35-
try:
36-
# Post-Koa: we must use the full import path.
37-
from common.djangoapps.student.models import AnonymousUserId
38-
except ImportError:
39-
# If we get here, we are not running within edx-platform
40-
# (e.g., we are running problem-builder unit tests).
41-
AnonymousUserId = None
27+
from .platform_dependencies import AnonymousUserId
4228

4329

4430
# Classes ###########################################################
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (c) 2014-2015 Harvard, edX & OpenCraft
4+
#
5+
# This software's license gives you freedom; you can copy, convey,
6+
# propagate, redistribute and/or modify this program under the terms of
7+
# the GNU Affero General Public License (AGPL) as published by the Free
8+
# Software Foundation (FSF), either version 3 of the License, or (at your
9+
# option) any later version of the AGPL published by the FSF.
10+
#
11+
# This program is distributed in the hope that it will be useful, but
12+
# WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
14+
# General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Affero General Public License
17+
# along with this program in a file in the toplevel directory called
18+
# "AGPLv3". If not, see <http://www.gnu.org/licenses/>.
19+
#
20+
21+
# PURPOSE OF THIS MODULE:
22+
# problem-builder has a couple dependencies on models in the edx-platform
23+
# repository. This comes with two challenges:
24+
# 1. We cannot import from edx-platform during unit tests, because
25+
# it is not installed into the testing environment.
26+
# 2. Some edx-platform import paths differ between Open edX releases.
27+
# In the interest of performing these imports in a consistent way,
28+
# we centralize the imports here, to be re-imported by other modules.
29+
30+
# pylint: disable=unused-import
31+
32+
try:
33+
# Koa and earlier: use shortened import path.
34+
# This will raise a warning in Koa, but that's OK.
35+
from courseware.models import StudentModule
36+
from static_replace import replace_static_urls
37+
from student.models import AnonymousUserId
38+
from xblock_django.models import XBlockConfiguration
39+
except Exception: # pylint: disable=broad-except
40+
# (catch broadly, since the exception could manifest as either an ImportError
41+
# or an EdxPlatformDeprecatedImportError, the latter of which is not a subclass
42+
# of the former, and only exists on edx-platform master between Koa and Lilac).
43+
try:
44+
# Post-Koa: we must use the full import path.
45+
from lms.djangoapps.courseware.models import StudentModule
46+
from common.djangoapps.static_replace import replace_static_urls
47+
from common.djangoapps.student.models import AnonymousUserId
48+
from common.djangoapps.xblock_django.models import XBlockConfiguration
49+
except ImportError:
50+
# If we get here, we are not running within edx-platform
51+
# (e.g., we are running problem-builder unit tests).
52+
StudentModule = None
53+
replace_static_urls = None
54+
AnonymousUserId = None
55+
XBlockConfiguration = None

problem_builder/swipe.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,9 @@ def expand_static_url(self, url):
151151
elif hasattr(self.runtime, 'course_id'):
152152
# edX Studio uses a different runtime for 'studio_view' than 'student_view',
153153
# and the 'studio_view' runtime doesn't provide the replace_urls API.
154-
try:
155-
from static_replace import replace_static_urls # pylint: disable=import-error
154+
from .platform_dependencies import replace_static_urls
155+
if replace_static_urls:
156156
url = replace_static_urls('"{}"'.format(url), None, course_id=self.runtime.course_id)[1:-1]
157-
except ImportError:
158-
pass
159157
return url
160158

161159
def mentoring_view(self, context=None):

problem_builder/v1/upgrade.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,18 @@
3737
from lxml import etree
3838
from six import StringIO
3939

40-
from courseware.models import StudentModule
4140
from mentoring import MentoringBlock
4241
from problem_builder.mentoring import MentoringBlock as NewMentoringBlock
4342

43+
from .platform_dependencies import StudentModule
4444
from .studio_xml_utils import studio_update_from_node
4545
from .xml_changes import convert_xml_to_v2
4646

4747

48+
if not StudentModule:
49+
raise ImportError("Could not import StudentModule from edx-platform courseware app.")
50+
51+
4852
def upgrade_block(store, block, from_version="v1"):
4953
"""
5054
Given a MentoringBlock "block" with old-style (v1) data in its "xml_content" field, parse

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
# Constants #########################################################
3030

31-
VERSION = '4.1.10'
31+
VERSION = '4.1.11'
3232

3333
# Functions #########################################################
3434

0 commit comments

Comments
 (0)