Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
0d213810
Commit
0d213810
authored
Aug 07, 2014
by
Don Mitchell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove hardcoding of key syntax for base url
and /static and /jump_to_id link substitutions LMS-11198
parent
9efe5d92
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
44 deletions
+32
-44
common/lib/xmodule/xmodule/contentstore/content.py
+5
-3
common/lib/xmodule/xmodule/modulestore/store_utilities.py
+27
-41
No files found.
common/lib/xmodule/xmodule/contentstore/content.py
View file @
0d213810
import
re
import
re
import
uuid
XASSET_LOCATION_TAG
=
'c4x'
XASSET_LOCATION_TAG
=
'c4x'
XASSET_SRCREF_PREFIX
=
'xasset:'
XASSET_SRCREF_PREFIX
=
'xasset:'
...
@@ -103,9 +104,10 @@ class StaticContent(object):
...
@@ -103,9 +104,10 @@ class StaticContent(object):
return
None
return
None
assert
(
isinstance
(
course_key
,
CourseKey
))
assert
(
isinstance
(
course_key
,
CourseKey
))
# create a dummy asset location and then strip off the last character: 'a',
placeholder_id
=
uuid
.
uuid4
()
.
hex
# since the AssetLocator rejects the empty string as a legal value for the block_id.
# create a dummy asset location with a fake but unique name. strip off the name, and return it
return
course_key
.
make_asset_key
(
'asset'
,
'a'
)
.
for_branch
(
None
)
.
to_deprecated_string
()[:
-
1
]
url_path
=
unicode
(
course_key
.
make_asset_key
(
'asset'
,
placeholder_id
)
.
for_branch
(
None
))
return
url_path
.
replace
(
placeholder_id
,
''
)
@staticmethod
@staticmethod
def
get_location_from_path
(
path
):
def
get_location_from_path
(
path
):
...
...
common/lib/xmodule/xmodule/modulestore/store_utilities.py
View file @
0d213810
import
re
import
re
import
logging
import
logging
from
xmodule.contentstore.content
import
StaticContent
import
uuid
def
_prefix_only_url_replace_regex
(
p
refix
):
def
_prefix_only_url_replace_regex
(
p
attern
):
"""
"""
Match static urls in quotes that don't end in '?raw'.
Match urls in quotes pulling out the fields from pattern
To anyone contemplating making this more complicated:
http://xkcd.com/1171/
"""
return
ur"""
(?x) # flags=re.VERBOSE
(?P<quote>\\?['"]) # the opening quotes
(?P<prefix>{prefix}) # the prefix
(?P<rest>.*?) # everything else in the url
(?P=quote) # the first matching closing quote
"""
.
format
(
prefix
=
re
.
escape
(
prefix
))
def
_prefix_and_category_url_replace_regex
(
prefix
):
"""
Match static urls in quotes that don't end in '?raw'.
To anyone contemplating making this more complicated:
http://xkcd.com/1171/
"""
"""
return
ur"""
return
re
.
compile
(
ur"""
(?x) # flags=re.VERBOSE
(?x) # flags=re.VERBOSE
(?P<quote>\\?['"]) # the opening quotes
(?P<quote>\\?['"]) # the opening quotes
(?P<prefix>{prefix}) # the prefix
{}
(?P<category>[^/]+)/
(?P<rest>.*?) # everything else in the url
(?P=quote) # the first matching closing quote
(?P=quote) # the first matching closing quote
"""
.
format
(
p
refix
=
re
.
escape
(
prefix
))
"""
.
format
(
p
attern
))
def
rewrite_nonportable_content_links
(
source_course_id
,
dest_course_id
,
text
):
def
rewrite_nonportable_content_links
(
source_course_id
,
dest_course_id
,
text
):
"""
"""
Does a regex replace on non-portabl
e links:
rewrite any non-portable links to (->) relativ
e links:
/c4x/<org>/<course>/asset/<name> -> /static/<name>
/c4x/<org>/<course>/asset/<name> -> /static/<name>
/jump_to/i4x://<org>/<course>/<category>/<name> -> /jump_to_id/<id>
/jump_to/i4x://<org>/<course>/<category>/<name> -> /jump_to_id/<id>
"""
"""
def
portable_asset_link_subtitution
(
match
):
def
portable_asset_link_subtitution
(
match
):
quote
=
match
.
group
(
'quote'
)
quote
=
match
.
group
(
'quote'
)
rest
=
match
.
group
(
'rest
'
)
block_id
=
match
.
group
(
'block_id
'
)
return
quote
+
'/static/'
+
rest
+
quote
return
quote
+
'/static/'
+
block_id
+
quote
def
portable_jump_to_link_substitution
(
match
):
def
portable_jump_to_link_substitution
(
match
):
quote
=
match
.
group
(
'quote'
)
quote
=
match
.
group
(
'quote'
)
rest
=
match
.
group
(
'
rest
'
)
rest
=
match
.
group
(
'
block_id
'
)
return
quote
+
'/jump_to_id/'
+
rest
+
quote
return
quote
+
'/jump_to_id/'
+
rest
+
quote
# NOTE: ultimately link updating is not a hard requirement, so if something blows up with
# if something blows up, log the error and continue
# the regex substitution, log the error and continue
c4x_link_base
=
StaticContent
.
get_base_url_path_for_course_assets
(
source_course_id
)
# create a serialized template for what the id will look like in the source_course but with
# the block_id as a regex pattern
placeholder_id
=
uuid
.
uuid4
()
.
hex
asset_block_pattern
=
unicode
(
source_course_id
.
make_asset_key
(
'asset'
,
placeholder_id
))
asset_block_pattern
=
asset_block_pattern
.
replace
(
placeholder_id
,
r'(?P<block_id>.*?)'
)
try
:
try
:
text
=
re
.
sub
(
_prefix_only_url_replace_regex
(
c4x_link_base
),
portable_asset_link_subtitution
,
text
)
text
=
_prefix_only_url_replace_regex
(
asset_block_pattern
)
.
sub
(
portable_asset_link_subtitution
,
text
)
except
Exception
as
exc
:
# pylint: disable=broad-except
except
Exception
as
exc
:
# pylint: disable=broad-except
logging
.
warning
(
"Error producing regex substitution
%
r for text =
%
r.
\n\n
Error msg =
%
s"
,
c4x_link_base
,
text
,
str
(
exc
))
logging
.
warning
(
"Error producing regex substitution
%
r for text =
%
r.
\n\n
Error msg =
%
s"
,
asset_block_pattern
,
text
,
str
(
exc
))
jump_to_link_base
=
u'/courses/{course_key_string}/jump_to/i4x://{course_key.org}/{course_key.course}/'
.
format
(
placeholder_category
=
'cat_{}'
.
format
(
uuid
.
uuid4
()
.
hex
)
course_key_string
=
source_course_id
.
to_deprecated_string
(),
course_key
=
source_course_id
usage_block_pattern
=
unicode
(
source_course_id
.
make_usage_key
(
placeholder_category
,
placeholder_id
))
usage_block_pattern
=
usage_block_pattern
.
replace
(
placeholder_category
,
r'(?P<category>[^/+@]+)'
)
usage_block_pattern
=
usage_block_pattern
.
replace
(
placeholder_id
,
r'(?P<block_id>.*?)'
)
jump_to_link_base
=
ur'/courses/{course_key_string}/jump_to/{usage_key_string}'
.
format
(
course_key_string
=
unicode
(
source_course_id
),
usage_key_string
=
usage_block_pattern
)
)
try
:
try
:
text
=
re
.
sub
(
_prefix_and_category_url_replace_regex
(
jump_to_link_base
),
portable_jump_to_link_substitution
,
text
)
text
=
_prefix_only_url_replace_regex
(
jump_to_link_base
)
.
sub
(
portable_jump_to_link_substitution
,
text
)
except
Exception
as
exc
:
# pylint: disable=broad-except
except
Exception
as
exc
:
# pylint: disable=broad-except
logging
.
warning
(
"Error producing regex substitution
%
r for text =
%
r.
\n\n
Error msg =
%
s"
,
jump_to_link_base
,
text
,
str
(
exc
))
logging
.
warning
(
"Error producing regex substitution
%
r for text =
%
r.
\n\n
Error msg =
%
s"
,
jump_to_link_base
,
text
,
str
(
exc
))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment