예제 #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()
예제 #2
0
파일: api_tag.py 프로젝트: anwen/anwen
    def get(self):
        ver = self.get_argument("ver", 3)
        name = self.get_argument("name", '')
        sid = self.get_argument("id", 0)
        ver = int(ver)
        sid = int(sid)
        # parents [0]
        if name or sid:
            # 具体某个标签
            if not name and sid:
                tag = Tag.by_sid(sid)
                name = tag['name']
            self.res = d_tags_v3.get(name, {})

            if self.res:
                parents = d_tags_parents.get(self.res['name'], {})
                node = d_tags_v3.get(parents, {})
                self.res['parents'] = copy.deepcopy(node)
                self.res['parents'].pop('subs')

                brothers = []
                for sub in copy.deepcopy(node)['subs']:
                    sub.pop('subs')
                    brothers.append(sub)
                self.res['brothers'] = brothers
                if parents:
                    parents_p = d_tags_parents.get(parents, {})
                    if parents_p:
                        node = d_tags_v3.get(parents_p, {})

                        self.res['parents']['parents'] = copy.deepcopy(node)
                        self.res['parents']['parents'].pop('subs')

                        parents_pp = d_tags_parents.get(parents_p, {})
                        if parents_pp:
                            node = d_tags_v3.get(parents_pp, {})
                            self.res['parents']['parents'][
                                'parents'] = copy.deepcopy(node)
                            self.res['parents']['parents']['parents'].pop(
                                'subs')

                    self.res['articleNumber'] = Share.count_by_tag(
                        self.res['name'])
                    self.res['followerNumber'] = User.find({
                        'user_tags': {
                            '$in': [name]
                        }
                    }).count()
                    self.res['isFollowing'] = False
                    user_id = self.current_user[
                        "user_id"] if self.current_user else None
                    if user_id:
                        user = User.by_sid(user_id)
                        if user:
                            # model1
                            self.res['isFollowing'] = name in user['user_tags']
                            # model2 查看like 表
                            # self.res['isFollowing'] = name in user['user_tags']

                    print("self.res['followerNumber']",
                          self.res['followerNumber'])
                tag = Tag.by_name(self.res['name'])

                if tag:
                    self.res['id'] = tag['id']
            else:
                self.res['id'] = -1
        else:
            # 从根节点开始
            if ver == 3:
                self.res = d_tags_v2
                self.res['parents'] = {}  # root
                self.res['articleNumber'] = Share.count_by_tag(
                    self.res['name'])
                tag = Tag.by_name(self.res['name'])  # 7491
                self.res['id'] = -1
                if tag:
                    self.res['id'] = tag['id']
            elif ver == 2:
                self.res = d_tags_v2
                if self.current_user and 'user_id' in self.current_user:
                    user = User.by_sid(self.current_user['user_id'])
                    self.res['watched_tags'] = user['user_tags']
            else:
                self.res = d_tags
        self.write_json()