コード例 #1
0
ファイル: following.py プロジェクト: buyinta/Flask_demo
    def post(self):
        # 获取参数
        userid = g.userid
        parser = RequestParser()
        parser.add_argument('target', required=True, location='json', type=int)
        args = parser.parse_args()
        target = args.target

        # 查询参数
        relation = Relation.query.options(load_only(Relation.id)).filter(
            Relation.user_id == userid, Relation.author_id == target).first()
        if relation:
            # 如果存在关系对象,将记录修改为关注
            relation.relation = Relation.RELATION.FOLLOW

        else:
            relation = Relation(user_id=userid,
                                author_id=target,
                                relation=Relation.RELATION.FOLLOW)
            db.session.add(relation)

        # 作者的粉丝数量加一
        User.query.filter(User.id == target).update(
            {'fans_count': User.fans_count + 1})
        # 用户的关注数量加一
        User.query.filter(User.id == userid).update(
            {'following_count': User.following_count + 1})
        db.session.commit()

        return {'target': target}
コード例 #2
0
ファイル: following.py プロジェクト: yeyuning1/FlaskProjects
    def post(self):
        """
        关注用户
        """
        # 解析参数
        json_parser = RequestParser()
        json_parser.add_argument('target', required=True, location='json')
        args = json_parser.parse_args()
        target = args.target
        if target == g.user_id:
            return {'message': 'User cannot follow self.'}, 400
        ret = 1

        # 往数据库中存储关注情况
        try:
            follow = Relation(user_id=g.user_id, target_user_id=target, relation=Relation.RELATION.FOLLOW)
            db.session.add(follow)
            db.session.commit()
        except IntegrityError:
            db.session.rollback()
            ret = Relation.query.filter(Relation.user_id == g.user_id,
                                        Relation.target_user_id == target,
                                        Relation.relation != Relation.RELATION.FOLLOW)\
                .update({'relation': Relation.RELATION.FOLLOW})
            db.session.commit()

        if ret > 0:
            timestamp = time.time()
            # 用户的关注列表中缓存层中追加数据
            user.UserFollowingCache(g.user_id).update(target, timestamp)
            # 作者的粉丝列表中缓存层中追加数据
            user.UserFollowersCache(target).update(g.user_id, timestamp)
            # 统计数据的持久化存储
            statistic.UserFollowingCountStorage.incr(g.user_id)
            statistic.UserFansCountStorage.incr(target)

        """发送关注通知"""
        # 获取粉丝的基本信息
        _user = user.UserProfileCache(g.user_id).get()
        _data = {
            'user_id': g.user_id,
            'user_name': _user['name'],
            'user_photo': _user['profile_photo'],
            'timestamp': int(time.time())
        }
        # 将推送通知发送给IM服务器(放入消息队列中)    将消息发送到作者的用户id对应的房间 target="2"
        current_app.siomgr.emit('following notify', _data, room=target)


        return {'target': target}, 201
        
コード例 #3
0
    def post(self):
        """
        关注用户
        """
        json_parser = RequestParser()
        json_parser.add_argument('target',
                                 type=parser.user_id,
                                 required=True,
                                 location='json')
        args = json_parser.parse_args()
        target = args.target
        if target == g.user_id:
            return {'message': 'User cannot follow self.'}, 400
        ret = 1
        try:
            follow = Relation(user_id=g.user_id,
                              target_user_id=target,
                              relation=Relation.RELATION.FOLLOW)
            db.session.add(follow)
            db.session.commit()
        except IntegrityError:
            db.session.rollback()
            ret = Relation.query.filter(Relation.user_id == g.user_id,
                                        Relation.target_user_id == target,
                                        Relation.relation != Relation.RELATION.FOLLOW)\
                .update({'relation': Relation.RELATION.FOLLOW})
            db.session.commit()

        if ret > 0:
            timestamp = time.time()
            cache_user.UserFollowingCache(g.user_id).update(target, timestamp)
            cache_user.UserFollowersCache(target).update(g.user_id, timestamp)
            cache_statistic.UserFollowingsCountStorage.incr(g.user_id)
            cache_statistic.UserFollowersCountStorage.incr(target)
            cache_user.UserRelationshipCache(g.user_id).clear()

        # 发送关注通知
        _user = cache_user.UserProfileCache(g.user_id).get()
        _data = {
            'user_id': g.user_id,
            'user_name': _user['name'],
            'user_photo': _user['photo'],
            'timestamp': int(time.time())
        }
        # 通过socketio提供的kombu管理对象 向rabbitmq中写入数据,记录需要由socketio服务器向客户端推送消息的任务
        current_app.sio_mgr.emit('following notify',
                                 data=_data,
                                 room=str(target))

        return {'target': target}, 201
コード例 #4
0
ファイル: blacklist.py プロジェクト: dylankung/toutiao
    def post(self):
        """
        拉黑用户
        """
        json_parser = RequestParser()
        json_parser.add_argument('target',
                                 type=parser.user_id,
                                 required=True,
                                 location='json')
        args = json_parser.parse_args()
        target = args.target
        if target == g.user_id:
            return {'message': 'User cannot blacklist self.'}, 400
        try:
            blacklist = Relation(user_id=g.user_id,
                                 target_user_id=target,
                                 relation=Relation.RELATION.BLACKLIST)
            db.session.add(blacklist)
            db.session.commit()
        except IntegrityError:
            db.session.rollback()
            fol_ret = 0
            # Change relation from DELETE to BLACKLIST
            del_ret = Relation.query.filter(Relation.user_id == g.user_id,
                                        Relation.target_user_id == target,
                                        Relation.relation == Relation.RELATION.DELETE) \
                .update({'relation': Relation.RELATION.BLACKLIST})
            if del_ret == 0:
                # Change relation from FOLLOW to BLACKLIST
                fol_ret = Relation.query.filter(Relation.user_id == g.user_id,
                                            Relation.target_user_id == target,
                                            Relation.relation == Relation.RELATION.FOLLOW) \
                    .update({'relation': Relation.RELATION.BLACKLIST})

            db.session.commit()

            if fol_ret > 0:
                timestamp = time.time()
                cache_user.UserFollowingCache(g.user_id).update(
                    target, timestamp, -1)
                cache_user.UserFollowersCache(target).update(
                    g.user_id, timestamp, -1)
                cache_statistic.UserFollowingsCountStorage.incr(g.user_id, -1)
                cache_statistic.UserFollowersCountStorage.incr(target, -1)

        return {'target': target}, 201
コード例 #5
0
    def post(self):
        userid = g.userid
        parser = RequestParser()
        parser.add_argument('target',required=True,location='json',type=positive)
        args = parser.parse_args()
        target_id = args.target

        if userid ==target_id:
            return {'message':"can not follow self"},400

        #先查询关系是否存在
        relation = Relation.query.options(load_only(Relation.id)).\
            filter(Relation.user_id ==userid,Relation.target_user_id==target_id).first()

        if relation:#更新关系为关注
            relation.relation = Relation.RELATION.FOLLOW

        else:#不存在关系,添加关系
            relation = Relation(user_id = userid,target_user_id=target_id,relation=Relation.RELATION.FOLLOW)
            db.session.add(relation)
        db.session.commit()

        return {'target':target_id},201