コード例 #1
0
ファイル: serializers.py プロジェクト: NamPNQ/vp.repo
    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
ファイル: migrate_categories.py プロジェクト: NamPNQ/vp.repo
def importMaterialCats():
    cat_objs = Category.objects.all() 
    jsfile = open('exported-cats.json', 'r')
    cats = json.load(jsfile)
    for cat in cats:
        try:
            raw_cats = restoreAssignedCategory(cat[1])
            material = Material.objects.get(pk=cat[0])
            if isinstance(raw_cats, list) or isinstance(raw_cats, tuple):
                [material.categories.add(cat_objs[cid-1]) for cid in raw_cats]
            else:
                material.categories.add(cat_objs[raw_cats])
            print cat[0]
        except:
            import pdb;pdb.set_trace()
            raise