Exemple #1
0
 def to_python(self, value):
     if value is not None:
         if not isinstance(value, DBRef):
             if "$ref" not in value:
                 value = value.get_dbref()
             else:
                 value = DBRef(database=value.get("$db"), collection=value["$ref"], id=value["$id"])
         if value.database:
             database = value.database
         else:
             database = self._fallback_database
         if database is None:
             raise RuntimeError(
                 "It appears that you try to use autorefs. I found a DBRef without"
                 " database specified.\n If you do want to use the current database, you"
                 " have to add the attribute `force_autorefs_current_db` as True. Please see the doc"
                 " for more details.\n The DBRef without database is : %s " % value
             )
         col = self.connection[database][value.collection]
         doc = col.find_one({"_id": value.id})
         if doc is None:
             raise AutoReferenceError(
                 "Something wrong append. You probably change"
                 " your object when passing it as a value to an autorefs enable document.\n"
                 'A document with id "%s" is not saved in the database "%s" but was giving as'
                 " a reference to a %s document" % (value.id, database, self._doc.__name__)
             )
         return self._doc(doc, collection=col)
Exemple #2
0
 def to_python(self, value):
     if value is not None:
         if not isinstance(value, DBRef):
             if '$ref' not in value:
                 value = value.get_dbref()
             else:
                 value = DBRef(database=value.get('$db'),
                               collection=value['$ref'],
                               id=value['$id'])
         if value.database:
             database = value.database
         else:
             database = self._fallback_database
         if database is None:
             raise RuntimeError(
                 "It appears that you try to use autorefs. I found a DBRef without"
                 " database specified.\n If you do want to use the current database, you"
                 " have to add the attribute `force_autorefs_current_db` as True. Please see the doc"
                 " for more details.\n The DBRef without database is : %s "
                 % value)
         col = self.connection[database][value.collection]
         doc = col.find_one({'_id': value.id})
         if doc is None:
             raise AutoReferenceError(
                 'Something wrong append. You probably change'
                 ' your object when passing it as a value to an autorefs enable document.\n'
                 'A document with id "%s" is not saved in the database "%s" but was giving as'
                 ' a reference to a %s document' %
                 (value.id, database, self._doc.__name__))
         return self._doc(doc, collection=col)
Exemple #3
0
    def __to_dict(self, obj):
        """Iterate over the nested object and convert it to an dict.
		"""

        d = {}
        for k in dir(obj):
            # ignore values with a beginning underscore. these are private.
            if k.startswith('_'): continue

            # get value an type
            value = getattr(obj, k)
            obj_type = type(value)

            # preocess Nested objects
            if obj_type == Nested:
                d[k] = self.__to_dict(value)
            # items
            elif obj_type == Doc:
                d[k] = DBRef(value._collection, value._id)
            # lists, that can consist of Nested objects,
            # Docs (references) or primitive values
            elif obj_type == list:
                d[k] = []
                for i in value:
                    if type(i) == Nested:
                        d[k].append(self.__to_dict(i))
                    elif type(i) == Doc:
                        d[k].append(DBRef(i._collection, i._id))
                    else:
                        d[k].append(i)
            # primitive values
            else:
                d[k] = value

        return d
Exemple #4
0
 def test_repr(self):
     self.assertEqual(
         repr(DBRef("coll", ObjectId("1234567890abcdef12345678"))),
         "DBRef(u'coll', ObjectId('1234567890abcdef12345678'))")
     self.assertEqual(
         repr(DBRef(u"coll", ObjectId("1234567890abcdef12345678"))),
         "DBRef(u'coll', ObjectId('1234567890abcdef12345678'))")
Exemple #5
0
 def test_creation(self):
     a = ObjectId()
     self.assertRaises(TypeError, DBRef)
     self.assertRaises(TypeError, DBRef, "coll")
     self.assertRaises(TypeError, DBRef, 4, a)
     self.assertRaises(TypeError, DBRef, 1.5, a)
     self.assertRaises(TypeError, DBRef, a, a)
     self.assertRaises(TypeError, DBRef, None, a)
     self.assert_(DBRef("coll", a))
     self.assert_(DBRef(u"coll", a))
     self.assert_(DBRef(u"coll", 5))
Exemple #6
0
    def test_deref(self):
        db = self.connection.pymongo_test
        db.test.remove({})

        self.assertRaises(TypeError, db.dereference, 5)
        self.assertRaises(TypeError, db.dereference, "hello")
        self.assertRaises(TypeError, db.dereference, None)

        self.assertEqual(None, db.dereference(DBRef("test", ObjectId())))
        obj = {"x": True}
        key = db.test.save(obj)
        self.assertEqual(obj, db.dereference(DBRef("test", key)))

        self.assertEqual(None, db.dereference(DBRef("test", 4)))
        obj = {"_id": 4}
        db.test.save(obj)
        self.assertEqual(obj, db.dereference(DBRef("test", 4)))
Exemple #7
0
 def get_dbref(self):
     """
     return a pymongo DBRef instance related to the document
     """
     assert '_id' in self, "You must specify an '_id' for using this method"
     return DBRef(database=self.db.name,
                  collection=self.collection.name,
                  id=self['_id'])
Exemple #8
0
 def get(self, username, type):
     user = User.one({'username': username})
     if not user:
         raise tornado.web.HTTPError(404)
     ctype = type[:-1] # articles, pages, projects, posts
     ctype = getattr(CONTENT_TYPE, ctype.upper())
     spec = {'type': ctype, 'status':'published', 'author': DBRef(User.collection_name, user._id)}
     pagination = Pagination(self, Content, spec)
     self.render('profile/items', pagination=pagination, user=user, type=type)
Exemple #9
0
 def transform_value(value):
     if isinstance(value, dict):
         if "_id" in value and "_ns" in value:
             return DBRef(value["_ns"], transform_value(value["_id"]))
         else:
             return transform_dict(SON(value))
     elif isinstance(value, list):
         return [transform_value(v) for v in value]
     return value
Exemple #10
0
    def test_from_xml(self):
        smorgasbord = """
<twonk>
  <meta/>
  <doc>
    <oid name="_id">285a664923b5fcd8ec000000</oid>
    <int name="the_answer">42</int>
    <string name="b">foo</string>
    <boolean name="c">true</boolean>
    <number name="pi">3.14159265358979</number>
    <array name="an_array">
      <string name="0">x</string>
      <string name="1">y</string>
      <string name="2">z</string>
      <doc name="3">
        <string name="subobject">yup</string>
      </doc>
    </array>
    <date name="now">123144452057</date>
    <ref name="dbref">
      <ns>namespace</ns>
      <oid>ca5c67496c01d896f7010000</oid>
    </ref>
    <regex name="regex">
      <pattern>foobar</pattern>
      <options>i</options>
    </regex>
    <code name="$where">this is code</code>
    <null name="mynull"/>
  </doc>
</twonk>
"""
        self.assertEqual(
            SON.from_xml(smorgasbord),
            SON([
                (u"_id",
                 ObjectId("\x28\x5A\x66\x49\x23\xB5\xFC"
                          "\xD8\xEC\x00\x00\x00")),
                (u"the_answer", 42),
                (u"b", u"foo"),
                (u"c", True),
                (u"pi", 3.14159265358979),
                (u"an_array",
                 [u"x", u"y", u"z",
                  SON([(u"subobject", u"yup")])]),
                (u"now", datetime.datetime(1973, 11, 26, 6, 47, 32, 57000)),
                (u"dbref",
                 DBRef(
                     "namespace",
                     ObjectId("\xCA\x5C\x67\x49\x6C\x01"
                              "\xD8\x96\xF7\x01\x00\x00"))),
                (u"regex", re.compile(u"foobar", re.IGNORECASE)),
                (u"$where", "this is code"),
                (u"mynull", None),
            ]))
Exemple #11
0
 def transform_value(value):
     if isinstance(value, Document):
         if value._id is None:
             value.save()
         return DBRef(value.objects.collection_name,
                      transform_value(value._id))
     elif isinstance(value, types.DictType):
         return transform_dict(value)
     elif isinstance(value, types.ListType):
         return [transform_value(v) for v in value]
     return value
Exemple #12
0
    def post(self):
        next = self.get_argument("next", None)
        text = self.get_argument("text")
        content_id = self.get_argument("content_id")
        
        comment = {"id": unicode(uuid.uuid4()), 
                   'user': DBRef(User.collection_name, self.current_user._id),
                   'created_at': datetime.datetime.utcnow(),
                   'text': sanitize(text),
                   }
        
        parent = self.get_argument("parent", None)
        if parent is None:
            comment.update({"responses": []})
            html = self.render_string("statics/item-comment", 
                                      user=self.current_user,
                                      comment=comment,
                                      commenters={self.current_user._id: self.current_user},
                                      item={'_id': content_id}
                                      )
            append_to = "#commentsbox"
        else:
            html = "<p>%s - %s</p>" % (comment['text'], self.current_user['username'])
            append_to = "#resp-" + parent
            
        js_add_comment = """
function add_resp(id, parent,comment) {
    spec = {_id:id}
    c = db.contents.findOne(spec);
    comments = c['comments'];
    if (parent) {
        idx = null;
        for(var i=0; i< comments.length; i++) {
            if (comments[i]['id'] == parent) {idx=i; break;}
        }
        if (idx != null) {
            comments[idx]['responses'].push(comment);
            c['comments'] = comments
            db.contents.save(c);
        }
    } else {
        db.contents.update(spec, {$push: {"comments": comment}});
    }
}        
        """
        Content.collection.database.eval(js_add_comment, content_id, parent, comment)
                    
        if self.is_xhr():
            data = {'append':True, 'html': html, 'target': append_to}
            return self.json_response(None, "OK", data)  
        else:
            self.redirect(self.get_argument('next'))
Exemple #13
0
 def test_cmp(self):
     self.assertEqual(DBRef("coll", ObjectId("1234567890abcdef12345678")),
                      DBRef(u"coll", ObjectId("1234567890abcdef12345678")))
     self.assertNotEqual(
         DBRef("coll", ObjectId("1234567890abcdef12345678")),
         DBRef("col", ObjectId("1234567890abcdef12345678")))
     self.assertNotEqual(
         DBRef("coll", ObjectId("1234567890abcdef12345678")),
         DBRef("coll", ObjectId("123456789011")))
     self.assertNotEqual(
         DBRef("coll", ObjectId("1234567890abcdef12345678")), 4)
Exemple #14
0
    def test_read_only(self):
        a = DBRef("coll", ObjectId())

        def foo():
            a.collection = "blah"

        def bar():
            a.id = "aoeu"

        a.collection
        a.id
        self.assertRaises(AttributeError, foo)
        self.assertRaises(AttributeError, bar)
Exemple #15
0
def object_hook(dct):
    if "$oid" in dct:
        return ObjectId(str(dct["$oid"]))
    if "$ref" in dct:
        return DBRef(dct["$ref"], dct["$id"], dct.get("$db", None))
    if "$date" in dct:
        return datetime.datetime.utcfromtimestamp(float(dct["$date"]) / 1000.0)
    if "$regex" in dct:
        flags = 0
        if "i" in dct["$options"]:
            flags |= re.IGNORECASE
        if "m" in dct["$options"]:
            flags |= re.MULTILINE
        return re.compile(dct["$regex"], flags)
    return dct
Exemple #16
0
    def update(self):
        log = logging.getLogger('django_vcs_watch.feed.update')
        log.debug('updating %s' % self._id)

        if self.ignore is None:
            self.ignore = []

        if self.watch is None:
            self.watch = []

        if self.num_items is None:
            self.num_items = 0

        watch_query = ' || '.join(' && '.join("this.%s == '%s'" % item
                                              for item in rule.items())
                                  for rule in self.watch)

        ignore_query = ' || '.join(' && '.join("this.%s == '%s'" % item
                                               for item in rule.items())
                                   for rule in self.ignore)

        if not watch_query:
            return

        query = {
            '$where': '(%s) && !(%s)' % (watch_query, ignore_query or '0')
        }

        last_item = FeedItem.objects.find_one(dict(slug=self._id))

        if last_item is not None:
            query['date'] = {'$gt': last_item.date}

        Commit.objects.ensure_index([('slug', 1)])
        Commit.objects.ensure_index([('author', 1)])

        from pymongo.dbref import DBRef

        for commit in Commit.objects.find(query):
            commit_ref = DBRef(commit.objects.collection_name, commit._id)
            if FeedItem.objects.find_one(dict(slug=self._id,
                                              commit=commit_ref)) is not None:
                logging.error('UPS I DID IT AGAIN!')

            FeedItem(slug=self._id, date=commit.date, commit=commit).save()
            self.num_items += 1

        self.save()
Exemple #17
0
def _get_ref(data):
    (collection, data) = _get_c_string(data[4:])
    (oid, data) = _get_oid(data)
    return (DBRef(collection, oid), data)
Exemple #18
0
def _get_object(data):
    (object, data) = _bson_to_dict(data)
    if "$ref" in object:
        return (DBRef(object["$ref"], object["$id"], object.get("$db",
                                                                None)), data)
    return (object, data)
Exemple #19
0
 def _set_callback(self, instance, value):
     """ Resolves a Model to a DBRef """
     if value:
         value = DBRef(self.model._get_name(), value.id)
     return value
Exemple #20
0
 def get_ref(self):
     """ Returns a DBRef for an document. """
     return DBRef(self._get_name(), self._get_id())
Exemple #21
0
 def make_ref(cls, idval):
     """ Generates a DBRef for a given id. """
     if type(idval) != cls._id_type:
         # Casting to ObjectId (or str, or whatever is configured)
         idval = cls._id_type(idval)
     return DBRef(cls._get_name(), idval)
Exemple #22
0
	
	# Load in original html using db.fs.get(uid)
	orig_doc = fs.get(uid)
	
	# Create a new dictionary for case object with fields from db.fs.files
	# (filename, media_type, tags, url, year) because don't want to need reference
	# to the GridFS file for this info
	case = {}
	case['filename'] = case_ref['filename']
	case['url'] = case_ref['url']
	case['media_type'] = case_ref['media_type']
	case['tags'] = case_ref['tags']
	case['year'] = case_ref['year']
	
	# Add DBRef to original file for further future processing
	case['file_ref'] = DBRef('fs.files', uid)
	
	# Example of dereferencing that file
	# cr = db.docs.find_one()
	# fs_file_ref = db.dereference(cr['file_ref'])
	# html = fs.get(cr['file_ref'].id).read()
	
	# NOTE: Before try to pull out gsl_opinion, BeautifulSoup has trouble
	# properly closing <center> tags which have child elements...
	# So, replace <center>...</center> with <div class="former_center">...</div>
	# then create soup
	html = orig_doc.read()
	soup = BeautifulSoup(html)
	
	# Pull out "content" from <div id="gsl_opinion" and from that...
	content_div = soup.find('div', {"id" : "gsl_opinion"})
Exemple #23
0
    def test_basic_from_dict(self):
        self.assertRaises(TypeError, BSON.from_dict, 100)
        self.assertRaises(TypeError, BSON.from_dict, "hello")
        self.assertRaises(TypeError, BSON.from_dict, None)
        self.assertRaises(TypeError, BSON.from_dict, [])

        self.assertEqual(BSON.from_dict({}), BSON("\x05\x00\x00\x00\x00"))
        self.assertEqual(
            BSON.from_dict({"test": u"hello world"}),
            "\x1B\x00\x00\x00\x02\x74\x65\x73\x74\x00\x0C\x00\x00"
            "\x00\x68\x65\x6C\x6C\x6F\x20\x77\x6F\x72\x6C\x64\x00"
            "\x00")
        self.assertEqual(
            BSON.from_dict({u"mike": 100}),
            "\x0F\x00\x00\x00\x10\x6D\x69\x6B\x65\x00\x64\x00\x00"
            "\x00\x00")
        self.assertEqual(
            BSON.from_dict({"hello": 1.5}),
            "\x14\x00\x00\x00\x01\x68\x65\x6C\x6C\x6F\x00\x00\x00"
            "\x00\x00\x00\x00\xF8\x3F\x00")
        self.assertEqual(BSON.from_dict({"true": True}),
                         "\x0C\x00\x00\x00\x08\x74\x72\x75\x65\x00\x01\x00")
        self.assertEqual(
            BSON.from_dict({"false": False}),
            "\x0D\x00\x00\x00\x08\x66\x61\x6C\x73\x65\x00\x00"
            "\x00")
        self.assertEqual(
            BSON.from_dict({"empty": []}),
            "\x11\x00\x00\x00\x04\x65\x6D\x70\x74\x79\x00\x05\x00"
            "\x00\x00\x00\x00")
        self.assertEqual(
            BSON.from_dict({"none": {}}),
            "\x10\x00\x00\x00\x03\x6E\x6F\x6E\x65\x00\x05\x00\x00"
            "\x00\x00\x00")
        self.assertEqual(
            BSON.from_dict({"test": Binary("test")}),
            "\x18\x00\x00\x00\x05\x74\x65\x73\x74\x00\x08\x00\x00"
            "\x00\x02\x04\x00\x00\x00\x74\x65\x73\x74\x00")
        self.assertEqual(
            BSON.from_dict({"test": Binary("test", 128)}),
            "\x14\x00\x00\x00\x05\x74\x65\x73\x74\x00\x04\x00\x00"
            "\x00\x80\x74\x65\x73\x74\x00")
        self.assertEqual(BSON.from_dict({"test": None}),
                         "\x0B\x00\x00\x00\x0A\x74\x65\x73\x74\x00\x00")
        self.assertEqual(
            BSON.from_dict({"date": datetime.datetime(2007, 1, 8, 0, 30, 11)}),
            "\x13\x00\x00\x00\x09\x64\x61\x74\x65\x00\x38\xBE\x1C"
            "\xFF\x0F\x01\x00\x00\x00")
        self.assertEqual(
            BSON.from_dict({"regex": re.compile("a*b", re.IGNORECASE)}),
            "\x12\x00\x00\x00\x0B\x72\x65\x67\x65\x78\x00\x61\x2A"
            "\x62\x00\x69\x00\x00")
        self.assertEqual(
            BSON.from_dict({"$where": Code("test")}),
            "\x1F\x00\x00\x00\x0F\x24\x77\x68\x65\x72\x65\x00\x12"
            "\x00\x00\x00\x05\x00\x00\x00\x74\x65\x73\x74\x00\x05"
            "\x00\x00\x00\x00\x00")
        a = ObjectId("\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B")
        self.assertEqual(
            BSON.from_dict({"oid": a}),
            "\x16\x00\x00\x00\x07\x6F\x69\x64\x00\x00\x01\x02\x03"
            "\x04\x05\x06\x07\x08\x09\x0A\x0B\x00")
        self.assertEqual(
            BSON.from_dict({"ref": DBRef("coll", a)}),
            "\x2F\x00\x00\x00\x03ref\x00\x25\x00\x00\x00\x02$ref"
            "\x00\x05\x00\x00\x00coll\x00\x07$id\x00\x00\x01\x02"
            "\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x00\x00")
Exemple #24
0
 def comments(self):
     return Comment.all({
         'entry': DBRef(self.collection_name, self['_id'])
     }).sort('date_added', pymongo.ASCENDING)
Exemple #25
0
# connect
connection = Connection()
db = Database(connection, "things")

# clean up
db.owners.remove()
db.tasks.remove()

# owners and tasks
db.owners.insert({"name": "Jim"})
db.tasks.insert([{"name": "read"}, {"name": "sleep"}])

# update jim with tasks: reading and sleeping
reading_task = db.tasks.find_one({"name": "read"})
sleeping_task = db.tasks.find_one({"name": "sleep"})

jim_update = db.owners.find_one({"name": "Jim"})
jim_update["tasks"] = [
    DBRef(collection="tasks", id=reading_task["_id"]),
    DBRef(collection="tasks", id=sleeping_task["_id"])
]

db.owners.save(jim_update)

# get jim fresh again and display his tasks
fresh_jim = db.owners.find_one({"name": "Jim"})
print "Jim's tasks are:"
for task in fresh_jim["tasks"]:
    print db.dereference(task)["name"]
Exemple #26
0
def gen_dbref():
    collection = gen_unicode(gen_range(0, 20))
    return lambda: DBRef(collection(), gen_mongo_value(1, True)())
Exemple #27
0
 def get_comments_for(self, user):
     spec = {'for': DBRef(User.collection_name, user._id)}
     return Pagination(self, Comment, spec)
Exemple #28
0
 def to_bson(self, value):
     if value is not None:
         return DBRef(database=value.db.name,
                      collection=value.collection.name,
                      id=value['_id'])