コード例 #1
0
ファイル: service.py プロジェクト: Aimsam/vd
def get_list(page, node, author):
    authorName = author
    cacheKey = "%s_author_%s_page_%s_node_%s" % (CACHE_KEY_VIDEO_LIST, author, page, node)
    data = cache.get(cacheKey)
    if data is None:
        print "from database" #@todo debug message
        if authorName != 'all':
            try:
                authorObject = Author.objects.get(id=author)
                authorName = authorObject.name
            except Author.DoesNotExist:
                return util.deleteUnicode("jsonp2(" + str({'code':201, 'message':'error author id', 'author':authorName}) + ")")
        if node == 'all':
            list = Video.objects.filter().order_by("-published")
        else:
            list = Video.objects.filter(node=node).order_by("-published")
        if author != 'all':
            list = list.filter(author=author)
        else:
            pass
        list = list.all()
        paginator = Paginator(list, PAGE_COUNT)
        try:
            list = paginator.page(page)
        except :
            return util.deleteUnicode("jsonp2(" + str({'code':202, 'message':'error page number', 'max_num' : paginator.num_pages
            })  + ")")
        data = {}
        data['count'] = (int)(paginator.num_pages)
        data['list'] = list.object_list
        cache.set(cacheKey, data, 60 * 5)#5min
    else:
        print "from cache" #@todo debug message
    list = data['list']
    videoList = []
    for video in list:
        increment = get_increment_byid(video.id)
        video_tmp = {'id' : video.id,
                     'author':video.author.id,
                     'user' : video.user.name,
                     #'tag':video.tags,
                     'title' : video.title,
                     'thumbnail' : video.thumbnail,
                     'thumbnail_2' : video.thumbnail_2,
                     'quality' : video.quality,
                     'duration' : video.duration,
                     'published' : video.published.strftime("%Y-%m-%d"),
                     'description' : video.description,
                     'remarks' : video.remarks,
                     'love' : int(video.love + increment['love']),
                     'click' : int(video.click + increment['click'])
        }

        videoList.append(video_tmp)
    data = {'code':200, 'message':'success', 'count' : data['count'], 'author':authorName, 'node': str(node), 'list':videoList}
    return util.deleteUnicode("jsonp2(" + str(data) + ")")
コード例 #2
0
ファイル: service.py プロジェクト: Aimsam/vd
def love(request, id):
    response =  HttpResponse("")
    if id is None or id == "":
        result = {"code":101, "message":"id is empty", "id":id}
        response.write(util.deleteUnicode("jsonp(" + str(result) + ")"))
        return response
    if "aaa333" in request.COOKIES:#@todo
        result = {"code":103, "message":"too fast", "id":id}
        response.write(util.deleteUnicode("jsonp(" + str(result) + ")"))
        return response
    else:
        response.set_cookie("aaa", "2", max_age = 60)
    cacheKey = "%s_love_click_id_%s" % (CACHE_KEY_VIDEO, id)
    data = cache.get(cacheKey)
    if data is None:
        try:
            data = Video.objects.get(id=id)
            data.love += 1
            data = {"love":data.love, "click":data.click}
            cache.set(cacheKey, data)
            keys = cache.get(CACHE_KEY_VIDEO_KEYS)
            if keys is None:
                keys = [id]
            else:
                keys.append(id)
            cache.set(CACHE_KEY_VIDEO_KEYS, keys)
        except Video.DoesNotExist:
            result = {"code":102, "message":"error id", "id":id}
            response.write(util.deleteUnicode("jsonp(" + str(result) + ")"))
            return response
    else:
        data['love']  += 1
        cache.set(cacheKey, data)
    result = {"code":100, "message":"success", "id":id, "love":data['love']}




    response.write(util.deleteUnicode("jsonp(" + str(result) + ")"))
    return response
コード例 #3
0
ファイル: service.py プロジェクト: Aimsam/vd
def get_author_list(_node):
    cacheKey = CACHE_KEY_AUTHOR_LIST + _node
    list = cache.get(cacheKey)
    if list is None:
        print "from database"#@todo debug message
        list = Author.objects.filter(node=_node).order_by("-weight").all()
        #if len(list) > 1:
        cache.set(cacheKey, list)
    else:
        print "from cache"
    authorList = []
    for author in list:
        tmp = {
            'id' : author.id,
            'name' : author.name,
            'node' : author.node.name,
            'description' : author.description,
            'avatar' : author.avatar,
            'love' : str(author.love),
        }
        authorList.append(tmp)
    return util.deleteUnicode("jsonp3(" + str({"code" : 300, "message" : "success", "list" : authorList}) + ")")