示例#1
0
    def convert_object(self, obj):
        """ Core of serialization.
            Convert an object into a dictionary of serialized field values.
        """
        ret = self._dict_class()
        ret.fields = {}

        fields = self.get_fields(nested=bool(self.opts.depth))
        for field_name, field in fields.items():
            key = self.get_field_key(field_name)
            value = field.field_to_native(obj, field_name)
            ret[key] = value
            ret.fields[key] = field

        if ret['user_id']:
            ret['id'] = obj.pk
        else:
            ret['id'] = None 
            # vpr: custom settings for categories field
            cids = models.restoreAssignedCategory(ret.get('categories', ''))
            cids = [str(cid) for cid in cids]
            ret['categories'] = ','.join(cids)

            # vpr: custom process for material author, editor
            material_roles = models.getMaterialPersons(obj.pk)
            for person_role in material_roles:
                ret[person_role] = material_roles[person_role] 

        return ret
示例#2
0
 def updateNode(node):
     if node['type'] == 'module':
         node['url'] = 'http://www.voer.edu.vn/m/' + node['id']
         node['license'] = "http://creativecommons.org/licenses/by/3.0/"
         # prepare authors 
         mrid = models.getMaterialRawID(node['id'], node['version'])
         persons = models.getMaterialPersons(mrid)
         author_ids = persons['author'].split(',')
         authors = models.getPersonName(author_ids)
         if isinstance(authors, str): authors = [authors,]
         node['authors'] = authors
     elif node['type'] == 'subcollection':
         neo_node = []
         for sub_node in node['content']:
             neo_node.append(updateNode(sub_node))
         node['content'] = neo_node
     return node
示例#3
0
def createMaterialDirectory(dir_path, material):
    """ Create directory for material which includes all material files,
        HTML content, and manifest file
    """
    mid = material.material_id
    version = material.version

    createDirectory(dir_path)

    # add material text content
    raw_content = material.text
    try:
        raw_content = raw_content.encode("utf-8")
    except:
        raw_content = raw_content.decode("utf-8").encode("utf-8")
    writeFileToDir(dir_path, ZIP_HTML_FILE, raw_content)

    # read all material files, and put into the zip package
    mfids = listMaterialFiles(mid, version)
    for mfid in mfids:
        try:
            mf = MaterialFile.objects.get(id=mfid)
            writeFileToDir(dir_path, mf.name, mf.mfile.read())
            mf.mfile.close()
        except:
            print "Error getting material file %s" % mf.name

    # generate material json
    persons = models.getMaterialPersons(material.id)
    try:
        author_ids = persons["author"].split(",")
    except:
        author_ids = []
    author_names = models.getPersonName(author_ids)
    index_content = {
        "title": material.title,
        "url": MOD_SOURCE_URL % material.material_id,
        "authors": author_names,
        "version": material.version,
        "language": material.language,
    }
    index_content = json.dumps(index_content)
    writeFileToDir(dir_path, ZIP_INDEX_MODULE, index_content)
示例#4
0
def zipMaterialInternal(material):
    """ Collects all material info and put it into a ZIP file.
        Full path of the zip file will be returned to the caller.
    """
    mid = material.material_id
    version = material.version
    mtype = material.material_type

    # init the zip package
    zip_path = os.path.join(settings.TEMP_DIR, ZIP_FILE_NAME % (str(mid), str(version)))
    zf = ZipFile(zip_path, "w", ZIP_DEFLATED)

    # check if module or collection
    if mtype == MTYPE_MODULE:
        # read all material files, and put into the zip package
        mfids = listMaterialFiles(mid, version)
        for mfid in mfids:
            try:
                mf = MaterialFile.objects.get(id=mfid)
                zf.writestr(mf.name, mf.mfile.read())
                mf.mfile.close()
            except:
                print "Error when getting material file %s" % mf.name

        # add material text content
        raw_content = material.text
        try:
            raw_content = raw_content.encode("utf-8")
        except:
            raw_content = raw_content.decode("utf-8").encode("utf-8")
        zf.writestr(ZIP_HTML_FILE, raw_content)

        # generate material json
        persons = models.getMaterialPersons(material.id)
        try:
            author_ids = persons["author"].split(",")
        except:
            author_ids = []
        author_names = models.getPersonName(author_ids)
        index_content = {
            "title": material.title,
            "url": MOD_SOURCE_URL % material.material_id,
            "authors": author_names,
            "version": material.version,
            "language": material.language,
        }
        index_content = json.dumps(index_content)
        zf.writestr(ZIP_INDEX_MODULE, index_content)

    elif mtype == MTYPE_COLLECTION:

        # 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]
            m_title = all_materials[cid][2]
            if m_version is None:
                m_version = getMaterialLatestVersion(m_id)
            mfids = listMaterialFiles(m_id, m_version)
            m_object = Material.objects.get(material_id=m_id)
            for mfid in mfids:
                try:
                    mf = MaterialFile.objects.get(id=mfid)
                    zf.writestr(m_id + "/" + mf.name, mf.mfile.read())
                    mf.mfile.close()
                except:
                    print "Error when reading material file: " + mf.name
            zf.writestr(m_id + "/" + ZIP_HTML_FILE, m_object.text)

        # prepare some fields
        editor_ids = models.getMaterialPersons(material.id)["editor"]
        editor_ids = editor_ids.split(",")
        editors = models.getPersonName(editor_ids)
        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"] = str(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 :]
        zf.writestr(ZIP_INDEX_COLLECTION, index_content)

    zf.close()
    return realpath(zf.filename)
示例#5
0
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)