Commit d410d829 by Piotr Mitros

All elements in course file include IDs

parent 71416732
...@@ -7,6 +7,7 @@ except: ...@@ -7,6 +7,7 @@ except:
from lxml import etree from lxml import etree
import json import json
import hashlib
''' This file will eventually form an abstraction layer between the ''' This file will eventually form an abstraction layer between the
course XML file and the rest of the system. course XML file and the rest of the system.
...@@ -14,6 +15,11 @@ course XML file and the rest of the system. ...@@ -14,6 +15,11 @@ course XML file and the rest of the system.
TODO: Shift everything from xml.dom.minidom to XPath (or XQuery) TODO: Shift everything from xml.dom.minidom to XPath (or XQuery)
''' '''
def fasthash(string):
m = hashlib.new("md4")
m.update(string)
return m.hexdigest()
def xpath(xml, query_string, **args): def xpath(xml, query_string, **args):
''' Safe xpath query into an xml tree: ''' Safe xpath query into an xml tree:
* xml is the tree. * xml is the tree.
...@@ -55,10 +61,32 @@ def item(l, default="", process=lambda x:x): ...@@ -55,10 +61,32 @@ def item(l, default="", process=lambda x:x):
return process(l[0]) return process(l[0])
else: else:
raise Exception('Malformed XML') raise Exception('Malformed XML')
def id_tag(course):
''' Tag all course elements with unique IDs '''
default_ids = {'video':'youtube',
'problem':'filename',
'sequential':'id',
'html':'filename',
'vertical':'id',
'tab':'id',
'schematic':'id'}
# Tag elements with unique IDs
elements = course.xpath("|".join(['//'+c for c in default_ids]))
for elem in elements:
if elem.get('id'):
pass
elif elem.get(default_ids[elem.tag]):
elem.set('id', elem.get(default_ids[elem.tag]))
else:
elem.set('id', fasthash(etree.tostring(elem)))
def course_file(user): def course_file(user):
# TODO: Cache. # TODO: Cache.
return etree.parse(settings.DATA_DIR+UserProfile.objects.get(user=user).courseware) tree = etree.parse(settings.DATA_DIR+UserProfile.objects.get(user=user).courseware)
id_tag(tree)
return tree
def module_xml(coursefile, module, id_tag, module_id): def module_xml(coursefile, module, id_tag, module_id):
''' Get XML for a module based on module and module_id. Assumes ''' Get XML for a module based on module and module_id. Assumes
......
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