Esempio n. 1
0
    def unfollow(cls, from_user_id, to_user_id):
        if from_user_id == to_user_id:
            return 0
        # Queryset 的 delete 操作返回两个值,一个是删了多少数据,一个是具体每种类型删了多少
        # 为什么会出现多种类型数据的删掉?因为可能因为 foreign key 设置了 cascade 出现级联
        # 删除,也就是比如 A model 的某个属性是 B model 的 foreign key,并且设置了
        # on_delete = models.CASCADE,那么当 B 的某个数据被删除的时候,A 中的关联也会被删除。
        # 所以 CASCADE 是很危险的,一般最好不要用,而是用 on_delete = models.SET_NULL
        # 取而代之,这样至少可以避免误删除操作带来的多米诺效应
        if not GateKeeper.is_switch_on('switch_friendship_to_hbase'):
            deleted, _ = Friendship.objects.filter(
                from_user_id=from_user_id,
                to_user_id=to_user_id,
            ).delete()
            return deleted

        instance = cls.get_follow_instance(from_user_id, to_user_id)
        if instance is None:
            return 0

        HBaseFollowing.delete(from_user_id=from_user_id,
                              created_at=instance.created_at)
        HBaseFollower.delete(to_user_id=to_user_id,
                             created_at=instance.created_at)
        return 1
    def unfollow(cls, from_user_id, to_user_id):
        if from_user_id == to_user_id:
            return 0

        if not GateKeeper.is_switch_on('switch_friendship_to_hbase'):
            deleted, _ = Friendship.objects.filter(
                from_user_id=from_user_id,
                to_user_id=to_user_id,
            ).delete()
            return deleted

        instance = cls.get_follow_instance(from_user_id, to_user_id)
        if instance is None:
            return 0

        HBaseFollowing.delete(from_user_id=from_user_id,
                              created_at=instance.created_at)
        HBaseFollower.delete(to_user_id=to_user_id,
                             created_at=instance.created_at)
        return 1
Esempio n. 3
0
    def unfollow(cls, from_user_id, to_user_id):
        if from_user_id == to_user_id:
            return 0

        if not GateKeeper.is_switch_on('switch_friendship_to_hbase'):
            delete, _ = Friendship.objects.filter(
                from_user_id=from_user_id,
                to_user_id=to_user_id,
            ).delete()
            return delete

        following = cls.get_following_instance(from_user_id=from_user_id,
                                               to_user_id=to_user_id)
        # fs = HBaseFollowing.filter(prefix=(from_user_id, None))
        # for f in fs:
        #     print(f.from_user_id, f.created_at, '->',  f.to_user_id)
        # print(following)
        if following is None:
            return 0
        HBaseFollowing.delete(from_user_id=from_user_id,
                              created_at=following.created_at)
        HBaseFollower.delete(to_user_id=to_user_id,
                             created_at=following.created_at)
        return 1