show news & options when submit comment
This commit is contained in:
93
lms/djangoapps/django_comment_client/utils.py
Normal file
93
lms/djangoapps/django_comment_client/utils.py
Normal file
@@ -0,0 +1,93 @@
|
||||
from importlib import import_module
|
||||
from courseware.models import StudentModuleCache
|
||||
from courseware.module_render import get_module
|
||||
from xmodule.modulestore import Location
|
||||
from xmodule.modulestore.django import modulestore
|
||||
|
||||
from django.conf import settings
|
||||
import operator
|
||||
import itertools
|
||||
|
||||
_FULLMODULES = None
|
||||
_DISCUSSIONINFO = None
|
||||
|
||||
def get_full_modules():
|
||||
global _FULLMODULES
|
||||
if not _FULLMODULES:
|
||||
class_path = settings.MODULESTORE['default']['ENGINE']
|
||||
module_path, _, class_name = class_path.rpartition('.')
|
||||
class_ = getattr(import_module(module_path), class_name)
|
||||
modulestore = class_(eager=True, **settings.MODULESTORE['default']['OPTIONS'])
|
||||
_FULLMODULES = modulestore.modules
|
||||
return _FULLMODULES
|
||||
|
||||
def get_categorized_discussion_info(request, course):
|
||||
"""
|
||||
return a dict of the form {category: modules}
|
||||
"""
|
||||
global _DISCUSSIONINFO
|
||||
if not _DISCUSSIONINFO:
|
||||
initialize_discussion_info(request, course)
|
||||
return _DISCUSSIONINFO['categorized']
|
||||
|
||||
def get_discussion_title(request, course, discussion_id):
|
||||
global _DISCUSSIONINFO
|
||||
if not _DISCUSSIONINFO:
|
||||
initialize_discussion_info(request, course)
|
||||
title = _DISCUSSIONINFO['by_id'].get(discussion_id, {}).get('title', '(no title)')
|
||||
if title == '(no title)':
|
||||
print "title shouldn't be none"
|
||||
import pdb; pdb.set_trace()
|
||||
return title
|
||||
|
||||
def initialize_discussion_info(request, course):
|
||||
|
||||
global _DISCUSSIONINFO
|
||||
if _DISCUSSIONINFO:
|
||||
return
|
||||
|
||||
course_id = course.id
|
||||
_, course_name, _ = course_id.split('/')
|
||||
user = request.user
|
||||
url_course_id = course_id.replace('/', '_').replace('.', '_')
|
||||
|
||||
_is_course_discussion = lambda x: x[0].dict()['category'] == 'discussion' \
|
||||
and x[0].dict()['course'] == course_name
|
||||
|
||||
_get_module_descriptor = operator.itemgetter(1)
|
||||
|
||||
def _get_module(module_descriptor):
|
||||
print module_descriptor
|
||||
module = get_module(user, request, module_descriptor.location, student_module_cache)[0]
|
||||
return module
|
||||
|
||||
def _extract_info(module):
|
||||
return {
|
||||
'title': module.title,
|
||||
'discussion_id': module.discussion_id,
|
||||
'category': module.discussion_category,
|
||||
}
|
||||
|
||||
def _pack_with_id(info):
|
||||
return (info['discussion_id'], info)
|
||||
|
||||
discussion_module_descriptors = map(_get_module_descriptor,
|
||||
filter(_is_course_discussion,
|
||||
get_full_modules().items()))
|
||||
|
||||
student_module_cache = StudentModuleCache(user, course)
|
||||
|
||||
discussion_info = map(_extract_info, map(_get_module, discussion_module_descriptors))
|
||||
|
||||
_DISCUSSIONINFO = {}
|
||||
|
||||
_DISCUSSIONINFO['by_id'] = dict(map(_pack_with_id, discussion_info))
|
||||
|
||||
_DISCUSSIONINFO['categorized'] = dict((category, list(l)) \
|
||||
for category, l in itertools.groupby(discussion_info, operator.itemgetter('category')))
|
||||
|
||||
_DISCUSSIONINFO['categorized']['General'] = [{
|
||||
'title': 'General discussion',
|
||||
'discussion_id': url_course_id,
|
||||
'category': 'General',
|
||||
}]
|
||||
Reference in New Issue
Block a user