示例#1
0
    def post(self, *args, **kwargs):
        rep = BaseResponse()

        news_id = self.get_argument('news_id', None)
        if not news_id:
            rep.summary = "新闻ID不能为空."
        else:
            user_info_id = self.session['user_info']['nid']
            conn = ORM.session()
            has_favor = conn.query(ORM.Favor).filter(ORM.Favor.user_info_id == user_info_id,
                                                     ORM.Favor.news_id == news_id).count()
            if has_favor:
                conn.query(ORM.Favor).filter(ORM.Favor.user_info_id == user_info_id,
                                             ORM.Favor.news_id == news_id).delete()
                conn.query(ORM.News).filter(ORM.News.nid == news_id).update(
                    {"favor_count": ORM.News.favor_count - 1}, synchronize_session="evaluate")
                rep.code = StatusCodeEnum.FavorMinus
            else:
                conn.add(ORM.Favor(user_info_id=user_info_id, news_id=news_id, ctime=datetime.datetime.now()))
                conn.query(ORM.News).filter(ORM.News.nid == news_id).update(
                    {"favor_count": ORM.News.favor_count + 1}, synchronize_session="evaluate")
                rep.code = StatusCodeEnum.FavorPlus
            conn.commit()
            conn.close()

            rep.status = True

        self.write(json.dumps(rep.__dict__))
示例#2
0
def favor(request):
    """
    点赞
    :param request:
    :return:
    """
    rep = BaseResponse()

    news_id = request.POST.get('news_id', None)
    if not news_id:
        rep.summary = "新闻ID不能为空."
    else:
        user_info_id = request.session['user_info']['nid']

        has_favor = models.Favor.objects.filter(user_info_id=user_info_id, news_id=news_id).count()
        if has_favor:
            models.Favor.objects.filter(user_info_id=user_info_id, news_id=news_id).delete()
            models.News.objects.filter(nid=news_id).update(favor_count=F('favor_count') - 1)

            rep.code = StatusCodeEnum.FavorMinus
        else:
            models.Favor.objects.create(user_info_id=user_info_id, news_id=news_id, ctime=datetime.datetime.now())
            models.News.objects.filter(nid=news_id).update(favor_count=F('favor_count') + 1)

            rep.code = StatusCodeEnum.FavorPlus

        rep.status = True

    return HttpResponse(json.dumps(rep.__dict__))
示例#3
0
def favor(request):
    """新闻点赞"""
    rep = BaseResponse()

    news_id = request.POST.get("news_id", None)
    if not news_id:
        rep.summary = "新闻ID不能为空"
    else:
        user_info_id = request.session["user_info"]["nid"]

        has_favor = models.Favor.objects.filter(user_info_id=user_info_id, news_id=news_id).count()
        if has_favor:
            models.Favor.objects.filter(user_info_id=user_info_id, news_id=news_id).delete()
            models.News.objects.filter(nid=news_id).update(favor_count=F("favor_count")-1)

            rep.code = StatusCodeEnum.FavorMinus
        else:
            models.Favor.objects.create(user_info_id=user_info_id, news_id=news_id, ctime=datetime.datetime.now())
            models.News.objects.filter(nid=news_id).update(favor_count=F("favor_count")+1)

            rep.code = StatusCodeEnum.FavorPlus

        # 获取当前点赞数返回给前端
        obj = models.News.objects.get(nid=news_id)
        rep.counts = obj.favor_count

        rep.status = True
    return HttpResponse(json.dumps(rep.__dict__))
示例#4
0
    def post(self, *args, **kwargs):
        rep = BaseResponse()

        # 前端对应index.html中的DoFavor方法中的ajax请求
        news_id = self.get_argument('news_id', None)
        if not news_id:
            rep.summary = "新闻ID不能为空."
        else:
            # 从session中获取用户信息
            user_info_id = self.session['user_info']['nid']
            conn = ORM.session()
            # 在Favor表中查询是否有该用户的点赞记录
            has_favor = conn.query(ORM.Favor).filter(
                ORM.Favor.user_info_id == user_info_id,
                ORM.Favor.news_id == news_id).count()
            if has_favor:
                # 用户已点过赞,则此时用户的点击操作代表消除自己的点赞
                # 因此先从数据库中删除点赞该用户的点赞记录
                # 分别从Favor和News表中同时删除,保持数据一致
                conn.query(ORM.Favor).filter(
                    ORM.Favor.user_info_id == user_info_id,
                    ORM.Favor.news_id == news_id).delete()
                conn.query(ORM.News).filter(ORM.News.nid == news_id).update(
                    {"favor_count": ORM.News.favor_count - 1},
                    synchronize_session="evaluate")
                # 要给前端发送的code信息,以便前端做出相应的效果改变
                rep.code = StatusCodeEnum.FavorMinus
            else:
                # 否则用户执行点赞操作,数据库内记录+1
                conn.add(
                    ORM.Favor(user_info_id=user_info_id,
                              news_id=news_id,
                              ctime=datetime.datetime.now()))
                conn.query(ORM.News).filter(ORM.News.nid == news_id).update(
                    {"favor_count": ORM.News.favor_count + 1},
                    synchronize_session="evaluate")
                rep.code = StatusCodeEnum.FavorPlus
            conn.commit()
            conn.close()

            rep.status = True
        # 将返回信息写入前端
        self.write(json.dumps(rep.__dict__))