Пример #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 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__))