예제 #1
0
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument("token")
        parser.add_argument("userid")

        args = parser.parse_args()

        args["userid"] = ObjectId(args["userid"])  # 将str的userid装换成objectid
        # 验证参数完整性
        ver_list = ["token", "userid"]
        if CustomTools.ver_par_integrity(ver_list, args) is False:
            return CustomTools.failure(1)

        # 验证
        token = args["token"]
        verify = Verify()
        if verify.verify_token(token) is False:
            failure = {'success': False, 'error': verify.error}
            return failure

        # 检测关注方是否被被关注方拉黑
        temp = DbTools.user_se_objectid(args["userid"])
        blacklist = CustomTools.batch_get_deref_userdata(
            temp['Information']['blacklist'])
        for user in CustomTools.bat_deref_fo(blacklist):
            if args["userid"] == user["_id"]:
                failure = {'success': False, 'error': '你已被屏蔽'}
                return failure
        # 检测是否已关注过该用户
        following = verify.userdata["Information"]["following"]
        for user in CustomTools.bat_deref_fo(following):
            if str(args["userid"]) == user["_id"]:
                failure = {'success': False, 'error': '你已关注该用户'}
                return failure
        # 给关注方添加following
        ref_user = DBRef(collection="userData", id=args["userid"])
        userData.update({'_id': verify.userdata["_id"]}, {
            '$push': {
                'Information.following': {
                    '$each': [ref_user],
                    '$position': 0
                }
            }
        })

        # 给被关注方添加followed
        ref_user = DBRef(collection="userData", id=verify.userdata["_id"])
        userData.update({'_id': args['userid']}, {
            '$push': {
                'Information.followed': {
                    '$each': [ref_user],
                    '$position': 0
                }
            }
        })

        success = {'success': True}
        return success
예제 #2
0
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument("token")
        parser.add_argument("title")
        parser.add_argument("text")
        parser.add_argument("tag")
        parser.add_argument("extra", action="append")
        parser.add_argument("type", type=int)

        args = parser.parse_args()
        # 验证必要参数完整性
        ver_list = ["token", "title", "text", "tag", "type"]
        if CustomTools.ver_par_integrity(ver_list, args) is False:
            return CustomTools.failure(1)
        if not args["extra"]:
            args["extra"] = []
        # 验证
        token = args["token"]
        verify = Verify()
        if verify.verify_token(token) is False or verify.verify_perm(
                args["type"]) is False:
            failure = {"success": False, "error": verify.error}
            return failure
        """插入新文章
        """
        up_time = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
        # 祖先数组树结构
        user_ref = DBRef(collection="userData", id=verify.userdata["_id"])
        article = {
            "title": args["title"],
            "text": args["text"],
            "user": user_ref,
            "type": args["type"],
            "date": up_time,
            "tag": args["tag"],
            "extra": args["extra"],
            "click": 0,
            "ancestor": None,
            "parent": None,
            "children": []
        }
        article_id = announcement.insert(article)

        # 在该用户中添加该文章引用
        article_ref = DBRef(collection="announcement", id=article_id)
        userData.update({"_id": verify.userdata["_id"]}, {
            "$push": {
                "Information.treehole": {
                    "$each": [article_ref],
                    "$position": 0
                }
            }
        })

        success = {'success': True}
        return success
예제 #3
0
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument("token")
        parser.add_argument("userid")

        args = parser.parse_args()
        args["userid"] = ObjectId(args["userid"])  # 将str的userid装换成objectid
        # 验证参数完整性
        ver_list = ["token", "userid"]
        if CustomTools.ver_par_integrity(ver_list, args) is False:
            return CustomTools.failure(1)

        # 验证
        token = args['token']
        verify = Verify()
        if verify.verify_token(token) is False:
            failure = {'success': False, 'error': verify.error}
            return failure

        try:
            # 拉黑方删除followed
            ref_user = DBRef(collection="userData", id=args["userid"])
            userData.update({"_id": verify.userdata["_id"]},
                            {"$pull": {
                                "Information.followed": ref_user
                            }})

            # 被拉黑删除following
            ref_user = DBRef(collection="userData", id=verify.userdata["_id"])
            userData.update({"_id": args["userid"]},
                            {"$pull": {
                                "Information.following": ref_user
                            }})

            # 将被拉黑方放加入blacklist
            ref_user = DBRef(collection="userData", id=args["userid"])
            userData.update({"_id": verify.userdata["_id"]}, {
                '$push': {
                    'Information.blacklist': {
                        '$each': [ref_user],
                        '$position': 0
                    }
                }
            })
        except:
            failure = {'success': False, 'error': '内部数据库错误'}
            return failure
        success = {'success': True}
        return success
예제 #4
0
print(child_curr)

parent_curr = []
for i in parent_find:
    parent_curr.append(db[parent].find({parent_name: i}))

print(child_curr[1]['_id'])

#for i in parent_curr:
#    print(child_curr[i]['_id'])
'''for i in parent_curr:
    for j in i:
        j[parent_name] = DBRef(collection = child, id="{}".format(child_curr[i['_id']]))
        db[parent].save(j)'''

for i in parent_curr:
    for j in i:
        if str(j[parent_name]) in mapp_value:
            index = mapp_value.index(str(j[parent_name]))
            j[parent_name] = DBRef(collection=child,
                                   id="{}".format(child_curr[index]['_id']))
            db[parent].save(j)

#for i in parent_curr:
#    for j in i:
#        if j['Species'] in mapp_value == True:
#            print("true")
#        else:
#            print("flase")
'''d['Species'] = DBRef(collection = "title",id = setosa_find['_id'])
    db.iris.save(d)'''
예제 #5
0
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('article_ID')
        parser.add_argument('parent_ID')
        parser.add_argument('token')
        parser.add_argument('content')

        args = parser.parse_args()

        args["article_ID"] = ObjectId(args["article_ID"])
        if args["parent_ID"]:
            args["parent_ID"] = ObjectId(args["parent_ID"])
        # 验证参数完整性
        ver_list = ["article_ID", "token", "content"]
        if CustomTools.ver_par_integrity(ver_list, args) is False:
            return CustomTools.failure(1)

        # 验证
        token = args['token']
        verify = Verify()
        if verify.verify_token(token) is False:
            failure = {'success': False, 'error': verify.error}
            return failure

        # 生成引用
        if args["parent_ID"]:
            ref_parent = DBRef(collection="announcement", id=args["parent_ID"])
        else:
            ref_parent = None
        ref_ancestor = DBRef(collection="announcement", id=args["article_ID"])
        upTime = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())

        # 将评论写入db.announcement
        user_ref = DBRef(collection="userData", id=verify.userdata["_id"])
        comment_data = {
            'text': args['content'],
            'user': user_ref,
            'type': None,
            'date': upTime,
            'ancestor': args["article_ID"],
            'parent': ref_parent,
            'children': []
        }
        try:
            id_ = announcement.insert(comment_data)
            ref_id = DBRef(collection="announcement", id=id_)
            # 写入成功后将此评论的引用提交给父评论
            if args["parent_ID"]:
                announcement.update({"_id": args["parent_ID"]}, {
                    "$push": {
                        "children": {
                            "$each": [ref_id],
                            "$position": 0
                        }
                    }
                })
                # 发送至消息提示至父评论
                par_author = db.dereference(ref_parent)
                Message.message_add(ref_id, par_author['_id'])
            # 发送至消息提示至作者,
            ref_author = announcement.find_one(
                {"_id": ObjectId(args['article_ID'])})
            author = db.dereference(ref_author["user"])
            Message.message_add(ref_id, author['_id'])
        except:
            failure = {'success': False, 'error': '内部数据库错误'}
            return failure
        success = {'success': True}
        return success
예제 #6
0
    def ref(self, table, _id):
        if not isinstance(_id, ObjectId):
            _id = ObjectId(_id)

        return DBRef(table, _id)