Exemple #1
0
def active_data_migration(request):
    """瓦片日常活动内容数据迁移"""
    if not request.user.is_staff:
        return render(request, "403.html")
    tiles = Tile.objects.filter(is_tips=0,category_id=9,group__isnull=False)
    i = 0
    for t in tiles:
        try:
            has_migration = TileToActivity.objects.get(tile=t)
        except:
            has_migration = None
            
        if not has_migration:
            active_description = get_active_description(t.description)
            if active_description:
                active = Activity()
                active.creator = t.creator
                active.user = t.user
                active.group_id = t.group_id
                active.description = json.dumps({"events":active_description})
                active.start_time = t.start_time
                active.microsecond = t.microsecond
                active.save()
                migration = TileToActivity()
                migration.tile = t
                migration.active = active
                migration.save()
                i += 1
    message = "actives_count:" + str(i)
    return HttpResponse(message)
Exemple #2
0
    def post(self, request):
        """
        发布一条内容, 针对个人或者班级.

        ``POST`` `actives/create/ <http://192.168.1.222:8080/api/v1/actives/create>`_

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

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

        :param content:
            内容描述

        """
        params = request.POST
        uid = params.get("uid", -1)
        class_id = params.get("class_id")
        content = params.get("content", "")
        
        try:
            user = User.objects.get(pk=uid)
        except User.DoesNotExist:
            return rc.not_here("user object  is not exist")

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

        try:
            desc = json.loads(content)
            act = desc['events']
        except:
            return rc.not_here("description object must be json has 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("description 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()
        
        return active if active.id else None
Exemple #3
0
    def modify(self, request):
        """
        修改一条内容, 针对个人或者班级.

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

        :param id:
            瓦片id
            
        :param type_id:
            瓦片类型

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

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

        :param content:
            内容描述

        :param img:
            二进制图片信息.
        """
        params = request.POST
        id = params.get("id")
        type_id = params.get("type_id","")
        uid = params.get("uid", "")
        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", "")
        print id,'======='
        try:
            tile = Tile.objects.get(pk=id)
        except Tile.DoesNotExist:
            return rc.not_here("tile object is not exist")

        category_id = int(category_id)
        if category_id in (1,2,3):
            category_id = 17

        if type_id:
            try:
                tile_type = TileType.objects.get(pk=type_id)
            except TileType.DoesNotExist:
                return rc.not_here("tile_type object is not exist")
            tile.type_id = type_id
            #return rc.NOT_HERE
        if category_id:
            try:
                tile_category = TileCategory.objects.all_with_deleted().get(pk=category_id)
            except TileCategory.DoesNotExist:
                return rc.not_here("tile_category object is not exist")
            tile.category_id = category_id
            
        if uid:
            try:
                user = User.objects.get(pk=uid)
            except User.DoesNotExist:
                return rc.not_here("user object  is not exist")
            tile.user = user

        if class_id:
            try:
                group = Group.objects.get(pk=class_id)
            except Group.DoesNotExist:
                return rc.not_here("group object  is not exist")
            tile.group = group
        
        if title:
            tile.title = title
        if content:
            tile.description = content
        if img:
            tile.img = img 
        if video:
            tile.video = video
        
        if tile.category_id == 9:
            #if not group:
                #return rc.not_here("group object is not exist for Activity")
            try:
                desc = json.loads(tile.description)
                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")
            try:
                has_migration = TileToActivity.objects.get(tile=tile)
            except:
                has_migration = None
            if has_migration:
                active = has_migration.active
            else:
                active = Activity()
                
            active.user = tile.user
            active.creator = tile.creator
            active.group = tile.group
            active.start_time = tile.start_time
            active.microsecond = tile.microsecond
            active.description = json.dumps({"events":desc['events']})
            active.save()   
            
            if not has_migration:
                migration = TileToActivity()
                migration.tile = tile
                migration.active = active
                migration.save()  
            
        tile.save()
        return tile
Exemple #4
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