def renderMaterialView(request, *args, **kwargs): """ Render a given material with simple UI. http://.../dashboard/render/<material_id>/<version> """ material = models.getMaterial(kwargs['mid'], kwargs.get('version', None)) data = { 'material': material, } return render(request, 'render.html', dictionary=data)
def getMaterialZip(request, *args, **kwargs): """ Return compressed files (Zip) of a material """ material = models.getMaterial(kwargs["mid"], kwargs.get("version", None)) zip_path = zipMaterial(material) try: with open(zip_path, "rb") as zf: zip_name = "%s-%d.zip" % (material.material_id, material.version) response = HttpResponse(zf.read(), mimetype="application/zip") response["content-disposition"] = "attachment; filename=" + zip_name return response except: raise Http404
def getMaterialZip(request, *args, **kwargs): """ Return compressed files (Zip) of a material """ material = models.getMaterial(kwargs['mid'], kwargs.get('version', None)) zip_path = zipMaterial(material) try: with open(zip_path, 'rb') as zf: zip_name = '%s-%d.zip' % (material.material_id, material.version) response = HttpResponse(zf.read(), mimetype = 'application/zip') response['content-disposition'] = 'attachment; filename='+zip_name return response except: raise Http404
def zipMaterialExternal(material): """ Collects all material info and put into a folder, then call external zip command to do its job. Full path of the zip file will be returned to the caller. """ mid = material.material_id version = material.version mtype = material.material_type # create material directory dir_path = os.path.join(settings.TEMP_DIR, "%s-%d" % (mid, version)) # check if module or collection if mtype == MTYPE_MODULE: createMaterialDirectory(dir_path, material) elif mtype == MTYPE_COLLECTION: createDirectory(dir_path) # get list of all contained materials all_materials = getNestedMaterials(material) # load materials into ZIP for cid in range(len(all_materials)): m_id = all_materials[cid][0] m_version = all_materials[cid][1] or models.getMaterialLatestVersion(m_id) m_object = models.getMaterial(m_id, m_version) m_path = os.path.join(dir_path, m_id) createMaterialDirectory(m_path, m_object) # prepare some fields try: editor_ids = models.getMaterialPersons(material.id)["editor"] editor_ids = editor_ids.split(",") editors = models.getPersonName(editor_ids) except KeyError: editors = [""] if isinstance(editors, str): editors = [editors] material_url = COL_SOURCE_URL % material.material_id # generate collection.json try: index_content = json.loads(material.text) index_content["id"] = material.material_id index_content["title"] = material.title index_content["version"] = material.version index_content["license"] = MATERIAL_LICENSE index_content["url"] = material_url index_content["editors"] = editors index_content["language"] = material.language index_content = json.dumps(index_content) except: # another way index_content = '{"id":"%s",' % material.material_id index_content += '"title":"%s",' % material.title index_content += '"version":"%s",' % str(material.version) index_content += '"license":"%s",' % MATERIAL_LICENSE index_content += '"url":"%s",' % material_url index_content += '"editors":"%s",' % editors index_content += '"language":"%s",' % material.language index_content += material.text[material.text.index("{") + 1 :] with open(os.path.join(dir_path, ZIP_INDEX_COLLECTION), "w") as mnf: mnf.write(index_content) # zip the material files cmd = "zip -r5 %s ./*" % buildZipPath(dir_path) process = Popen(cmd, shell=True, cwd=dir_path) try: process.wait() except TimeoutExpired: print "Timed-out when creating material ZIP file" finally: rmtree(dir_path) if process.poll() == 0: return buildZipPath(dir_path)