Ejemplo n.º 1
0
    def post(self, request):
        """
        发布一条内容, 针对个人或者班级.

        ``POST`` `tiles/create/ <http://192.168.1.222:8080/v1/tiles/types>`_

        :param type_id:
            瓦片类型

        :param uid:
            发布者,默认为匿名用户(uid: -1)

        :param class_id:
            瓦片所属班级,是否属于班级的内容

        :param content:
            内容描述

        :param img:
            二进制图片信息.
        """
        params = request.POST
        type_id = params.get("type_id")
        uid = params.get("uid", -1)
        class_id = params.get("class_id")
        content = params.get("content", "")
        category_id = params.get("category_id",0)
        img = request.FILES.get('img')
        video = params.get("video", "")
        title = params.get("title", "")
        tag = params.get("tag", "")

        category_id = int(category_id)

        try:
            tile_type = NewTileType.objects.get(pk=type_id)
            if not title:
                title = tile_type.name
        except NewTileType.DoesNotExist:
            pass
            #return rc.NOT_HERE

        try:
            tile_category = NewTileCategory.objects.all_with_deleted().get(pk=category_id)
            if not title:
                title = tile_category.name
        except NewTileCategory.DoesNotExist:
            return rc.not_here("tile_category object is not exist")
            #return rc.NOT_HERE

        try:
            user = User.objects.get(pk=uid)
        except User.DoesNotExist:
            return rc.not_here("user object  is not exist")
            #return rc.NOT_HERE

        try:
            group = Group.objects.get(pk=class_id) if class_id else None
        except Group.DoesNotExist:
            group = None

        tile = Tile(creator=request.user, user=user, group=group)
        tile.title = title
        tile.new_type_id = type_id
        tile.description = content
        tile.img = img
        tile.video = video

        tile.new_category_id = category_id
        try:
            is_exist = Tile.objects.get(creator=request.user, user=user, group=group,\
                title = title,description = content,img = img,video = video,new_category_id = category_id)
            return None
        except:
            tile.save()
            if tag and tile.id:
                tile_tag = TileCreateTag()
                tile_tag.tag = tag
                tile_tag.tile = tile
                tile_tag.save()
            
        return tile if tile.id else None
Ejemplo n.º 2
0
    def post(self, request):
        """
        发布一条内容, 针对个人或者班级.

        ``POST`` `tiles/create/ <http://192.168.1.222:8080/v1/tiles/types>`_

        :param type_id:
            瓦片类型

        :param uid:
            发布者,默认为匿名用户(uid: -1)

        :param class_id:
            瓦片所属班级,是否属于班级的内容

        :param content:
            内容描述

        :param img:
            二进制图片信息.
        """
        params = request.POST
        type_id = params.get("type_id")
        uid = params.get("uid", -1)
        class_id = params.get("class_id")
        content = params.get("content", "")
        category_id = params.get("category_id",type_id)
        img = request.FILES.get('img')
        video = params.get("video", "")
        title = params.get("title", "")
        tag = params.get("tag", "")
        
        group = None
        category_id = int(category_id)
        if category_id in (1,2,3):
            category_id = 17
        
        from oa.helpers import user_access_list
        user = request.user
        code_list = [c.code for c in user_access_list(user)]
        school_level = True if "manage_school_tile" in code_list else False

        try:
            tile_type = TileType.objects.get(pk=type_id)
            if not title:
                title = tile_type.name
        except TileType.DoesNotExist:
            pass
            #return rc.NOT_HERE

        try:
            tile_category = TileCategory.objects.all_with_deleted().get(pk=category_id)
            if not title:
                title = tile_category.name
        except TileCategory.DoesNotExist:
            return rc.not_here("tile_category object is not exist")
            #return rc.NOT_HERE
        
        group_id = 0
        if school_level:
            user = None
            try:
                group = Group.objects.get(pk=uid) if uid else None
                group_id = group.id
            except Group.DoesNotExist:
                group = None
        else:
            try:
                user = User.objects.get(pk=uid)
            except User.DoesNotExist:
                return rc.not_here("user object  is not exist")
                #return rc.NOT_HERE
        if class_id and group_id == 0:
            try:
                group = Group.objects.get(pk=class_id) if class_id else None
            except Group.DoesNotExist:
                group = None
        tile = Tile(creator=request.user, user=user, group=group)

        tile.title = title

        tile.type_id = type_id
        if category_id == 9:
            #if not group:
                #return rc.not_here("group object is not exist for Activity")
            try:
                desc = json.loads(content)
                act = desc['events']
            except:
                return rc.not_here("Activity description object must be json include key events")
            if not act:
                desc = ''
            else:
                i = 0
                for d in act:
                    if not d['content']:
                       i += 1 
                if i == len(act):
                    desc = ''
            if not desc:
                return rc.not_here("Activity description object can not be null")
            active = Activity()
            active.user = user
            active.creator = request.user
            active.group = group
            active.description = json.dumps({"events":desc['events']})
            active.save()
    
        tile.description = content
#        tile.img = img
        tile.video = video



        # try: 
        #     assert category_id != None
        #     tc = TileCategory.objects.get(pk=category_id)
        #     assert not tc.is_parent
        # except Exception, e:
        #     print e
        #     return rc.BAD_REQUEST

        tile.category_id = category_id
        try:
            is_exist = Tile.objects.get(creator=request.user, user=user, group=group,\
                title = title,description = content,img = img,video = video,category_id = category_id)
            return None
        except:
            tile.save()
            if tag and tile.id:
                tile_tag = TileCreateTag()
                tile_tag.tag = tag
                tile_tag.tile = tile
                tile_tag.save()
        if tile.id and img:
            try:
                date = str(datetime.datetime.strftime(datetime.datetime.now(),"%Y%m%d"))
                salt, hash = generate_sha1(tile.id)
                extension = str(img).split('.')[-1].lower()
                file_name = 'tile/' + date + '/' + hash[:22] + '.' + extension
                AliyunStorage(). _put_file(file_name, img.read())
                tile.img = file_name
                tile.save()
            except:
                pass
        return tile if tile.id else None