def __init__(self, outputDir, package, pages, scormType): """ Initialize 'outputDir' is the directory that we read the html from and also output the mainfest.xml """ self.outputDir = outputDir self.package = package self.idGenerator = UniqueIdGenerator() self.pages = pages self.itemStr = "" self.resStr = "" self.scorm_type = scormType self.dependencies = {}
class Manifest(object): """ Represents an imsmanifest xml file """ def __init__(self, outputDir, package, pages, scormType): """ Initialize 'outputDir' is the directory that we read the html from and also output the mainfest.xml """ self.outputDir = outputDir self.package = package self.idGenerator = UniqueIdGenerator() self.pages = pages self.itemStr = "" self.resStr = "" self.scorm_type = scormType self.dependencies = {} def createMetaData(self): """ if user did not supply metadata title, description or creator then use package title, description, or creator in imslrm if they did not supply a package title, use the package name if they did not supply a date, use today """ if self.scorm_type == SCORM12: template_name = 'imslrm.xml' elif self.scorm_type == COMMONCARTRIDGE: template_name = 'cc.xml' else: raise AttributeError("Can't create metadata for %s" \ % self.scorm_type) static_dir = Path(settings.STATIC_ROOT) templateFilename = static_dir / 'templates' / template_name template = open(templateFilename, 'r', encoding='utf-8').read() lrm = model_to_dict(self.package.dublincore) if lrm.get('title', '') == '': lrm['title'] = self.package.title if lrm['title'] == '': lrm['title'] = self.package.name if lrm.get('description', '') == '': lrm['description'] = self.package.description if lrm['description'] == '': lrm['description'] = self.package.title if lrm.get('creator', '') == '': lrm['creator'] = self.package.author if lrm['date'] == '': lrm['date'] = time.strftime('%Y-%m-%d') # if they don't look like VCARD entries, coerce to fn: for f in ('creator', 'publisher', 'contributors'): if re.match('.*[:;]', lrm[f]) == None: lrm[f] = 'FN:' + lrm[f] xml = template % lrm return xml def save(self, filename): """ Save a imsmanifest file to self.outputDir """ out = open(self.outputDir / filename, "w", encoding='utf-8') if filename == "imsmanifest.xml": out.write(self.createXML()) out.close() if self.scorm_type == SCORM12: xml = self.createMetaData() out = open(self.outputDir / 'imslrm.xml', 'wb') out.write(xml.encode('utf8')) out.close() def createXML(self): manifestId = str(self.idGenerator.generate()) orgId = str(self.idGenerator.generate()) if self.scorm_type == COMMONCARTRIDGE: # FIXME flatten hierarchy for page in self.pages: self.genItemResStr(page) self.itemStr += "</item>\n" else: depth = 0 for page in self.pages: while depth >= page.depth: self.itemStr += "</item>\n" depth -= 1 self.genItemResStr(page) depth = page.depth while depth >= 1: self.itemStr += "</item>\n" depth -= 1 return render_to_string("exe/export/scorm_manifest.html", {"SCORM12": SCORM12, "SCORM2004": SCORM2004, "COMMONCARTRIDGE": COMMONCARTRIDGE, "manifestId": manifestId, "orgId": orgId, "manifest": self}) def genItemResStr(self, page): """ Returning xml string for items and resources """ itemId = "ITEM-" + str(self.idGenerator.generate()) resId = "RES-" + str(self.idGenerator.generate()) filename = page.name + ".html" self.itemStr += '<item identifier="' + itemId + '" ' if self.scorm_type != COMMONCARTRIDGE: self.itemStr += 'isvisible="true" ' self.itemStr += 'identifierref="' + resId + '">\n' self.itemStr += " <title>" self.itemStr += page.node.titleShort self.itemStr += "</title>\n" self.resStr += " <resource identifier=\"" + resId + "\" " self.resStr += "type=\"webcontent\" " # FIXME force dependency on popup_bg.gif on every page # because it isn't a "resource" so we can't tell which # pages will use it from content.css if self.scorm_type == COMMONCARTRIDGE: self.resStr += """href="%s"> <file href="%s"/> <file href="base.css"/> <file href="content.css"/> <file href="popup_bg.gif"/>""" % (filename, filename) if page.node.package.backgroundImg: self.resStr += '\n <file href="%s"/>' % \ page.node.package.backgroundImg.basename() self.dependencies["../base.css"] = True self.dependencies["content.css"] = True self.dependencies["popup_bg.gif"] = True else: self.resStr += "adlcp:scormtype=\"sco\" " self.resStr += "href=\"" + filename + "\"> \n" self.resStr += """\ <file href="%s"/> <file href="base.css"/> <file href="content.css"/> <file href="popup_bg.gif"/> <file href="APIWrapper.js"/> <file href="SCOFunctions.js"/>""" % filename self.resStr += "\n" fileStr = "" for resource in page.node.resources: fileStr += " <file href=\"" + os.path.basename( resource) + "\"/>\n" self.dependencies[resource] = True self.resStr += fileStr self.resStr += " </resource>\n"