Commit 0cba4ffd by Calen Pennington Committed by Matthew Mongeau

Read week headings from mongodb

parent 5bad95cf
"""
This module provides an abstraction for working objects that conceptually have
the following attributes:
location: An identifier for an item, of which there might be many revisions
children: A list of urls for other items required to fully define this object
data: A set of nested data needed to define this object
editor: The editor/owner of the object
parents: Url pointers for objects that this object was derived from
revision: What revision of the item this is
"""
class Location(object):
''' Encodes a location.
Can be:
* String (url)
* Tuple
* Dictionary
'''
def __init__(self, location):
self.update(location)
def update(self, location):
if isinstance(location, basestring):
self.tag = location.split('/')[0][:-1]
(self.org, self.course, self.category, self.name) = location.split('/')[2:]
elif isinstance(location, list):
(self.tag, self.org, self.course, self.category, self.name) = location
elif isinstance(location, dict):
self.tag = location['tag']
self.org = location['org']
self.course = location['course']
self.category = location['category']
self.name = location['name']
elif isinstance(location, Location):
self.update(location.list())
def url(self):
return "{tag}://{org}/{course}/{category}/{name}".format(**self.dict())
def list(self):
return [self.tag, self.org, self.course, self.category, self.name]
def dict(self):
return {'tag': self.tag,
'org': self.org,
'course': self.course,
'category': self.category,
'name': self.name}
def to_json(self):
return self.dict()
class KeyStore(object):
def get_item(self, location):
"""
Returns an XModuleDescriptor instance for the item at location
If no object is found at that location, raises keystore.exceptions.ItemNotFoundError
Searches for all matches of a partially specifed location, but raises an
keystore.exceptions.InsufficientSpecificationError if more
than a single object matches the query.
location: Something that can be passed to Location
"""
raise NotImplementedError
def create_item(self, location, editor):
"""
Create an empty item at the specified location with the supplied editor
location: Something that can be passed to Location
"""
raise NotImplementedError
def update_item(self, location, data):
"""
Set the data in the item specified by the location to
data
location: Something that can be passed to Location
data: A nested dictionary of problem data
"""
raise NotImplementedError
def update_children(self, location, children):
"""
Set the children for the item specified by the location to
data
location: Something that can be passed to Location
children: A list of child item identifiers
"""
raise NotImplementedError
class KeyStoreItem(object):
"""
An object from a KeyStore, which can be saved back to that keystore
"""
def __init__(self, location, children, data, editor, parents, revision):
self.location = location
self.children = children
self.data = data
self.editor = editor
self.parents = parents
self.revision = revision
def save(self):
raise NotImplementedError
import pymongo
from . import KeyStore, Location
from .exceptions import ItemNotFoundError, InsufficientSpecificationError
from xmodule.x_module import XModuleDescriptor
class MongoKeyStore(KeyStore):
"""
A Mongodb backed KeyStore
"""
def __init__(self, host, db, collection, port=27017):
self.collection = pymongo.connection.Connection(
host=host,
port=port
)[db][collection]
# Force mongo to report errors, at the expense of performance
self.collection.safe = True
def get_item(self, location):
"""
Returns an XModuleDescriptor instance for the item at location
If no object is found at that location, raises keystore.exceptions.ItemNotFoundError
Searches for all matches of a partially specifed location, but raises an
keystore.exceptions.InsufficientSpecificationError if more
than a single object matches the query.
location: Something that can be passed to Location
"""
query = dict(
('location.{key}'.format(key=key), val)
for (key, val)
in Location(location).dict().items()
if val is not None
)
items = self.collection.find(
query,
sort=[('revision', pymongo.ASCENDING)],
limit=1,
)
if items.count() > 1:
raise InsufficientSpecificationError(location)
if items.count() == 0:
raise ItemNotFoundError(location)
return XModuleDescriptor.load_from_json(items[0], self.get_item)
def create_item(self, location, editor):
"""
Create an empty item at the specified location with the supplied editor
location: Something that can be passed to Location
"""
self.collection.insert({
'location': Location(location).dict(),
'editor': editor
})
def update_item(self, location, data):
"""
Set the data in the item specified by the location to
data
location: Something that can be passed to Location
data: A nested dictionary of problem data
"""
self.collection.update(
{'location': Location(location).dict()},
{'$set': {'data': data}}
)
def update_children(self, location, children):
"""
Set the children for the item specified by the location to
data
location: Something that can be passed to Location
children: A list of child item identifiers
"""
self.collection.update(
{'location': Location(location).dict()},
{'$set': {'children': children}}
)
import json
from lxml import etree
from x_module import XModule, XModuleDescriptor
# HACK: This shouldn't be hard-coded to two types
# OBSOLETE: This obsoletes 'type'
class_priority = ['video', 'problem']
class ModuleDescriptor(XModuleDescriptor):
pass
class Module(XModule):
''' Layout module which lays out content in a temporal sequence
'''
id_attribute = 'id'
def get_state(self):
return json.dumps({ 'position':self.position })
@classmethod
def get_xml_tags(c):
obsolete_tags = ["sequential", 'tab']
modern_tags = ["videosequence"]
return obsolete_tags + modern_tags
def get_html(self):
self.render()
return self.content
def get_init_js(self):
self.render()
return self.init_js
def get_destroy_js(self):
self.render()
return self.destroy_js
def handle_ajax(self, dispatch, get): # TODO: bounds checking
''' get = request.POST instance '''
if dispatch=='goto_position':
self.position = int(get['position'])
return json.dumps({'success':True})
raise self.system.exception404
def render(self):
if self.rendered:
return
## Returns a set of all types of all sub-children
child_classes = [set([i.tag for i in e.iter()]) for e in self.xmltree]
titles = ["\n".join([i.get("name").strip() for i in e.iter() if i.get("name") is not None]) \
for e in self.xmltree]
self.contents = self.rendered_children()
for contents, title in zip(self.contents, titles):
contents['title'] = title
for (content, element_class) in zip(self.contents, child_classes):
new_class = 'other'
for c in class_priority:
if c in element_class:
new_class = c
content['type'] = new_class
# Split </script> tags -- browsers handle this as end
# of script, even if it occurs mid-string. Do this after json.dumps()ing
# so that we can be sure of the quotations being used
params={'items':json.dumps(self.contents).replace('</script>', '<"+"/script>'),
'id':self.item_id,
'position': self.position,
'titles':titles,
'tag':self.xmltree.tag}
if self.xmltree.tag in ['sequential', 'videosequence']:
self.content = self.system.render_template('seq_module.html', params)
if self.xmltree.tag == 'tab':
self.content = self.system.render_template('tab_module.html', params)
self.rendered = True
def __init__(self, system, xml, item_id, state=None):
XModule.__init__(self, system, xml, item_id, state)
self.xmltree = etree.fromstring(xml)
self.position = 1
if state is not None:
state = json.loads(state)
if 'position' in state: self.position = int(state['position'])
# if position is specified in system, then use that instead
if system.get('position'):
self.position = int(system.get('position'))
self.rendered = False
class CourseModuleDescriptor(XModuleDescriptor):
pass
class ChapterModuleDescriptor(XModuleDescriptor):
pass
from lxml import etree
import pkg_resources
import logging
from keystore import Location
log = logging.getLogger('mitx.' + __name__)
def dummy_track(event_type, event):
pass
class ModuleMissingError(Exception):
pass
class Plugin(object):
@classmethod
def load_class(cls, identifier):
classes = list(pkg_resources.iter_entry_points(cls.entry_point, name=identifier))
if len(classes) > 1:
log.warning("Found multiple classes for {entry_point} with identifier {id}: {classes}. Returning the first one.".format(
entry_point=cls.entry_point,
id=identifier,
classes=", ".join([class_.module_name for class_ in classes])))
if len(classes) == 0:
raise ModuleMissingError(identifier)
return classes[0].load()
class XModule(object):
''' Implements a generic learning module.
Initialized on access with __init__, first time with state=None, and
then with state
See the HTML module for a simple example
'''
id_attribute='id' # An attribute guaranteed to be unique
@classmethod
def get_xml_tags(c):
''' Tags in the courseware file guaranteed to correspond to the module '''
return []
@classmethod
def get_usage_tags(c):
''' We should convert to a real module system
For now, this tells us whether we use this as an xmodule, a CAPA response type
or a CAPA input type '''
return ['xmodule']
def get_name(self):
name = self.__xmltree.get('name')
if name:
return name
else:
raise "We should iterate through children and find a default name"
def rendered_children(self):
'''
Render all children.
This really ought to return a list of xmodules, instead of dictionaries
'''
children = [self.render_function(e) for e in self.__xmltree]
return children
def __init__(self, system = None, xml = None, item_id = None,
json = None, track_url=None, state=None):
''' In most cases, you must pass state or xml'''
if not item_id:
raise ValueError("Missing Index")
if not xml and not json:
raise ValueError("xml or json required")
if not system:
raise ValueError("System context required")
self.xml = xml
self.json = json
self.item_id = item_id
self.state = state
self.DEBUG = False
self.__xmltree = etree.fromstring(xml) # PRIVATE
if system:
## These are temporary; we really should go
## through self.system.
self.ajax_url = system.ajax_url
self.tracker = system.track_function
self.filestore = system.filestore
self.render_function = system.render_function
self.DEBUG = system.DEBUG
self.system = system
### Functions used in the LMS
def get_state(self):
''' State of the object, as stored in the database
'''
return ""
def get_score(self):
''' Score the student received on the problem.
'''
return None
def max_score(self):
''' Maximum score. Two notes:
* This is generic; in abstract, a problem could be 3/5 points on one randomization, and 5/7 on another
* In practice, this is a Very Bad Idea, and (a) will break some code in place (although that code
should get fixed), and (b) break some analytics we plan to put in place.
'''
return None
def get_html(self):
''' HTML, as shown in the browser. This is the only method that must be implemented
'''
return "Unimplemented"
def handle_ajax(self, dispatch, get):
''' dispatch is last part of the URL.
get is a dictionary-like object '''
return ""
class XModuleDescriptor(Plugin):
entry_point = "xmodule.v1"
@staticmethod
def load_from_json(json_data, load_item):
class_ = XModuleDescriptor.load_class(json_data['location']['category'])
return class_.from_json(json_data, load_item)
@classmethod
def from_json(cls, json_data, load_item):
"""
Creates an instance of this descriptor from the supplied json_data.
json_data: Json data specifying the data, children, and metadata for the descriptor
load_item: A function that takes an i4x url and returns a module descriptor
"""
return cls(load_item=load_item, **json_data)
def __init__(self,
load_item,
data=None,
children=None,
**kwargs):
self.load_item = load_item
self.data = data if data is not None else {}
self.children = children if children is not None else []
self.name = Location(kwargs.get('location')).name
self._child_instances = None
def get_children(self):
"""Returns a list of XModuleDescriptor instances for the children of this module"""
if self._child_instances is None:
self._child_instances = [self.load_item(child) for child in self.children]
return self._child_instances
def get_xml(self):
''' For conversions between JSON and legacy XML representations.
'''
if self.xml:
return self.xml
else:
raise NotImplementedError("JSON->XML Translation not implemented")
def get_json(self):
''' For conversions between JSON and legacy XML representations.
'''
if self.json:
raise NotImplementedError
return self.json # TODO: Return context as well -- files, etc.
else:
raise NotImplementedError("XML->JSON Translation not implemented")
#def handle_cms_json(self):
# raise NotImplementedError
#def render(self, size):
# ''' Size: [thumbnail, small, full]
# Small ==> what we drag around
# Full ==> what we edit
# '''
# raise NotImplementedError
...@@ -28,3 +28,4 @@ pymongo ...@@ -28,3 +28,4 @@ pymongo
django_nose django_nose
nosexcover nosexcover
rednose rednose
-e common/lib/xmodule
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