Commit 7fe75030 by ichuang

fix staff edit link in module content display (goes to github)

parent 85af1d88
import re
import json
import logging
from django.conf import settings
from functools import wraps
from static_replace import replace_urls
from mitxmako.shortcuts import render_to_string
from xmodule.seq_module import SequenceModule
from xmodule.vertical_module import VerticalModule
log = logging.getLogger("mitx.xmodule_modifiers")
def wrap_xmodule(get_html, module, template):
"""
......@@ -69,27 +75,33 @@ def add_histogram(get_html, module):
the output of the old get_html function with additional information
for admin users only, including a histogram of student answers and the
definition of the xmodule
Does nothing if module is a SequenceModule
"""
@wraps(get_html)
def _get_html():
if type(module) in [SequenceModule, VerticalModule]: # TODO: make this more general, eg use an XModule attribute instead
return get_html()
module_id = module.id
histogram = grade_histogram(module_id)
render_histogram = len(histogram) > 0
# TODO: fixme - no filename in module.xml in general (this code block
# for edx4edx) the following if block is for summer 2012 edX course
# development; it will change when the CMS comes online
if settings.MITX_FEATURES.get('DISPLAY_EDIT_LINK') and settings.DEBUG and module_xml.get('filename') is not None:
coursename = multicourse_settings.get_coursename_from_request(request)
github_url = multicourse_settings.get_course_github_url(coursename)
fn = module_xml.get('filename')
if module_xml.tag=='problem': fn = 'problems/' + fn # grrr
edit_link = (github_url + '/tree/master/' + fn) if github_url is not None else None
if module_xml.tag=='problem': edit_link += '.xml' # grrr
# TODO (ichuang): Remove after fall 2012 LMS migration done
if settings.MITX_FEATURES.get('ENABLE_LMS_MIGRATION'):
filename = module.definition.get('filename','')
log.debug('filename = %s' % filename)
data_dir = module.system.filestore.root_path.rsplit('/')[-1]
edit_link = "https://github.com/MITx/%s/tree/master/%s" % (data_dir,filename)
log.debug('edit_link = %s' % edit_link)
log.debug('module = %s' % dir(module))
log.debug('module type = %s' % type(module))
log.debug('location = %s' % str(module.location))
else:
edit_link = False
staff_context = {'definition': json.dumps(module.definition, indent=4),
staff_context = {'definition': module.definition.get('data'),
'metadata': json.dumps(module.metadata, indent=4),
'element_id': module.location.html_id(),
'edit_link': edit_link,
......@@ -99,3 +111,4 @@ def add_histogram(get_html, module):
return render_to_string("staff_problem_info.html", staff_context)
return _get_html
......@@ -94,7 +94,15 @@ class HtmlDescriptor(XmlDescriptor, EditingDescriptor):
msg = "Couldn't parse html in {0}.".format(filepath)
log.warning(msg)
system.error_tracker("Warning: " + msg)
return {'data' : html}
definition = {'data' : html}
# TODO (ichuang): remove this after migration
# for Fall 2012 LMS migration: keep filename
definition['filename'] = filepath
return definition
except (ResourceNotFoundError) as err:
msg = 'Unable to load file contents at path {0}: {1} '.format(
filepath, err)
......
......@@ -9,7 +9,7 @@ from fs.errors import ResourceNotFoundError
import os
import sys
log = logging.getLogger(__name__)
log = logging.getLogger('mitx.' + __name__)
_AttrMapBase = namedtuple('_AttrMap', 'metadata_key to_metadata from_metadata')
......@@ -110,6 +110,7 @@ class XmlDescriptor(XModuleDescriptor):
filename = xml_object.get('filename')
if filename is None:
definition_xml = copy.deepcopy(xml_object)
filepath = ''
else:
filepath = cls._format_filepath(xml_object.tag, filename)
......@@ -137,7 +138,13 @@ class XmlDescriptor(XModuleDescriptor):
raise Exception, msg, sys.exc_info()[2]
cls.clean_metadata_from_xml(definition_xml)
return cls.definition_from_xml(definition_xml, system)
definition = cls.definition_from_xml(definition_xml, system)
# TODO (ichuang): remove this after migration
# for Fall 2012 LMS migration: keep filename
definition['filename'] = filepath
return definition
@classmethod
......
......@@ -117,7 +117,13 @@ def get_course_info_section(course, section_key):
raise KeyError("Invalid about key " + str(section_key))
def course_staff_group_name(course):
coursename = course.metadata.get('course','')
'''
course should be either a CourseDescriptor instance, or a string (the .course entry of a Location)
'''
if type(course)==str:
coursename = course
else:
coursename = course.metadata.get('course','')
if not coursename: # Fall 2012: not all course.xml have metadata correct yet
coursename = course.metadata.get('data_dir','UnknownCourseName')
return 'staff_%s' % coursename
......
......@@ -15,6 +15,8 @@ from xmodule.exceptions import NotFoundError
from xmodule.x_module import ModuleSystem
from xmodule_modifiers import replace_static_urls, add_histogram, wrap_xmodule
from courseware.courses import has_staff_access_to_course
log = logging.getLogger("mitx.courseware")
......@@ -188,7 +190,8 @@ def get_module(user, request, location, student_module_cache, position=None):
module.metadata['data_dir']
)
if settings.MITX_FEATURES.get('DISPLAY_HISTOGRAMS_TO_STAFF') and user.is_staff:
if (settings.MITX_FEATURES.get('DISPLAY_HISTOGRAMS_TO_STAFF') and
(user.is_staff or has_staff_access_to_course(user, module.location.course))):
module.get_html = add_histogram(module.get_html, module)
# If StudentModule for this instance wasn't already in the database,
......
${module_content}
<div class="staff_info">
definition = ${definition | h}
metadata = ${metadata | h}
</div>
%if edit_link:
<div><a href="${edit_link}">Edit</a></div>
% endif
<div class="staff_info">
definition = <pre>${definition | h}</pre>
metadata = ${metadata | h}
</div>
%if render_histogram:
<div id="histogram_${element_id}" class="histogram" data-histogram="${histogram}"></div>
%endif
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