Example #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)
def _get_object(data, view, position, obj_end, opts, dummy):
    """Decode a BSON subdocument to opts.document_class or bson.dbref.DBRef."""
    obj_size, end = _get_object_size(data, position, obj_end)
    if _raw_document_class(opts.document_class):
        return (opts.document_class(data[position:end + 1],
                                    opts), position + obj_size)

    obj = _elements_to_dict(data, view, position + 4, end, opts)

    position += obj_size
    # If DBRef validation fails, return a normal doc.
    if (isinstance(obj.get('$ref'), str) and "$id" in obj
            and isinstance(obj.get('$db'), (str, type(None)))):
        return (DBRef(obj.pop("$ref"), obj.pop("$id", None),
                      obj.pop("$db", None), obj), position)
    return obj, position
Example #3
0
    def setXX(self):
        self.ocur.callproc('dbms_mview.refresh', ['mv_user_news', 'c'])
        rows = self.ocur.execute('select * from mv_user_news').fetchall()
        if not rows: return

        c = self.mdb.xx
        for r in rows:
            l = {}
            l['_id'] = r[2]
            l['userid'] = r[0]
            l['newsid'] = DBRef('ggxx', r[1])
            c.insert(l)

        lastid, = self.ocur.execute(
            'select nvl(max(id),0) from mv_user_news').fetchone()
        self.setConf('xx', lastid)
Example #4
0
    def post(self):
        form = ThemeSetForm(self.request.arguments)
        if not form.validate():
            raise HTTPError(404)

        theme = form.theme.data

        yield UserSettingDocument.update(
            {'user': DBRef(
                UserDocument.meta['collection'],
                ObjectId(self.current_user['_id'])
            )},
            {'$set': {'theme': theme}}
        )

        self.finish()
Example #5
0
    def post(self):
        form = NotificationSetForm(self.request.arguments)
        if not form.validate():
            raise HTTPError(404)

        email_notify_when_offline = form.email_notify_when_offline.data

        yield UserSettingDocument.update(
            {'user': DBRef(
                UserDocument.meta['collection'],
                ObjectId(self.current_user['_id'])
            )},
            {'$set': {'email_notify_when_offline': email_notify_when_offline}}
        )

        self.finish()
Example #6
0
    def test_handle_auto_object_inside_a_list(self):
        parent = self.model.get_or_create({'test': 'hellotest'})
        child = self.model.create(test="testing",
                                  parents=[parent],
                                  parent=parent)

        child = self.model.query.find_one({"test": "testing"})
        assert child.parents[0].test == "hellotest"
        assert child.parents[0].__class__.__name__ == self.model.__name__
        assert isinstance(child, self.model)
        assert isinstance(child.parents[0], self.model)

        parent = self.model.create(test="test_two")
        child = child.update_with_reload(
            {'parents': [DBRef(parent.__collection__, parent._id)]})
        assert child.parents[0].test == "test_two"
Example #7
0
    def to_document(self, display_only=False):
        doc = MBSTask.to_document(self, display_only=display_only)
        doc.update({
            "_type":
            "Restore",
            "sourceBackup":
            DBRef("backups", self.source_backup.id),
            "sourceDatabaseName":
            self.source_database_name,
            "destination":
            self.destination.to_document(display_only=display_only),
            "destinationStats":
            self.destination_stats
        })

        return doc
Example #8
0
def test_unwrap():
    class A(Document):
        x = IntField()
    s = get_session()
        
    a = A(x=5)
    s.insert(a)
    
    aref = {'$id':a.mongo_id, '$ref':'A'}
    dbaref = DBRef(db='unit-testing', collection='A', id=a.mongo_id)

    ret = RefField(DocumentField(A)).unwrap(dbaref)
    assert isinstance(ret, DBRef), ret

    ret = SRefField(A).unwrap(a.mongo_id)
    assert isinstance(ret, ObjectId), ret
 def serialize_document_field(self, field_name, field_value, **kwargs):
     """If this field is a reference or an embedded document, either return
     a DBRef or serialize it using a resource found in `related_resources`.
     """
     if DocumentProxy and isinstance(field_value, DocumentProxy):
         # Don't perform a DBRef isinstance check below since
         # it might trigger an extra query.
         return field_value.to_dbref()
     if isinstance(field_value, DBRef):
         return field_value
     if isinstance(field_value, PyMongoReference):
         if field_value.document_cls.opts.collection_name:
             return DBRef(field_value.document_cls.opts.collection_name,
                          field_value.pk)
         else:
             return field_value.pk
     return field_value and field_value.to_dbref()
Example #10
0
    def get_recommend_friends(user_id, size=5):
        '''得到推荐的朋友

        :Parameters:
          - `user_id`: 为谁推荐?
          - `size`: 推荐数量
        '''
        from app.message.document import MessageDocument

        user = yield UserDocument.find_one({'_id': ObjectId(user_id)})
        if not user:
            raise gen.Return([])

        friend_list = yield FriendDocument.get_friend_list(user['_id'])
        friend_objectid_list = [user['_id']
                                ] + [friend['_id'] for friend in friend_list]

        cursor = MessageDocument.find({
            'sender':
            DBRef(UserDocument.meta['collection'], ObjectId(user_id)),
            'message_type':
            MessageTopic.FRIEND_REQUEST_NEW
        })

        message_list = yield MessageDocument.to_list(cursor)
        for message in message_list:
            friend_objectid_list.append(ObjectId(message['recipient'].id))

        query = {
            '_id': {
                '$nin': friend_objectid_list
            },
            'activated': True,
            'avatar_updated': True
        }

        cursor = UserDocument.find(query)

        count = yield UserDocument.find(query).count()
        if count > size:
            cursor = cursor.skip(random.randint(0, count - size))

        cursor = cursor.limit(size)
        user_list = yield UserDocument.to_list(cursor)

        raise gen.Return(user_list)
Example #11
0
def addEntry(jsonData,tags,autoThrashed):
    a = mydb.userInfo.find_one({"name":jsonData["name"]})
    newDbref = DBRef("mydb.userInfo",a["_id"])
    scan_date = datetime.datetime.today()
    scan_date = scan_date + datetime.timedelta(hours=9)
    end_date = scan_date + datetime.timedelta(days=10)
    scan_date = str(scan_date.day) +"-"+ str(scan_date.month)+"-" + str(scan_date.year)
    end_date = str(end_date.day) +"-" +str(end_date.month)+"-" + str(end_date.year)
    if(autoThrashed):
        end_date = scan_date
    if( not autoThrashed and len(tags) >= 3):
        #mydb.mltable.insert({"code":jsonData["code"],"tags": tags,"status":"Keep","user_id":1,"otherdbref":newDbref})  Actual Code
        mydb.mltable.insert({"code":jsonData["code"],"tags": tags,"status":"Keep","user_id":1,"otherdbref":newDbref})#Test code to be removed
        #end_date = scan_date
    mydb.userMailInfo.insert({"code":jsonData["code"],"scan_date":scan_date,"end_date":end_date,"otherdbref":newDbref,"userDeleted":False,"user_id":1,"source":jsonData["source"]})
    jsonData["autoThrashed"] = autoThrashed
    return json.dumps(jsonData)
    def test_cursor(self):
        db = self.db

        db.drop_collection("test")
        docs = [
            {'foo': [1, 2]},
            {'bar': {'hello': 'world'}},
            {'code': Code("function x() { return 1; }")},
            {'bin': Binary(b"\x00\x01\x02\x03\x04", USER_DEFINED_SUBTYPE)},
            {'dbref': {'_ref': DBRef('simple',
                               ObjectId('509b8db456c02c5ab7e63c34'))}}
        ]

        db.test.insert_many(docs)
        reloaded_docs = json_util.loads(json_util.dumps(db.test.find()))
        for doc in docs:
            self.assertTrue(doc in reloaded_docs)
Example #13
0
    def get_last_comment(topic_id):
        '''得到某一话题的最后一个回复'''

        cursor = TopicCommentDocument.find({
            'topic':
            DBRef(TopicDocument.meta['collection'], ObjectId(topic_id))
        }).sort([('comment_time', pymongo.DESCENDING)]).limit(1)

        comment_list = yield TopicCommentDocument.to_list(cursor)

        last_comment = None
        if comment_list:
            last_comment = comment_list[0]
            last_comment['author'] = yield UserDocument.translate_dbref(
                last_comment['author'])

        raise gen.Return(last_comment)
def update_user_roles():
    for user in db.users.find():
        changes = {}
        if not "roles" in user:
            changes["roles"] = [DBRef("roles", ObjectId("58542d56861bd736c42a0202"))]
        if not "language" in user:
            changes["language"] = "PT_BR"
        if not "dateformat" in user:
            changes["dateformat"] = "DDMMYYYY"
        if not "zoneId" in user:
            changes["zoneId"] = "AMERICA_SAO_PAULO"

        if changes:
            print "Updating roles for {}: {}".format( user[u'_id'], str(changes))
            db.users.update({"_id": user[u'_id']},
                            {"$set": changes},
                            multi=False)
    return True
Example #15
0
 def test_skeleton_types(self):
     # ensure that all valid BSON types can be
     # skeleton'd; lists and subobjects are
     # tested in other functions and omitted here
     self.assertEqual(s.skeleton({'a': 1}), '{a}')
     self.assertEqual(s.skeleton({'a': 1L}), '{a}')
     self.assertEqual(s.skeleton({'a': 1.0}), '{a}')
     self.assertEqual(s.skeleton({'a': '1'}), '{a}')
     self.assertEqual(s.skeleton({'a': u'1'}), '{a}')
     self.assertEqual(s.skeleton({'a': True}), '{a}')
     self.assertEqual(s.skeleton({'a': datetime.now()}), '{a}')
     self.assertEqual(
         s.skeleton({'a': ObjectId('000000000000000000000000')}), '{a}')
     self.assertEqual(s.skeleton({'a': re.compile(r'^$')}), '{a}')
     self.assertEqual(s.skeleton({'a': Code('function(){}')}), '{a}')
     self.assertEqual(s.skeleton({'a': None}), '{a}')
     self.assertEqual(s.skeleton({'a': Binary('123456')}), '{a}')
     self.assertEqual(s.skeleton({'a': DBRef('coll', 123)}), '{a}')
Example #16
0
def add_one_profile_cover(img_path, name):
    '''事先填充个人封面图片数据库'''

    document = {}
    document['name'] = name
    document['content_type'] = 'JPEG'

    existed = yield ImageDocument.find_one(document)
    if existed:
        return

    path = os.path.join(img_path, name)
    image = Image.open(path)

    scale = image.size[0] * 1.0 / 960
    width = int(image.size[0])
    height = int(300 * scale)
    box = (0, 0, width, height)

    image = image.crop(box)
    image = image.resize((969, 300), Image.ANTIALIAS)

    output = StringIO()
    image.save(output, document['content_type'], quality=100)
    document['body'] = Binary(output.getvalue())
    output.close()

    image = Image.open(path)
    image = image.resize((260, 160), Image.ANTIALIAS)

    output = StringIO()
    image.save(output, document['content_type'], quality=100)
    document['thumbnail'] = Binary(output.getvalue())
    output.close()

    image_id = yield ImageDocument.insert(document)
    document = {
        'image': DBRef(ImageDocument.meta['collection'], ObjectId(image_id))
    }

    yield OfficialProfileCoverDocument.insert(document)

    raise gen.Return()
Example #17
0
 def get_back_links(self, resolve=False, collection=None):
     """
     Enumerates all reverse references
     """
     self._logger.debug("Arguments to function '{}".format(
         self._debug_function()))
     if not self._id:
         self._logger.error("Was object deleted?")
         return None
     usedby_doc = self._mongo_collection.find_one({'_id': self._id}, {
         usedby_key: 1,
         '_id': 0
     })
     try:
         usedby_doc = usedby_doc[usedby_key]
     except:
         return []
     if bool(collection):
         try:
             collection_objs = usedby_doc.pop('collection')
         except:
             collection_objs = {}
         usedby_doc = {}
         usedby_doc['collection'] = collection_objs
     output = []
     for col_iter in usedby_doc:
         for uid in usedby_doc[col_iter]:
             dbref = DBRef(col_iter, ObjectId(uid))
             remote_mongo_collection = self._mongo_db[dbref.collection]
             if not resolve:
                 name = str(dbref.id)
             else:
                 try:
                     name = remote_mongo_collection.find_one(
                         {'_id': dbref.id})['name']
                 except:
                     name = str(dbref.id)
             output.extend([{
                 'collection': dbref.collection,
                 'name': name,
                 'DBRef': dbref
             }])
     return output
Example #18
0
    def get_friend_list(user_id, skip=0, limit=None):
        '''得到某一个人的朋友列表

        :Parameters:
          - `user_id`: 相关用户
        '''

        owner = DBRef(UserDocument.meta['collection'], ObjectId(user_id))
        cursor = FriendDocument.find({
            'owner': owner
        }).sort([('be_time', pymongo.DESCENDING)]).skip(skip)

        if limit is not None:
            cursor = cursor.limit(limit)

        friends = yield FriendDocument.to_list(cursor)
        friend_list = yield FriendDocument._gen_friend_list(friends, "friend")

        raise gen.Return(friend_list)
Example #19
0
def _get_object(data, position, obj_end, opts, element_name):
    """Decode a BSON subdocument to opts.document_class or bson.dbref.DBRef."""
    obj_size = _UNPACK_INT(data[position:position + 4])[0]
    end = position + obj_size - 1
    if data[end:position + obj_size] != b"\x00":
        raise InvalidBSON("bad eoo")
    if end >= obj_end:
        raise InvalidBSON("invalid object length")
    if _raw_document_class(opts.document_class):
        return (opts.document_class(data[position:end + 1], opts,
                                    element_name), position + obj_size)

    obj = _elements_to_dict(data, position + 4, end, opts)

    position += obj_size
    if "$ref" in obj:
        return (DBRef(obj.pop("$ref"), obj.pop("$id", None),
                      obj.pop("$db", None), obj), position)
    return obj, position
Example #20
0
	def update_activity_queue(self, action,company_id, benefit_id):
		from bson.dbref import DBRef 

		company = self.db.company_queue.find_one({'_id': company_id})
		dbref_obj = DBRef('benefits', benefit_id)
		if action == "reserve":
			company['queue'].append(dbref_obj)
		else: 
			if dbref_obj in company['queue']:
				for i in range(len(company['queue'])):
					if company['queue'][i] == dbref_obj:
						company['queue'].pop(i)
						break
					else: 
						pass	 


		self.db.company_queue.save(company)
		return None
    def save_person(self, person):
        person_stored = self.get_object('person', 'originalId',
                                        person.originalId)

        person_dict = person.dict()

        # setting body
        person_dict['body'] = DBRef(collection='body', id=self.body_uid)

        # ensure that there is an originalId
        if 'originalId' not in person_dict:
            logging.critical("Fatal error: no originalId avaiable at url %s",
                             person_dict.originalUrl)

        # dereference objects
        person_dict = self.dereference_object(person_dict, 'membership')

        # save data
        return self.save_object(person_dict, person_stored, 'person')
Example #22
0
    def remove_one(query):
        like = yield ShareLikeDocument.find_one(query)
        if like:
            yield ShareLikeDocument.remove(query)

            like_times = yield ShareLikeDocument.get_like_times(
                like['share'].id)
            yield ShareDocument.update({'_id': ObjectId(like['share'].id)},
                                       {'$set': {
                                           'like_times': like_times
                                       }})

            yield MessageDocument.remove({
                'data':
                DBRef(ShareLikeDocument.meta['collection'],
                      ObjectId(like['_id']))
            })

        raise gen.Return()
Example #23
0
    def get_reached_friends_sync(user_id, skip=0, limit=None):
        user = DBRef(UserDocument.meta['collection'], ObjectId(user_id))

        cursor = FriendDocument.get_collection(True).find({
            'owner': user,
            'shielded': False,
            'blocked': False
        })
        friend_list = set(item['friend'] for item in cursor)

        cursor = FriendDocument.get_collection(True).find({
            'friend': user,
            'shielded': False,
            'blocked': False
        })
        for item in cursor:
            friend_list.add(item['owner'])

        return list(friend_list)
Example #24
0
    def get_comment_list(share_id, skip=0, limit=None):
        cursor = ShareCommentDocument.find({
            'share':
            DBRef(ShareDocument.meta['collection'], ObjectId(share_id))
        }).sort([('comment_time', pymongo.ASCENDING)]).skip(skip)

        if limit is not None:
            cursor = cursor.limit(limit)

        comment_list = yield ShareCommentDocument.to_list(cursor)

        for i, comment in enumerate(comment_list):
            comment['floor'] = skip + 1 + i
            comment['author'] = yield UserDocument.translate_dbref(
                comment['author'])
            if 'replyeder' in comment:
                comment['replyeder'] = yield UserDocument.translate_dbref(
                    comment['replyeder'])

        raise gen.Return(comment_list)
    def save_organization(self, organization):
        organization_stored = self.get_object('organization', 'originalId',
                                              organization.originalId)
        organization_dict = organization.dict()

        # setting body
        organization_dict['body'] = DBRef(collection='body', id=self.body_uid)

        # ensure that there is an originalId
        if 'originalId' not in organization_dict:
            logging.critical("Fatal error: no originalId avaiable at url %s",
                             organization_dict.originalUrl)

        # create slug
        organization_dict['slug'] = self.create_slug(organization_dict,
                                                     'committee')

        # save data
        return self.save_object(organization_dict, organization_stored,
                                'organization')
Example #26
0
    def get(self, cover_type, id_, content_type=None):
        if cover_type == 'user':
            user_setting = yield UserSettingDocument.find_one({
                'user':
                DBRef(UserDocument.meta['collection'], ObjectId(id_))
            })

            profile_cover = yield OfficialProfileCoverDocument.find_one(
                {'_id': ObjectId(user_setting['profile_cover'].id)})
        else:
            profile_cover = yield OfficialProfileCoverDocument.find_one(
                {'_id': ObjectId(id_)})

        content = str(profile_cover['content'])
        if content_type is not None:
            content = str(
                profile_cover['profile_content'] if content_type ==
                'profile-content' else profile_cover['setting_content'])

        self.finish(content)
Example #27
0
    def get_chat_message_list(user_id, skip=0, limit=None):
        '''得到与某人有关的私聊信息'''

        user_dbref = DBRef(UserDocument.meta['collection'], ObjectId(user_id))
        query = {
            '$or': [{'sender': user_dbref}, {'recipient': user_dbref}]
        }

        cursor = ChatMessageDocument.find(query).sort(
            [('send_time', pymongo.DESCENDING)]
        ).skip(skip)

        if limit is not None:
            cursor = cursor.limit(limit)

        chat_message_list = yield ChatMessageDocument.to_list(cursor)
        chat_message_list = yield ChatMessageDocument.translate_dbref_in_document_list(
            chat_message_list)

        raise gen.Return(chat_message_list)
Example #28
0
def checkIfAutoThrashed(jsonData,tags):
    if(len(tags) < 3):
        return False
    a = mydb.userInfo.find_one({"name":jsonData["name"]})
    newDbref = DBRef("mydb.userInfo",a["_id"])
    foundMails = mydb.mltable.find({"otherdbref":newDbref,"status":"trash"})
    foundMailsList = list(mydb.mltable.find({"otherdbref":newDbref,"status":"trash"}))
    if(len(foundMailsList) < 10):
        return False
    tagcount = 0
    thrashcount = 0
    for item in foundMails:
        for tag in tags:
            if(tag in item["tags"]):
                tagcount+=1
        if(tagcount >= 3):
            thrashcount+=1
    if(thrashcount >=10):
        return True
    return False
    def add_law(self, dataset, law, jurisdictions, effective, through, title):
        #add law
        #logging.info("Add Law")
        jurisdictions_dbrefs = []
        jurisdiction_names = ''
        for jurisdiction in jurisdictions:
            if jurisdiction is not None:
                jurisdiction_names = jurisdiction_names + jurisdiction[
                    "name"] + " - "
                jurisdictions_dbrefs.append(
                    DBRef('Jurisdiction', ObjectId(jurisdiction["_id"]),
                          "sym_lawatlas_api_dev"))
        #ready the law, the insert and update
        law_html = self.html_entity_decode(law)
        law_text = self.remove_tags(law_html)
        law_id = self.my_symfony_db.Law.insert({
            "text":
            law_text,
            "html":
            law_html,
            "datasets":
            self.datasets,
            "jurisdictions":
            jurisdictions_dbrefs,
            "version":
            0,
            "effective":
            effective,
            "through":
            through,
            "title":
            title + jurisdiction_names
        })

        #update with series_root equals _id
        updated_series = self.my_symfony_db.Law.update(
            {"_id": ObjectId(law_id)}, {"$set": {
                "series_root": str(law_id)
            }})

        return law_id
Example #30
0
def query_measurements(unis_ids, event_types, meta_filter=None, data_filter=None):
    """
    Queries mongodb for measurements data by unis_id, and event_types
    """
    
    if not meta_filter:
        meta_filter = {}
    if not data_filter:
        data_filter = {}
    
    if not isinstance(meta_filter, dict):
        raise ValueError("meta_filter should be dict.")
    
    if not isinstance(data_filter, dict):
        raise ValueError("data_filter should be dict.")
    
    if not isinstance(event_types, list):
        event_types = [event_types]
    if not isinstance(unis_ids, list):
        unis_ids = [unis_ids]
    
    mongodb = get_mongodb()
    results = {'meta': {}, 'data': {}}
    
    meta_filter['unis_id'] = {'$in': unis_ids}
    meta_filter['event_type']= {'$in': event_types}
    
    metas = mongodb.metadata.find(meta_filter)
    results['meta'] = dict([(str(v['_id']), v) for v in metas])
    meta_refs = [DBRef('metadata', v.pop('_id')) for v in results['meta'].values()]
    results['data'] = dict([(meta_id, []) for meta_id in results['meta'].keys()])
    
    data_filter['meta_ref'] = {'$in': meta_refs}
    data = mongodb.measurements.find(data_filter, {'_id': 0})
    del meta_refs
    
    for datum in data:
        meta_ref = str(datum.pop('meta_ref').id)
        results['data'][meta_ref].append(datum)
    
    return results
Example #31
0
    def delete_one(comment_id):
        comment = yield ShareCommentDocument.find_one(
            {'_id': ObjectId(comment_id)})
        if comment:
            yield ShareCommentDocument.remove({'_id': ObjectId(comment_id)})

            comment_times = yield ShareCommentDocument.get_comment_times(
                comment['share'].id)
            yield ShareDocument.update(
                {'_id': ObjectId(comment['share'].id)},
                {'$set': {
                    'comment_times': comment_times
                }})

            yield MessageDocument.remove({
                'data':
                DBRef(ShareCommentDocument.meta['collection'],
                      ObjectId(comment_id))
            })

        raise gen.Return()