def upload(self, filepath): # extract zip files = self.extractZipFiles(filepath) # validate files xmltools = XMLTools() allValid = xmltools.validateFiles(files) if len(files) != 0 and allValid: REST = RESTClient() for filetype, content in files: if filetype == "cp": cp = content # get topic name for filename tree = etree.fromstring(cp) ns = {'ns': 'http://www.imsglobal.org/xsd/imsld_v1p0'} topic = tree.xpath('//ns:learning-design/ns:title', namespaces=ns)[0].text # write cp file to db r = REST.post("cp", data={'topic': topic}, payload=cp) print r if filetype == "qti": qti = content # get item id for filename tree = etree.fromstring(qti) id = tree.xpath("/*[local-name()]")[0].attrib['identifier'] id = id.split("ITM_")[1] fname = 'qti_' + id + '.xml' #write qti file to db r = REST.post("item", data={'fname': fname}, payload=qti) print r
def insertResources(self): # read learning content from json with open('../content/cp.json') as f: res = yaml.safe_load(f.read()) # load ims cp template and merge content into cp loader = airspeed.CachingFileLoader(self.templates.get("path")) t = loader.load_template(self.templates.get("cp")) cp = t.merge(res, loader=loader) # validate ims cp (incl. ld, md) xmltools = XMLTools() scheme_path = "../xsd/IMS_master.xsd" xmltools.validateStreamAgainstScheme(cp, scheme_path) # get topic name for filename topic = res.get("learning_design").get("title").lower() # write cp file to local file with open('../xml/cp_'+topic+'.xml', 'w+') as f: f.write(cp) # write cp file to db REST = RESTClient() r = REST.post("cp", data={'topic': topic}, payload=cp) print r # build and write qti items to db for la in res['learning_activities']: # load qti template t = loader.load_template(self.templates.get(la['content']['interaction'])) qti = t.merge(la, loader=loader) # write qti files to local files fname = 'qti_' + la['id'] + '.xml' with open('../xml/' + fname, 'w+') as f: f.write(qti) # write qti files to db r = REST.post("item", data={'fname': fname}, payload=qti) print r