Пример #1
0
    def by_doc(ctx: ByDocContext):
        doc = ctx.document
        if updater.field_name not in doc or doc[updater.field_name] is None:
            return

        f = doc[updater.field_name]
        is_dict = isinstance(f, dict)
        collection_name = ctx.collection.name if ctx.collection is not None else None

        if isinstance(f, str):  # ObjectId as string
            try:
                f = bson.ObjectId(f)
            except bson.errors.BSONError:
                pass

        # We cannot get dynamic ref from other types of refs because
        # of lack of '_cls' value. Mongoengine fields which use this
        # converter can keep DBRef. So return DBRef instead
        if is_dict and isinstance(f.get('_ref'),
                                  bson.DBRef):  # Already dynamic ref
            return
        elif isinstance(f, bson.DBRef):
            return
        elif is_dict and isinstance(f.get('_id'), bson.ObjectId):  # manual ref
            doc[updater.field_name] = bson.DBRef(collection_name, f['_id'])
        elif isinstance(f, bson.ObjectId):
            doc[updater.field_name] = bson.DBRef(collection_name, f)
        elif updater.migration_policy.name == 'strict':  # Other data type
            raise InconsistencyError(
                f"Field {updater.field_name} has wrong value {f!r} "
                f"(should be DBRef, ObjectId, manual ref, dynamic ref) "
                f"in record {doc}")
Пример #2
0
    def by_doc(ctx: ByDocContext):
        doc = ctx.document
        if updater.field_name not in doc or doc[updater.field_name] is None:
            return

        f = doc[updater.field_name]
        if isinstance(f, bson.DBRef):  # Already DBRef
            return
        elif isinstance(f, str):  # ObjectId as string
            try:
                f = bson.ObjectId(f)
            except bson.errors.BSONError:
                pass

        collection_name = ctx.collection.name if ctx.collection is not None else None
        is_dict = isinstance(f, dict)
        if is_dict and isinstance(f.get('_id'), bson.ObjectId):  # manual ref
            doc[updater.field_name] = bson.DBRef(collection_name, f['_id'])
        elif is_dict and isinstance(f.get('_ref'), bson.DBRef):  # dynamic ref
            doc[updater.field_name] = f['_ref']
        elif isinstance(f, bson.ObjectId):
            doc[updater.field_name] = bson.DBRef(collection_name, f)
        elif updater.migration_policy.name == 'strict':  # Other data type
            raise InconsistencyError(
                f"Field {updater.field_name} has wrong value {f!r} "
                f"(should be DBRef, ObjectId, manual ref, dynamic ref, "
                f"ObjectId string) in record {doc}")
Пример #3
0
 def remove_token(self, user_id: str, token: str):
     tokens: pymongo.collection.Collection = self.db.token
     tokens.find_one_and_delete({
         "user":
         bson.DBRef("user", bson.ObjectId(user_id)),
         "token":
         token,
     })
Пример #4
0
 def add_token(self, user_id: str, token: str) -> str:
     tokens: pymongo.collection.Collection = self.db.token
     token_id = tokens.insert_one({
         "token":
         token,
         "user":
         bson.DBRef("user", bson.ObjectId(user_id)),
     }).inserted_id
     return token_id
Пример #5
0
 def deocde_reference(field_type, field_name, obj):
     return {
         field_name:
         field_type.to_python(
             bson.DBRef(obj["collection"],
                        bson.ObjectId(obj["id"]),
                        database=obj.get("database")) if field_type.
             dbref else obj)
     }
Пример #6
0
def _get_object(data, position, obj_end, opts, dummy):
    """Decode a BSON subdocument to opts.document_class or bson.dbref.DBRef."""
    obj_size = bson._UNPACK_INT(data[position:position + 4])[0]
    end = position + obj_size - 1
    if data[end:position + obj_size] != b"\x00":
        raise bson.InvalidBSON("bad eoo")
    if end >= obj_end:
        raise bson.InvalidBSON("invalid object length")
    if _raw_document_class(opts.document_class):
        return (opts.document_class(data[position:end + 1],
                                    opts), position + obj_size)

    obj = _elements_to_dict(data, position + 4, end, opts, subdocument=True)
    position += obj_size
    if "$ref" in obj:
        return (bson.DBRef(obj.pop("$ref"), obj.pop("$id", None),
                           obj.pop("$db", None), obj), position)
    return obj, position
Пример #7
0
 def by_doc(ctx: ByDocContext):
     doc = ctx.document
     if isinstance(doc.get(updater.field_name), bson.ObjectId):
         doc[updater.field_name] = bson.DBRef(ctx.collection.name,
                                              doc[updater.field_name])
Пример #8
0
 def test_dbref(self):
     validate({'a': bson.DBRef}, {'a': bson.DBRef('a', 'b')})