Skip to content

Commit

Permalink
updated dbref
Browse files Browse the repository at this point in the history
  • Loading branch information
fiorix committed Jan 20, 2010
1 parent 6f36c93 commit d769c58
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
31 changes: 31 additions & 0 deletions examples/dbref.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env python
# coding: utf-8

import txmongo
from txmongo.dbref import DBRef
from twisted.internet import defer, reactor

@defer.inlineCallbacks
def example():
mongo = yield txmongo.MongoConnection()

foo = mongo.foo # `foo` database
test = foo.test # `test` collection

doc_a = {"username":"foo", "password":"bar"}
result = yield test.insert(doc_a, safe=True)

doc_b = {"settings":"foobar", "owner":DBRef(test, result)}
yield test.insert(doc_b, safe=True)

doc = yield test.find_one({"settings":"foobar"})
print "doc is:", doc

if isinstance(doc["owner"], DBRef):
ref = doc["owner"]
owner = yield foo[ref.collection].find_one(ref.id)
print "owner:", owner

if __name__ == '__main__':
example().addCallback(lambda ign: reactor.stop())
reactor.run()
4 changes: 2 additions & 2 deletions txmongo/_pymongo/_cbsonmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ static int write_element_to_buffer(bson_buffer* buffer, int type_byte, PyObject*
ObjectId = PyObject_GetAttrString(module, "ObjectId");
Py_DECREF(module);

module = PyImport_ImportModule("txmongo._pymongo.dbref");
module = PyImport_ImportModule("txmongo.dbref");
if (!module) {
return 0;
}
Expand Down Expand Up @@ -1461,7 +1461,7 @@ PyMODINIT_FUNC init_cbson(void) {
ObjectId = PyObject_GetAttrString(module, "ObjectId");
Py_DECREF(module);

module = PyImport_ImportModule("txmongo._pymongo.dbref");
module = PyImport_ImportModule("txmongo.dbref");
if (!module) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion txmongo/_pymongo/bson.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
import datetime
import calendar

from txmongo.dbref import DBRef
from txmongo._pymongo.binary import Binary
from txmongo._pymongo.code import Code
from txmongo._pymongo.objectid import ObjectId
from txmongo._pymongo.dbref import DBRef
from txmongo._pymongo.son import SON
from txmongo._pymongo.errors import InvalidBSON, InvalidDocument
from txmongo._pymongo.errors import InvalidName, InvalidStringData
Expand Down
4 changes: 4 additions & 0 deletions txmongo/_pymongo/dbref.py → txmongo/dbref.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import types

from txmongo._pymongo.son import SON
from txmongo.collection import Collection


class DBRef(object):
Expand All @@ -38,6 +39,9 @@ def __init__(self, collection, id, database=None):
.. versionadded:: 1.1.1
The `database` parameter.
"""
if isinstance(collection, Collection):
collection = collection._collection_name

if not isinstance(collection, types.StringTypes):
raise TypeError("collection must be an instance of (str, unicode)")
if not isinstance(database, (types.StringTypes, types.NoneType)):
Expand Down

0 comments on commit d769c58

Please sign in to comment.