Пример #1
0
def commentSubmit(request):

    comment = request.POST.get("comment")
    entity_type = request.POST.get("entity_type")
    entity_id = request.POST.get("entity_id")
    user_id = request.user.id

    com = Comment.objects.create()
    com.content = comment
    com.user_id = user_id
    com.entity_type = entity_type
    com.entity_id = entity_id
    com.save()

    # 找到评论最初属主ID及type
    # 最初属主的type 可以为"2" 及评论的最初属主不可为评论
    ParentEntityId = entity_id
    ParentEntityType = entity_type
    while ParentEntityType == "2":
        ParentEntityId = Comment.objects.get(id=ParentEntityId).entity_id
        ParentEntityType = Comment.objects.get(id=ParentEntityId).entity_type

    # 如果最初属主是问题,则给该评论属主问题的sumAscores加1
    if ParentEntityType == "1":
        field = "Qanswer"
        rs = RankService()
        Qanswer = rs.IncrbyFactor(rankType["question"].encode(),
                                  str(ParentEntityId), field)

    return HttpResponse(content="yes")
Пример #2
0
def publicS(request):
    # import pdb; pdb.set_trace()

    try:
        # 敏感词过滤
        Sensitive = Sservice()
        # import pdb; pdb.set_trace()
        title = Sensitive.filter(request.POST.get('title'))
        content = Sensitive.filter(request.POST.get('content'))
        # title = request.POST.get('title')
        # content = request.POST.get("content")
        userId = request.user.id
        # import pdb; pdb.set_trace()
        question = Question.objects.create(title=title,
                                           content=content,
                                           user_id=userId)

        qId = question.id
        # import pdb; pdb.set_trace()

        # 问题发布成功设置其初始因素值
        try:
            rs = RankService()
            rs.setInfluenceFactors(rankType['question'].encode(), str(qId),
                                   QuestionFactors)
            rs.CalculateAndUpdate(rankType['question'].encode(), str(qId))
            return True, "success"
        except Exception as e:
            print e
            return False, e

    except Exception as e:
        return False, e
Пример #3
0
def unfollowS(request):
    entity_type = request.POST.get("entity_type")
    entity_id = request.POST.get("entity_id")
    userId = str(request.user.id)
    # import pdb; pdb.set_trace()
    if entity_type is None or entity_id is None:
        return HttpResponse("wrong request \n type or id is None")
    else:
        fs = Fs()
        fs.unfollow(entity_type, entity_id, userId)

        # 如果取消关注的是问题,则给该问题因素Qfollowers减1
        if entity_type == "1":
            # print "关注的是问题"
            field = "Qfollowers"
            rs = RankService()
            Qfollowers = rs.IncrbyFactor(rankType["question"].encode(),
                                         str(entity_id), field, -1)
        return HttpResponse(content="d1")
Пример #4
0
    "Qscore": '14',
    "sumAscores": '3',
    "QageInHours": '80',
    "Qupdated": '50',
}
factors4 = {
    "Qviews": '10',
    "Qanswer": "2",
    "Qscore": '2',
    "sumAscores": '3',
    "QageInHours": '80',
    "Qupdated": '50',
}

rankType = "TRank"
rs = RankService()
print rs.IncrbyFactor(rankType, "qId10", "Qviews")
print rs.getRankResult(rankType)
#
# rs.setRankScore(rankType,11,"qId11")
# rs.setRankScore(rankType,12,"qId12")

# # print "所有Rank Id:",rs.getRankResult(rankType)
# # print "offset 0 count 2 Rank Id:",rs.getRankResult(rankType,0,2)
# # print "++++++++++++++++++++++++++++++++++++++"
# rs.setInfluenceFactors(rankType,"qId10",factors1)
# factors = rs.getInfluenceFactors(rankType,"qId10")
# # print "get Factors:",factors
# # rs.getFactorsValue(rankType,"qId10",factors)
# score = rs.CalculatQuestionScores(rankType,"qId10")
# rs.setRankScore(rankType,score,"qId10")
Пример #5
0
def oneQuestion(request):

    # 显示该问题
    # import pdb; pdb.set_trace()
    if request.method == "POST":
        qId = request.POST.get("questionId")
    # 用于我关注的问题
    elif request.method == "GET":
        qId = request.path.encode().split('/')[-2]

    # 增加浏览次数
    field = "Qviews"
    rs = RankService()
    Qv = rs.IncrbyFactor(rankType["question"].encode(), str(qId), field)

    factor = ["Qanswer", "Qscore", "sumAscores", "Qfollowers", "Qviews"]
    factors = rs.getFactorsValue(rankType["question"].encode(), str(qId),
                                 factor)

    qa = Question.objects.get(id=qId)
    # import pdb; pdb.set_trace()
    username = User.objects.get(id=qa.user_id).username
    newQuestion = viewObject()
    newQuestion.setKey("title", qa.title)
    newQuestion.setKey("content", qa.content)
    newQuestion.setKey("date", qa.create_date)
    newQuestion.setKey("username", username)
    newQuestion.setKey("userId", qa.user_id)
    newQuestion.setKey("type", "1")
    newQuestion.setKey("id", qa.id)

    # 问题影响因素值
    newQuestion.setKey("Qanswer", factors[0])
    newQuestion.setKey("Qscore", factors[1])
    newQuestion.setKey("sumAscores", factors[2])
    newQuestion.setKey("Qfollowers", factors[3])
    newQuestion.setKey("Qviews", factors[4])

    # import pdb; pdb.set_trace()
    # 是否已关注
    fs = Fs()
    # import pdb; pdb.set_trace()
    IsFollow = fs.isFollower(entityType["question"], qId, request.user.id)
    newQuestion.setKey("IsFollow", IsFollow)

    # 喜欢问题
    l = LikeService()
    # 显示问题评论
    comt = []
    comments = Comment.objects.filter(entity_id=qId)
    # import pdb; pdb.set_trace()
    for com in comments:
        comView = viewObject()
        counts = l.getLikecount("2", str(com.id))
        commentUser = User.objects.get(id=com.user_id)
        comView.setKey("content", com.content)
        comView.setKey("create_date", com.create_date)
        comView.setKey("commentUser", commentUser)
        comView.setKey("commentUserId", com.user_id)
        comView.setKey("commentId", com.id)
        comView.setKey("type", "2")
        comView.setKey("likeCount", counts)
        comt.append(comView)

    return render(request, "QaPlat/oneQa.html", {
        'news': newQuestion,
        'comments': comt
    })
Пример #6
0
def qaPlat(request):

    # 我关注的问题
    # 关注列表和粉丝类表给template
    entity_type = entityType["user"]
    entity_id = str(request.user.id)
    # 所有列表
    offset = 0
    count = -1
    fs = Fs()
    followee = fs.getFollowees(entity_type, entity_id, offset, count)

    # 可以关注问题,评论,用户
    fv = []
    for fee in followee:
        # entityTypeId[0]-->type  entityTypeId[1]-->id
        entityTypeId = fee.split("_")
        if entityTypeId[0] == "1":
            entity_type = entityTypeId[0]
            followerType = "关注问题"
            # print  "关注问题"
            # import pdb; pdb.set_trace()
            followerId = entityTypeId[1]
            title = Question.objects.get(id=int(entityTypeId[1])).title
            user_id = Question.objects.get(id=int(entityTypeId[1])).user_id
            username = User.objects.get(id=user_id).username
            # print "标题:%s 作者:%s "%(title,username)
        else:
            continue
        followeeView = viewObject()
        followeeView.setKey("followerType", followerType)
        followeeView.setKey("entityeType", entity_type)
        followeeView.setKey("followerId", followerId)
        followeeView.setKey("title", title)
        followeeView.setKey("username", username)
        fv.append(followeeView)

    # 喜欢该问题的人数
    l = LikeService()
    """
         显示最新问题N个问题
         从QuestionRank 中拿到问题id
    """
    rs = RankService()
    if rs.CalculateUpdateAll(rankType["question"].encode()):
        print "更新所有QRank 成功"

    QRankIds = rs.getRankResult(rankType["question"].encode(),
                                offset=0,
                                count=-1)

    questions = []
    for QRankId in QRankIds:
        questions.append(Question.objects.get(id=QRankId))

    # questions = Question.objects.order_by('id').reverse()
    news = []
    for qa in questions:
        if qa.title == "" or qa.user_id == "":
            continue
        username = User.objects.get(id=qa.user_id).username
        counts = l.getLikecount("1", str(qa.id))
        newQuestion = viewObject()
        newQuestion.setKey("title", qa.title)
        newQuestion.setKey("content", qa.content)
        newQuestion.setKey("date", qa.create_date)
        newQuestion.setKey("username", username)
        newQuestion.setKey("userId", qa.user_id)
        newQuestion.setKey("id", qa.id)
        newQuestion.setKey("type", "1")
        newQuestion.setKey("likeCount", counts)
        news.append(newQuestion)
    # print news[0].title,news[0].content
    return render(request, "QaPlat/qa.html", {'news': news, "followerQa": fv})
Пример #7
0
def likeService(request):

    entity_type = request.POST.get("entity_type")
    entity_id = request.POST.get("entity_id")
    like = request.POST.get("like")
    userId = request.user.id

    # 评论的实体ID type:1 问题 2评论
    if entity_type == "1":
        entityOwnerId = Question.objects.get(id=entity_id).user_id
    elif entity_type == "2":
        entityOwnerId = Comment.objects.get(id=entity_id).user_id

    l = LikeService()

    # 操作状态与当前状态一样,直接返回
    if l.getLikeStatus(userId, entity_type, entity_id) == like:
        counts = l.getLikecount(entity_type, entity_id)
        print "counts:", counts
        return HttpResponse(content=counts)

    if like == "1":
        l.like(userId, entity_type, entity_id)

        # 如果点赞的是问题,则给该问题因素Qscore增1
        if entity_type == "1":
            # print "关注的是问题"
            counts = l.getLikecount(entity_type, entity_id)
            field = {
                "Qscore": counts,
            }
            rs = RankService()
            rs.setInfluenceFactors(rankType["question"].encode(),
                                   str(entity_id), field)
        # 如果点赞的是评论,
        elif entity_type == "2":
            # 找到评论最初属主ID及type

            ParentEntityId = entity_id
            ParentEntityType = entity_type
            while ParentEntityType == "2":
                EntityId = Comment.objects.get(
                    id=int(ParentEntityId)).entity_id
                EntityType = Comment.objects.get(
                    id=int(ParentEntityId)).entity_type

                ParentEntityId = EntityId
                ParentEntityType = EntityType

            # 如果最初属主是问题,则给该评论属主问题的sumAscores增1
            if ParentEntityType == "1":
                field = "sumAscores"
                rs = RankService()
                print "field"
                sumAscores = rs.IncrbyFactor(rankType["question"].encode(),
                                             str(ParentEntityId), field)

        # 触发异步事件
        em = EventModle()
        em.setKey("TYPE","like").setKey("actorId",userId).setKey("entityType",entity_type)\
        .setKey("entityId",entity_id).setKey("entityOwnerId",entityOwnerId)
        producer = eventProducer()
        producer.fireEvnet(em)

    elif like == "-1":
        l.disLike(userId, entity_type, entity_id)

        # 如果点踩的是问题,则给该问题因素Qscore减1
        if entity_type == "1":
            # print "关注的是问题"
            field = "Qscore"
            rs = RankService()
            Qscore = rs.IncrbyFactor(rankType["question"].encode(),
                                     str(entity_id), field, -1)
        # 如果点踩的是评论,
        elif entity_type == "2":
            # 找到评论最初属主ID及type

            ParentEntityId = entity_id
            ParentEntityType = entity_type
            while ParentEntityType == "2":
                EntityId = Comment.objects.get(
                    id=int(ParentEntityId)).entity_id
                EntityType = Comment.objects.get(
                    id=int(ParentEntityId)).entity_type

                ParentEntityId = EntityId
                ParentEntityType = EntityType

            # 如果最初属主是问题,则给该评论属主问题的sumAscores增1
            if ParentEntityType == "1":
                field = "sumAscores"
                rs = RankService()
                print "field"
                # import pdb; pdb.set_trace()
                sumAscores = rs.IncrbyFactor(rankType["question"].encode(),
                                             str(ParentEntityId), field, -1)

    counts = l.getLikecount(entity_type, entity_id)

    return HttpResponse(content=counts)