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
75967bd2
Commit
75967bd2
authored
Mar 31, 2013
by
Piotr Mitros
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Render code isolated to analytics modules
parent
4c6d6e0c
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
98 additions
and
8 deletions
+98
-8
src/djanalytics/core/render.py
+80
-0
src/djanalytics/core/views.py
+2
-2
src/djanalytics/modules/testmodule/__init__.py
+6
-1
src/djanalytics/modules/testmodule/templates/hello.html
+1
-0
src/djanalytics/modules/tests.py
+4
-0
src/djanalytics/settings.py
+1
-1
src/djanalytics/urls.py
+4
-4
No files found.
src/djanalytics/core/render.py
0 → 100644
View file @
75967bd2
### HACK HACK HACK ###
#
# This is a horrible hack to make a render() function for modules.
#
# In the future, each module should run within its own space
# (globals(), locals()), but for now, this kind of works.
#
# This code:
# 1. Looks at the stack to figure out the calling module
# 2. Matches that up to a module in INSTALLED_ANALYTICS_MODULE (based
# on longest matching path)
# 3. Uses pkg_resources to find the place where templates are
# stored (this is part of setuptools)
# 4. Renders the template with Mako
#
# I apologize about this code, but the alternative would be to
# ship without this, in which case we'd accrue technical debt
# with each new module written.
#
### HACK HACK HACK ###
import
atexit
import
importlib
import
os.path
import
shutil
import
sys
import
tempfile
import
traceback
from
pkg_resources
import
resource_filename
from
mako.lookup
import
TemplateLookup
from
django.conf
import
settings
## Code borrowed from mitx/common/lib/tempdir
def
mkdtemp_clean
(
suffix
=
""
,
prefix
=
"tmp"
,
dir
=
None
):
"""Just like mkdtemp, but the directory will be deleted when the process ends."""
the_dir
=
tempfile
.
mkdtemp
(
suffix
=
suffix
,
prefix
=
prefix
,
dir
=
dir
)
atexit
.
register
(
cleanup_tempdir
,
the_dir
)
return
the_dir
def
cleanup_tempdir
(
the_dir
):
"""Called on process exit to remove a temp directory."""
if
os
.
path
.
exists
(
the_dir
):
shutil
.
rmtree
(
the_dir
)
module_directory
=
getattr
(
settings
,
'MAKO_MODULE_DIR'
,
None
)
if
module_directory
is
None
:
module_directory
=
mkdtemp_clean
()
lookups
=
{}
def
lookup
(
directory
):
if
directory
in
lookups
:
return
lookups
[
directory
]
else
:
l
=
TemplateLookup
(
directories
=
[
directory
],
module_directory
=
module_directory
,
output_encoding
=
'utf-8'
,
input_encoding
=
'utf-8'
,
encoding_errors
=
'replace'
)
lookups
[
directory
]
=
l
return
l
def
render
(
templatefile
,
context
,
caller
=
None
):
stack
=
traceback
.
extract_stack
()
if
not
caller
:
caller_path
=
os
.
path
.
abspath
(
stack
[
-
2
][
0
])
# For testing, use: sys.modules.keys() if sys.modules[module] and '__file__' in sys.modules[module].__dict__]#
analytics_modules
=
[
sys
.
modules
[
module
]
for
module
in
settings
.
INSTALLED_ANALYTICS_MODULES
]
analytics_modules
.
sort
(
key
=
lambda
x
:
len
(
os
.
path
.
commonprefix
([
x
.
__file__
,
os
.
path
.
abspath
(
caller_path
)])))
caller_module
=
analytics_modules
[
-
1
]
caller_name
=
caller_module
.
__name__
template_directory
=
os
.
path
.
abspath
(
resource_filename
(
caller_name
,
"templates"
))
print
"Caller, "
,
template_directory
template
=
lookup
(
template_directory
)
.
get_template
(
templatefile
)
return
template
.
render_unicode
(
**
context
)
return
"Hello!"
src/djanalytics/core/views.py
View file @
75967bd2
...
...
@@ -83,8 +83,8 @@ def handle_view(request, category, name, **kwargs):
Category is where this should be place (per student, per problem, etc.)
Name is specific
'''
if
not
request
.
user
.
is_authenticated
():
return
HttpResponseRedirect
(
reverse
(
'django.contrib.auth.views.login'
))
#
if not request.user.is_authenticated():
#
return HttpResponseRedirect(reverse('django.contrib.auth.views.login'))
return
HttpResponse
(
handle_request
(
request
,
'view'
,
category
,
name
,
**
kwargs
))
...
...
src/djanalytics/modules/testmodule/__init__.py
View file @
75967bd2
...
...
@@ -4,7 +4,12 @@
modules_to_import
=
[]
from
djanalytics.core.decorators
import
query
,
event_handler
from
djanalytics.core.decorators
import
query
,
event_handler
,
view
@view
()
def
hello_template
():
from
djanalytics.core.render
import
render
return
render
(
"hello.html"
,
{})
@query
()
def
event_count
(
db
):
...
...
src/djanalytics/modules/testmodule/templates/hello.html
0 → 100644
View file @
75967bd2
<html>
Hello World
</html>
src/djanalytics/modules/tests.py
View file @
75967bd2
...
...
@@ -93,3 +93,7 @@ class SimpleTest(TestCase):
self
.
send_event
(
c
,
{
'filename'
:
"foo4.txt"
,
'fs_forgets_expiry'
:
-
15
})
expire_objects
()
verify
({
"foo1.txt"
:
False
,
"foo2.txt"
:
False
,
"foo3.txt"
:
False
,
"foo4.txt"
:
False
})
def
test_render
(
self
):
c
=
Client
()
self
.
assertEqual
(
c
.
get
(
'/view/global/hello_template'
)
.
content
,
open
(
'modules/testmodule/templates/hello.html'
)
.
read
())
src/djanalytics/settings.py
View file @
75967bd2
...
...
@@ -22,7 +22,7 @@ DJFS = { 'type' : 'osfs',
TIME_BETWEEN_DATA_REGENERATION
=
datetime
.
timedelta
(
minutes
=
1
)
INSTALLED_ANALYTICS_MODULES
=
(
'modules.testmodule'
,)
INSTALLED_ANALYTICS_MODULES
=
(
'
djanalytics.
modules.testmodule'
,)
#Initialize celery
import
djcelery
...
...
src/djanalytics/urls.py
View file @
75967bd2
...
...
@@ -37,8 +37,8 @@ if settings.DEBUG and settings.DJFS['type'] == 'osfs':
'show_indexes'
:
True
,
}),
)
else
:
urlpatterns
+=
patterns
(
'frontend.views'
,
url
(
r'^data/(?P<path>.*)$'
,
'protected_data'
)
)
#
else:
#
urlpatterns+= patterns('frontend.views',
#
url(r'^data/(?P<path>.*)$', 'protected_data')
#
)
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