Ejemplo n.º 1
0
    def post(self, action):
        entity_id = int(self.get_argument("entity_id", 0))
        entity_type = self.get_argument("entity_type", None)
        user_id = self.current_user["user_id"]
        assert action in 'addlike dellike adddislike deldislike'.split()
        assert entity_type in 'share comment viewpoint tag'.split()
        _action = action[3:] + 'num'
        doc = {
            'user_id': user_id,
            'entity_id': entity_id,
            'entity_type': entity_type,
        }
        is_changed = Like.change_like(doc, _action, action[:3])
        # 冗余储存 没有做成事件绑定,需要定期校验修复
        if entity_type == 'share':
            entity = Share.by_sid(entity_id)
            # 如果是管理员,需要将status + 1
            # 64=kp 65=kp email
            # 63=lb # 60=xie
            if is_changed and user_id in admin_ids:
                if action == 'addlike':
                    if entity['status'] == 0:
                        entity['suggested'] = time.time()
                    entity['status'] += 1
                elif action == 'adddislike':
                    entity['status'] -= 1
                elif action == 'deldislike':
                    entity['status'] += 1
                else:
                    entity['status'] -= 1
        elif entity_type == 'comment':
            entity = Comment.by_sid(entity_id)
        elif entity_type == 'viewpoint':
            entity = Viewpoint.by_sid(entity_id)
        elif entity_type == 'tag':
            entity = Tag.by_sid(entity_id)
            user = User.by_sid(user_id)
            if action == 'addlike' and entity.name not in user.user_tags:
                user.user_tags.append(entity.name)
            elif action == 'dellike' and entity.name in user.user_tags:
                user.user_tags.pop(entity.name)
            user.save()

        if action[:3] == 'add':
            entity[_action] += 1
        else:
            entity[_action] -= 1
        entity.save()
        if entity.dislikenum < 0:
            entity.dislikenum = 0
        if entity.likenum < 0:
            entity.likenum = 0
        self.res = {
            'likenum': entity.likenum,
            'dislikenum': entity.dislikenum,
        }
        self.write_json()
Ejemplo n.º 2
0
 def post(self, action):
     share_id = int(self.get_argument("share_id", None))
     user_id = self.current_user["user_id"]
     doc = {
         'user_id': user_id,
         'share_id': share_id
     }
     newlikes = None
     if action == 'addlike':
         Like.change_like(doc, 'likenum')
         share = Share.by_sid(share_id)
         share.likenum += 1
         share.save()
         user = User.by_sid(share.user_id)
         user.user_leaf += 4
         user.save()
         user = User.by_sid(user_id)
         user.user_leaf += 2
         user.save()
         newlikes = str(share.likenum)
     elif action == 'dellike':
         Like.change_like(doc, 'likenum')
         share = Share.by_sid(share_id)
         share.likenum -= 1
         share.save()
         user = User.by_sid(share.user_id)
         user.user_leaf -= 4
         user.save()
         user = User.by_sid(user_id)
         user.user_leaf -= 2
         user.save()
         newlikes = str(share.likenum)
     elif action == 'adddislike':
         Like.change_like(doc, 'dislikenum')
         share = Share.by_sid(share_id)
         share.dislikenum += 1
         share.save()
         newlikes = str(share.dislikenum)
     elif action == 'deldislike':
         Like.change_like(doc, 'dislikenum')
         share = Share.by_sid(share_id)
         share.dislikenum -= 1
         share.save()
         newlikes = str(share.dislikenum)
     self.write(newlikes)
Ejemplo n.º 3
0
    def post(self, action):
        entity_id = int(self.get_argument("entity_id", 0))
        entity_type = self.get_argument("entity_type", None)
        user_id = self.current_user["user_id"]
        assert action in 'addlike dellike adddislike deldislike'.split()
        assert entity_type in 'share comment viewpoint'.split()
        _action = action[3:] + 'num'
        doc = {
            'user_id': user_id,
            'entity_id': entity_id,
            'entity_type': entity_type,
        }
        is_changed = Like.change_like(doc, _action, action[:3])

        # 冗余储存 没有做成事件绑定,需要定期校验修复
        if entity_type == 'share':
            entity = Share.by_sid(entity_id)
            # 如果是管理员,需要将status + 1
            # 64=kp 65=kp email
            # 63=lb
            # 60=xie
            if is_changed and user_id in admin_ids:
                if action == 'addlike':
                    entity['status'] += 1
                elif action == 'adddislike':
                    entity['status'] -= 1
                elif action == 'deldislike':
                    entity['status'] += 1
                else:
                    entity['status'] -= 1
        elif entity_type == 'comment':
            entity = Comment.by_sid(entity_id)
        elif entity_type == 'viewpoint':
            entity = Viewpoint.by_sid(entity_id)
        if action[:3] == 'add':
            entity[_action] += 1
        else:
            entity[_action] -= 1
        entity.save()
        self.res = {
            'likenum': entity.likenum,
            'dislikenum': entity.dislikenum,
        }
        self.write_json()