Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
I
insights
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
insights
Commits
d3576e7a
Commit
d3576e7a
authored
Mar 27, 2013
by
Vik Paruchuri
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Get rid of static replace
parent
37097506
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
0 additions
and
247 deletions
+0
-247
anserv/static_replace/__init__.py
+0
-121
anserv/static_replace/management/__init__.py
+0
-0
anserv/static_replace/management/commands/__init__.py
+0
-0
anserv/static_replace/management/commands/clear_collectstatic_cache.py
+0
-15
anserv/static_replace/test/test_static_replace.py
+0
-111
No files found.
anserv/static_replace/__init__.py
deleted
100644 → 0
View file @
37097506
import
logging
import
re
from
staticfiles.storage
import
staticfiles_storage
from
staticfiles
import
finders
from
django.conf
import
settings
from
xmodule.modulestore.django
import
modulestore
from
xmodule.modulestore.xml
import
XMLModuleStore
from
xmodule.contentstore.content
import
StaticContent
log
=
logging
.
getLogger
(
__name__
)
def
_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
r"""
(?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
=
prefix
)
def
try_staticfiles_lookup
(
path
):
"""
Try to lookup a path in staticfiles_storage. If it fails, return
a dead link instead of raising an exception.
"""
try
:
url
=
staticfiles_storage
.
url
(
path
)
except
Exception
as
err
:
log
.
warning
(
"staticfiles_storage couldn't find path {0}: {1}"
.
format
(
path
,
str
(
err
)))
# Just return the original path; don't kill everything.
url
=
path
return
url
def
replace_course_urls
(
text
,
course_id
):
"""
Replace /course/$stuff urls with /courses/$course_id/$stuff urls
text: The text to replace
course_module: A CourseDescriptor
returns: text with the links replaced
"""
def
replace_course_url
(
match
):
quote
=
match
.
group
(
'quote'
)
rest
=
match
.
group
(
'rest'
)
return
""
.
join
([
quote
,
'/courses/'
+
course_id
+
'/'
,
rest
,
quote
])
return
re
.
sub
(
_url_replace_regex
(
'/course/'
),
replace_course_url
,
text
)
def
replace_static_urls
(
text
,
data_directory
,
course_namespace
=
None
):
"""
Replace /static/$stuff urls either with their correct url as generated by collectstatic,
(/static/$md5_hashed_stuff) or by the course-specific content static url
/static/$course_data_dir/$stuff, or, if course_namespace is not None, by the
correct url in the contentstore (c4x://)
text: The source text to do the substitution in
data_directory: The directory in which course data is stored
course_namespace: The course identifier used to distinguish static content for this course in studio
"""
def
replace_static_url
(
match
):
original
=
match
.
group
(
0
)
prefix
=
match
.
group
(
'prefix'
)
quote
=
match
.
group
(
'quote'
)
rest
=
match
.
group
(
'rest'
)
# Don't mess with things that end in '?raw'
if
rest
.
endswith
(
'?raw'
):
return
original
# In debug mode, if we can find the url as is,
if
settings
.
DEBUG
and
finders
.
find
(
rest
,
True
):
return
original
# if we're running with a MongoBacked store course_namespace is not None, then use studio style urls
elif
course_namespace
is
not
None
and
not
isinstance
(
modulestore
(),
XMLModuleStore
):
# first look in the static file pipeline and see if we are trying to reference
# a piece of static content which is in the mitx repo (e.g. JS associated with an xmodule)
if
staticfiles_storage
.
exists
(
rest
):
url
=
staticfiles_storage
.
url
(
rest
)
else
:
# if not, then assume it's courseware_copy specific content and then look in the
# Mongo-backed database
url
=
StaticContent
.
convert_legacy_static_url
(
rest
,
course_namespace
)
# Otherwise, look the file up in staticfiles_storage, and append the data directory if needed
else
:
course_path
=
"/"
.
join
((
data_directory
,
rest
))
try
:
if
staticfiles_storage
.
exists
(
rest
):
url
=
staticfiles_storage
.
url
(
rest
)
else
:
url
=
staticfiles_storage
.
url
(
course_path
)
# And if that fails, assume that it's course content, and add manually data directory
except
Exception
as
err
:
log
.
warning
(
"staticfiles_storage couldn't find path {0}: {1}"
.
format
(
rest
,
str
(
err
)))
url
=
""
.
join
([
prefix
,
course_path
])
return
""
.
join
([
quote
,
url
,
quote
])
return
re
.
sub
(
_url_replace_regex
(
'/static/(?!{data_dir})'
.
format
(
data_dir
=
data_directory
)),
replace_static_url
,
text
)
anserv/static_replace/management/__init__.py
deleted
100644 → 0
View file @
37097506
anserv/static_replace/management/commands/__init__.py
deleted
100644 → 0
View file @
37097506
anserv/static_replace/management/commands/clear_collectstatic_cache.py
deleted
100644 → 0
View file @
37097506
###
### Script for importing courseware_copy from XML format
###
from
django.core.management.base
import
NoArgsCommand
from
django.core.cache
import
get_cache
class
Command
(
NoArgsCommand
):
help
=
\
'''Import the specified data directory into the default ModuleStore'''
def
handle_noargs
(
self
,
**
options
):
staticfiles_cache
=
get_cache
(
'staticfiles'
)
staticfiles_cache
.
clear
()
anserv/static_replace/test/test_static_replace.py
deleted
100644 → 0
View file @
37097506
import
re
from
nose.tools
import
assert_equals
,
assert_true
,
assert_false
from
static_replace
import
(
replace_static_urls
,
replace_course_urls
,
_url_replace_regex
)
from
mock
import
patch
,
Mock
from
xmodule.modulestore
import
Location
from
xmodule.modulestore.mongo
import
MongoModuleStore
from
xmodule.modulestore.xml
import
XMLModuleStore
DATA_DIRECTORY
=
'data_dir'
COURSE_ID
=
'org/course/run'
NAMESPACE
=
Location
(
'org'
,
'course'
,
'run'
,
None
,
None
)
STATIC_SOURCE
=
'"/static/file.png"'
def
test_multi_replace
():
course_source
=
'"/course/file.png"'
assert_equals
(
replace_static_urls
(
STATIC_SOURCE
,
DATA_DIRECTORY
),
replace_static_urls
(
replace_static_urls
(
STATIC_SOURCE
,
DATA_DIRECTORY
),
DATA_DIRECTORY
)
)
assert_equals
(
replace_course_urls
(
course_source
,
COURSE_ID
),
replace_course_urls
(
replace_course_urls
(
course_source
,
COURSE_ID
),
COURSE_ID
)
)
@patch
(
'static_replace.staticfiles_storage'
)
def
test_storage_url_exists
(
mock_storage
):
mock_storage
.
exists
.
return_value
=
True
mock_storage
.
url
.
return_value
=
'/static/file.png'
assert_equals
(
'"/static/file.png"'
,
replace_static_urls
(
STATIC_SOURCE
,
DATA_DIRECTORY
))
mock_storage
.
exists
.
called_once_with
(
'file.png'
)
mock_storage
.
url
.
called_once_with
(
'data_dir/file.png'
)
@patch
(
'static_replace.staticfiles_storage'
)
def
test_storage_url_not_exists
(
mock_storage
):
mock_storage
.
exists
.
return_value
=
False
mock_storage
.
url
.
return_value
=
'/static/data_dir/file.png'
assert_equals
(
'"/static/data_dir/file.png"'
,
replace_static_urls
(
STATIC_SOURCE
,
DATA_DIRECTORY
))
mock_storage
.
exists
.
called_once_with
(
'file.png'
)
mock_storage
.
url
.
called_once_with
(
'file.png'
)
@patch
(
'static_replace.StaticContent'
)
@patch
(
'static_replace.modulestore'
)
def
test_mongo_filestore
(
mock_modulestore
,
mock_static_content
):
mock_modulestore
.
return_value
=
Mock
(
MongoModuleStore
)
mock_static_content
.
convert_legacy_static_url
.
return_value
=
"c4x://mock_url"
# No namespace => no change to path
assert_equals
(
'"/static/data_dir/file.png"'
,
replace_static_urls
(
STATIC_SOURCE
,
DATA_DIRECTORY
))
# Namespace => content url
assert_equals
(
'"'
+
mock_static_content
.
convert_legacy_static_url
.
return_value
+
'"'
,
replace_static_urls
(
STATIC_SOURCE
,
DATA_DIRECTORY
,
NAMESPACE
)
)
mock_static_content
.
convert_legacy_static_url
.
assert_called_once_with
(
'file.png'
,
NAMESPACE
)
@patch
(
'static_replace.settings'
)
@patch
(
'static_replace.modulestore'
)
@patch
(
'static_replace.staticfiles_storage'
)
def
test_data_dir_fallback
(
mock_storage
,
mock_modulestore
,
mock_settings
):
mock_modulestore
.
return_value
=
Mock
(
XMLModuleStore
)
mock_storage
.
url
.
side_effect
=
Exception
mock_storage
.
exists
.
return_value
=
True
assert_equals
(
'"/static/data_dir/file.png"'
,
replace_static_urls
(
STATIC_SOURCE
,
DATA_DIRECTORY
))
mock_storage
.
exists
.
return_value
=
False
assert_equals
(
'"/static/data_dir/file.png"'
,
replace_static_urls
(
STATIC_SOURCE
,
DATA_DIRECTORY
))
def
test_raw_static_check
():
"""
Make sure replace_static_urls leaves alone things that end in '.raw'
"""
path
=
'"/static/foo.png?raw"'
assert_equals
(
path
,
replace_static_urls
(
path
,
DATA_DIRECTORY
))
text
=
'text <tag a="/static/js/capa/protex/protex.nocache.js?raw"/><div class="'
assert_equals
(
path
,
replace_static_urls
(
path
,
text
))
def
test_regex
():
yes
=
(
'"/static/foo.png"'
,
'"/static/foo.png"'
,
"'/static/foo.png'"
)
no
=
(
'"/not-static/foo.png"'
,
'"/static/foo'
,
# no matching quote
)
regex
=
_url_replace_regex
(
'/static/'
)
for
s
in
yes
:
print
'Should match: {0!r}'
.
format
(
s
)
assert_true
(
re
.
match
(
regex
,
s
))
for
s
in
no
:
print
'Should not match: {0!r}'
.
format
(
s
)
assert_false
(
re
.
match
(
regex
,
s
))
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