Beispiel #1
0
    def test_equality(self):
        john_data = {
            '_id': 42,
            'name': 'John Doe',
            'birthday': datetime(1995, 12, 12),
            'gpa': 3.0
        }
        john = self.EasyIdStudent.build_from_mongo(data=john_data)
        john2 = self.EasyIdStudent.build_from_mongo(data=john_data)
        phillipe = self.EasyIdStudent.build_from_mongo(
            data={
                '_id': 3,
                'name': 'Phillipe J. Fry',
                'birthday': datetime(1995, 12, 12),
                'gpa': 3.0
            })

        assert john != phillipe
        assert john2 == john
        assert john == DBRef(collection='student', id=john.pk)

        john.name = 'William Doe'
        assert john == john2

        newbie = self.EasyIdStudent(name='Newbie')
        newbie2 = self.EasyIdStudent(name='Newbie')
        assert newbie != newbie2
Beispiel #2
0
def create_tenant(args, name):
    db = db_connect()
    if args.org:
        org = args.org
    else:
        org = domain_generator()
    tenant = tenant_find_by_domain(org)
    print("Info: Organization name = " + org)
    if tenant is None:
        try:
            inserted_id = db.tenants.insert_one({"name": name, "domainName": org}).inserted_id
            db.applications.insert_one({
                "_id" : org,
                "friendlyName" : org,
                "description" : "",
                "qualifier" : "brsp01a",
                "registrationDate" : int(datetime.datetime.now().strftime("%s")) * 1000,
                "tenant" : DBRef("tenants", inserted_id)
            })
            return inserted_id
        except Exception as e:
            print(e)
            sys.exit(1)
    else:
        return tenant['_id']
    def test_lazy_reference_bad_set(self):
        class Animal(Document):
            name = StringField()
            tag = StringField()

        class Ocurrence(Document):
            person = StringField()
            animal = LazyReferenceField(Animal)

        Animal.drop_collection()
        Ocurrence.drop_collection()

        class BadDoc(Document):
            pass

        animal = Animal(name="Leopard", tag="heavy").save()
        baddoc = BadDoc().save()
        for bad in (
                42,
                "foo",
                baddoc,
                DBRef(baddoc._get_collection_name(), animal.pk),
                LazyReference(BadDoc, animal.pk),
        ):
            with pytest.raises(ValidationError):
                Ocurrence(person="test", animal=bad).save()
Beispiel #4
0
    def to_mongo(self, document):
        if isinstance(document, DBRef):
            if not self.dbref:
                return DBRef.id
            return document
        elif not self.dbref and isinstance(document, basestring):
            return document

        id_field_name = self.document_type._meta['id_field']
        id_field = self.document_type._fields[id_field_name]

        if isinstance(document, Document):
            # We need the id from the saved object to create the DBRef
            id_ = document.pk
            if id_ is None:
                self.error('You can only reference documents once they have'
                           ' been saved to the database')
        else:
            id_ = document

        id_ = id_field.to_mongo(id_)
        if self.dbref:
            collection = self.document_type._get_collection_name()
            return DBRef(collection, id_)

        return id_
Beispiel #5
0
 def post(self, *args, **kwargs):
     object_parent_id = ""
     classify_name = self.get_argument("classify_name")
     classify_url = self.get_argument("classify_url")
     parent_classify_id = self.get_argument("classify_parent")
     if parent_classify_id != "":
         object_parent_id = ObjectId(parent_classify_id)
     right_classify_item = {
         "classify_name": classify_name,
         "classify_url": classify_url,
         "classify_color": "default",
         "classify_parent": object_parent_id,
         "classify_son": []
     }
     # 更新父类
     right_classify_item_id = self.db.right_classify.insert_one(
         right_classify_item).inserted_id
     if right_classify_item_id:
         self.db.right_classify.update({"_id": parent_classify_id}, {
             "$push": {
                 "classify_son": DBRef('right_classify',
                                       right_classify_item_id)
             }
         },
                                       multi=True)
         self.redirect("/")
Beispiel #6
0
def serviceto_role():
    _json = request.json
    _role_name = _json['role_name']
    _service_name = _json['service_name']
    existing_role = mongo.db.roles.find_one({'name': _role_name})
    existing_service = mongo.db.services.find_one({
        'name': _service_name,
    })
    # Add new service if not exist
    if (existing_service is None) and _role_name:
        mongo.db.services.insert({'name': _service_name, 'description': ''})
    # Add new role if not exist
    if (existing_role is None) and _service_name:
        mongo.db.roles.insert({
            'name': _role_name,
            'description': '',
            'services': []
        })
    # check if Role already has the service
    if _role_name and existing_service:
        existing_serviced = mongo.db.roles.find_one({
            'name':
            _role_name,
            'services':
            DBRef(collection="services", id=existing_service['_id'])
        })
    if existing_service and (existing_serviced is
                             None) and request.method == 'POST':
        # update role with new existing service
        mongo.db.roles.update_one({'name': _role_name}, {
            '$push': {
                'services':
                DBRef(collection="services", id=existing_service['_id'])
            }
        })
        message = {
            'data': "null",
            'result': {
                'isError': 'false',
                'message': 'service_to_role_added_successfully',
                'status': 200,
            }
        }
        # for json response
        return jsonify(message)
    else:
        return not_found()
Beispiel #7
0
    def post(cls, request, *args, **kwargs):
        """
        Creates a new instance of the class, based on the body
        received from the client.

        If audit_logs is enabled on the class, then a log item will be created
        for this action.

        Broadcasts the action to all connected users.

        :param request: client request
        :type request: raccoon.utils.request.Request
        :param args: not used
        :param kwargs: instance body
        :return: None
        """
        if not cls.model:
            raise ReplyError(404)

        params = {}
        for key, value in kwargs.items():
            if value and hasattr(cls.model, key):
                # !important
                # Make value = ObjectId(value) if field is a reference field
                field = cls.model._fields.get(key)
                if type(field) is ReferenceField:
                    if not value:
                        value = None
                    else:
                        model_name = field.document_type._get_collection().name
                        value = DBRef(model_name, ObjectId(value))

                params[key] = value

        try:
            response = cls.model.objects.create(**params)
        except NotUniqueError as e:
            log.info("Failed to create {}".format(cls.model.__name__),
                     exc_info=True)
            raise ReplyError(409, cls.model.get_message_from_exception(e))
        except InvalidDocumentError as e:
            log.info("Invalid document {}".format(cls.model.__name__),
                     exc_info=True)
            raise ReplyError(400, cls.model.get_message_from_exception(e))

        if cls.audit_logs:
            user = request.user
            audit_log = AuditLog(user=user.email,
                                 action='new {}'.format(cls.model.__name__),
                                 message='{} {} added'.format(
                                     cls.model.__name__, kwargs.get('name')))
            audit_log.save()

            request.broadcast(audit_log.get_dict(),
                              verb='post',
                              resource='/api/v1/auditlogs/',
                              admin_only=True)

        request.broadcast(response.get_dict())
 def __dict__(self):
     return {
         "name_song":
         self.name_song,
         "author":
         DBRef(collection=DataBase.get_type_collection(self.author),
               id=self.author.id)
     }
Beispiel #9
0
 def to_python(self, value):
     """Convert a MongoDB-compatible type to a Python type.
     """
     if (not self.dbref and
         not isinstance(value, (DBRef, Document, EmbeddedDocument))):
         collection = self.document_type._get_collection_name()
         value = DBRef(collection, self.document_type.id.to_python(value))
     return value
 def setUp(self):
     """Set up."""
     self.referenced_doc = ReferencedDocument(name="hi")
     self.referenced_doc.save()
     self.doc = IDCheckDocument(ref=self.referenced_doc)
     self.ref_doc = IDCheckDocument(enable_gj=True,
                                    ref=DBRef("referenced_document",
                                              self.referenced_doc.pk))
Beispiel #11
0
 def dbref(self):
     """
     Return a pymongo DBRef instance related to the document
     """
     if not self.is_created:
         raise NotCreatedError('Must create the document before'
                               ' having access to DBRef')
     return DBRef(collection=self.collection.name, id=self.pk)
Beispiel #12
0
    def test_reference(self):
        @self.instance.register
        class MyReferencedDoc(Document):
            class Meta:
                collection_name = 'my_collection'

        @self.instance.register
        class OtherDoc(Document):
            pass

        to_refer_doc = MyReferencedDoc.build_from_mongo(
            {'_id': ObjectId("5672d47b1d41c88dcd37ef05")})
        ref = Reference(MyReferencedDoc, to_refer_doc.pk)
        dbref = DBRef('my_collection', to_refer_doc.pk)
        other_doc = OtherDoc.build_from_mongo(
            {'_id': ObjectId("5672d47b1d41c88dcd37ef07")})

        # Test reference equality
        assert ref == to_refer_doc
        assert ref == dbref
        assert dbref == to_refer_doc
        assert dbref == ref
        assert to_refer_doc == ref
        assert to_refer_doc == dbref

        @self.instance.register
        class MyDoc(Document):
            ref = fields.ReferenceField(MyReferencedDoc,
                                        attribute='in_mongo_ref')

        MySchema = MyDoc.Schema

        MyDataProxy = data_proxy_factory('My', MySchema())
        d = MyDataProxy()
        d.load({'ref': ObjectId("5672d47b1d41c88dcd37ef05")})
        d.load({'ref': "5672d47b1d41c88dcd37ef05"})
        assert d.dump() == {'ref': "5672d47b1d41c88dcd37ef05"}
        assert d.get('ref').document_cls == MyReferencedDoc
        d.set('ref', to_refer_doc)
        assert d.to_mongo(update=True) == {
            '$set': {
                'in_mongo_ref': to_refer_doc.pk
            }
        }
        assert d.get('ref') == ref
        d.set('ref', ref)
        assert d.get('ref') == ref
        d.set('ref', dbref)
        assert d.get('ref') == ref

        with pytest.raises(ValidationError):
            d.set('ref', other_doc)
        not_created_doc = MyReferencedDoc()
        with pytest.raises(ValidationError):
            d.set('ref', not_created_doc)
        bad_ref = Reference(OtherDoc, other_doc.pk)
        with pytest.raises(ValidationError):
            d.set('ref', bad_ref)
def test_mongodb_origin_DBRef_type(sdc_builder, sdc_executor, mongodb):
    """
    DBRef datatype is a reference to a document in other collection.
    Step 1. Create two collections(#1 and #2) in MongoDB and add sample documents in collection #1.
    Step 2. Add test documents to collection #2 which refer to the sample documents in collection #1.
    Step 3. Confirm that MongoDB origin reads the test documents from collection #2

    The pipeline looks like:
        mongodb_origin >> wiretap
    """
    database_name = get_random_string(ascii_letters, 5)
    collection1 = get_random_string(ascii_letters, 10)
    collection2 = get_random_string(ascii_letters, 10)
    logger.debug('database_name: %s, collection1: %s, collection2: %s' , database_name, collection1, collection2)

    pipeline_builder = sdc_builder.get_pipeline_builder()
    pipeline_builder.add_error_stage('Discard')

    mongodb_origin = pipeline_builder.add_stage('MongoDB', type='origin')
    mongodb_origin.set_attributes(capped_collection=False,
                                  database=database_name,
                                  collection=collection2)

    wiretap = pipeline_builder.add_wiretap()
    mongodb_origin >> wiretap.destination
    pipeline = pipeline_builder.build().configure_for_environment(mongodb)
    sdc_executor.add_pipeline(pipeline)

    try:
        # Step 1. A sample documents to collection #1
        docs_in_database = copy.deepcopy(ORIG_DOCS)
        mongodb_database = mongodb.engine[database_name]
        mongodb_collection1 = mongodb_database[collection1]
        insert_list = [mongodb_collection1.insert_one(doc) for doc in docs_in_database]
        num_of_records  = len(insert_list)
        logger.info('Added %i documents into %s collection', num_of_records, collection1)

        # Step 2. Generate test documents with DBRef datatype and insert to collection #2
        logger.info('Adding test documents into %s collection...', collection2)
        mongodb_collection2 = mongodb_database[collection2]
        docs_in_col1 = [] # This will be used to compare the result
        # Obtain sample document's _id from collection #1 and assign it to test document as inserting into collection #2
        for doc in mongodb_collection1.find().sort('_id', 1):
            # Sort by _id so that we can compare the result easily later
            mongodb_collection2.insert_one({'test_ref': DBRef(collection=collection1, id=doc['_id'])})
            docs_in_col1.append(doc)

        # Step 3. Start pipeline and verify the documents using wiretap.
        sdc_executor.start_pipeline(pipeline)
        sdc_executor.wait_for_pipeline_metric(pipeline, 'input_record_count', 1)
        sdc_executor.stop_pipeline(pipeline)
        for record, expected in zip(wiretap.output_records, docs_in_col1):
            assert record.get_field_data('/test_ref/$ref') == collection1
            assert record.get_field_data('/test_ref/$id') == str(expected['_id'])

    finally:
        logger.info('Dropping %s database...', database_name)
        mongodb.engine.drop_database(mongodb_origin.database)
Beispiel #14
0
def friendReq(id):
    current_user = mongo.db.userReg.find_one({'email': session['email']})
    existing_user = mongo.db.userReg.find_one({'_id': ObjectId(id)})
    # check if userReg already has the friend
    existing_friend = mongo.db.userReg.find_one({
        'email':
        session['email'],
        'friends':
        DBRef(collection="userReg", id=ObjectId(id))
    })
    if existing_friend:
        #existing_friend = mongo.db.userReg.find_one({'email':session['email'], 'friends':DBRef(collection = "userReg", id = ObjectId(id) )  })
        message = {
            'data': "null",
            'result': {
                'isError': 'false',
                'message': 'Friend Already Exist',
                'status': 200,
            }
        }
        return jsonify(message)
    if existing_user and (existing_user != current_user) and (
            existing_friend is None) and request.method == 'POST':
        # update userReg with new existing friend
        #mongo.db.userReg.update_one({'email':session['email'] },{ '$push': {'friend_pending': DBRef(collection = "userReg", id = ObjectId(id) ) } })
        mongo.db.userReg.update_one({'_id': ObjectId(id)}, {
            '$push': {
                'friend_pending':
                DBRef(collection="userReg", id=current_user['_id'])
            }
        })

        message = {
            'data': "null",
            'result': {
                'isError': 'false',
                'message': 'Friend Request Sent',
                'status': 200,
            }
        }
        # for json response
        return jsonify(message)
    else:
        return not_found()
Beispiel #15
0
def oparl_agendaItem_data(params):
  data = db.get_agendaItem(search_params={'_id': ObjectId(params['_id'])})
  if len(data) == 1:
    data = oparl_agendaItem_layout(data[0], params)
    meeting = db.get_meeting(search_params={'agendaItem': DBRef('agendaItem', ObjectId(params['_id']))})
    if len(meeting):
      data['meeting'] = "%s/oparl/meeting/%s%s" % (app.config['api_url'], meeting[0]['_id'], generate_postfix(params))
    return data
  elif len(data) == 0:
    abort(404)
Beispiel #16
0
def oparl_consultation_data(params):
  data = db.get_consultation(search_params={'_id': ObjectId(params['_id'])})
  if len(data) == 1:
    data = oparl_consultation_layout(data[0], params)
    agendaItem = db.get_agendaItem(search_params={'consultation': DBRef('consultation', ObjectId(params['_id']))})
    if len(agendaItem):
      data['agendaItem'] = "%s/oparl/agendaItem/%s%s" % (app.config['api_url'], agendaItem[0]['_id'], generate_postfix(params))
    return data
  elif len(data) == 0:
    abort(404)
Beispiel #17
0
 def test_dereference_manipulator(self):
     self.session = utils.get_session(self.config, inc_manipulators=False)
     collection_a = self.session.get_collection('test_a')
     collection_b = self.session.get_collection('test_b')
     collection_a.insert({"_id": 1, "foo": "bar"})
     collection_b.insert({"baz": "blah", "foo": DBRef(collection="test_a", id=1)})
     manipulator = DereferenceManipulator(ref_fields=[('test_b', 'foo')])
     self.session.add_manipulator(manipulator)
     doc = collection_b.find_one({"baz": "blah"})
     self.assertEqual(doc['foo'], 'bar')
Beispiel #18
0
 def test_dbref(self):
     student = self.Student()
     with pytest.raises(exceptions.NotCreatedError):
         student.dbref
     # Fake document creation
     student.id = ObjectId('573b352e13adf20d13d01523')
     student.is_created = True
     student.clear_modified()
     assert student.dbref == DBRef(collection='student',
                                   id=ObjectId('573b352e13adf20d13d01523'))
Beispiel #19
0
def view_paper(id):
    """
  Gibt Dokumenten-Detailseite aus
  """
    result = db.get_paper(search_params={'_id': ObjectId(id)},
                          deref={
                              'values': [
                                  'body', 'mainFile', 'auxiliaryFile',
                                  'relatedPaper', 'subordinatedPaper',
                                  'superordinatedPaper'
                              ]
                          })
    if len(result) == 0:
        abort(404)
    result = result[0]
    result['numberOfFiles'] = 0
    if 'mainFile' in result:
        result['numberOfFiles'] += 1
    if 'auxiliaryFile' in result:
        result['numberOfFiles'] += len(result['auxiliaryFile'])
    result['consultation'] = db.get_consultation(
        search_params={'paper': DBRef('paper', ObjectId(id))})
    for consultation_id in range(len(result['consultation'])):
        agendaItem_result = db.get_agendaItem(
            search_params={
                'consultation':
                DBRef('consultation', result['consultation'][consultation_id]
                      ['_id'])
            })
        if len(agendaItem_result):
            result['consultation'][consultation_id][
                'agendaItem'] = agendaItem_result[0]
            meeting_result = db.get_meeting(
                search_params={
                    'agendaItem':
                    DBRef(
                        'agendaItem', result['consultation'][consultation_id]
                        ['agendaItem']['_id'])
                })
            if len(meeting_result):
                result['consultation'][consultation_id]['agendaItem'][
                    'meeting'] = meeting_result[0]
    return render_template('paper_details.html', paper=result)
Beispiel #20
0
 def to_dict(self):
     object_dict = {"db_ref": self.__db_ref}
     internal_fields = dir(self)
     for field in get_fields(self.__document_class_reference,
                             self.__document_name_reference):
         if field["name"] in self.__extended_document or field[
                 "name"] in internal_fields:
             if field["name"] in self.__extended_document:
                 value = self.__extended_document[field["name"]]
             else:
                 value = getattr(self, field["name"])
             if type(value) is list:
                 childs = []
                 for rec in value:
                     if type(field["type"]["list_type"]) is DocumentRef:
                         if type(rec) is DBRef:
                             childs.append(rec)
                         elif type(rec) is dict and "id" in rec:
                             childs.append(
                                 DBRef(
                                     field["type"]
                                     ["list_type"].db_ref.collection,
                                     rec['id']))
                         elif hasattr(rec, 'id'):
                             childs.append(
                                 DBRef(
                                     field["type"]
                                     ["list_type"].db_ref.collection,
                                     rec.id))
                         else:
                             childs.append(
                                 DBRef(
                                     field["type"]
                                     ["list_type"].db_ref.collection, rec))
                     elif "to_dict" in dir(rec):
                         childs.append(rec.to_dict())
                     else:
                         childs.append(rec)
                 object_dict[field["name"]] = childs
             else:
                 object_dict[field["name"]] = value
     return object_dict
Beispiel #21
0
def roleto_group():
    _json = request.json
    _group_name = _json['group_name']
    _role_name = _json['role_name']
    existing_group = mongo.db.groups.find_one({'name': _group_name})
    existing_role = mongo.db.roles.find_one({'name': _role_name})
    # Add new group if not exist
    if _group_name and (existing_group is None):
        mongo.db.groups.insert({
            'name': _group_name,
            'description': '',
            'roles': [],
            'services': []
        })
    # check if Group already has the Role
    if _group_name and existing_role:
        existing_roled = mongo.db.groups.find_one({
            'name':
            _group_name,
            'roles':
            DBRef(collection="roles", id=existing_role['_id'])
        })
    if existing_role and (existing_roled is None) and request.method == 'POST':
        # Update groups with new existing role
        mongo.db.groups.update_one({'name': _group_name}, {
            '$push': {
                'roles': DBRef(collection="roles", id=existing_role['_id'])
            }
        })
        message = {
            'data': "null",
            'result': {
                'isError': 'false',
                'message': 'service_to_role_added_successfully',
                'status': 200,
            }
        }
        # for json response
        return jsonify(message)
    else:
        return not_found()
Beispiel #22
0
 def test_database(self):
     client = self.client
     db = client.pymongo_test
     ops = [
         (db.command, ['ping'], {}),
         (db.create_collection, ['collection'], {}),
         (db.list_collection_names, [], {}),
         (db.validate_collection, ['collection'], {}),
         (db.drop_collection, ['collection'], {}),
         (db.dereference, [DBRef('collection', 1)], {}),
     ]
     self._test_ops(client, *ops)
    def test_dbref_to_mongo(self):
        """Make sure that calling to_mongo on a ReferenceField which
        has dbref=False, but actually actually contains a DBRef returns
        an ID of that DBRef.
        """
        class Person(Document):
            name = StringField()
            parent = ReferenceField("self", dbref=False)

        p = Person(name="Steve", parent=DBRef("person", "abcdefghijklmnop"))
        assert p.to_mongo() == SON([("name", u"Steve"),
                                    ("parent", "abcdefghijklmnop")])
Beispiel #24
0
    def test_reference(self, collection_moke):
        class MyReferencedDoc(Document):
            class Meta:
                collection = collection_moke

        class OtherDoc(Document):
            pass

        to_refer_doc = MyReferencedDoc.build_from_mongo(
            {'_id': ObjectId("5672d47b1d41c88dcd37ef05")})
        ref = Reference(MyReferencedDoc, to_refer_doc.pk)
        dbref = DBRef(collection_moke.name, to_refer_doc.pk)
        other_doc = OtherDoc.build_from_mongo(
            {'_id': ObjectId("5672d47b1d41c88dcd37ef07")})

        # Test reference equality
        assert ref == to_refer_doc
        assert ref == dbref
        assert dbref == to_refer_doc
        assert dbref == ref
        assert to_refer_doc == ref
        assert to_refer_doc == dbref

        class MySchema(Schema):
            ref = fields.ReferenceField(MyReferencedDoc,
                                        attribute='in_mongo_ref')

        d = DataProxy(MySchema())
        d.load({'ref': ObjectId("5672d47b1d41c88dcd37ef05")})
        d.load({'ref': "5672d47b1d41c88dcd37ef05"})
        assert d.dump() == {'ref': "5672d47b1d41c88dcd37ef05"}
        d.get('ref').document_cls == MyReferencedDoc
        d.set('ref', to_refer_doc)
        assert d.to_mongo(update=True) == {
            '$set': {
                'in_mongo_ref': to_refer_doc.pk
            }
        }
        assert d.get('ref') == ref
        d.set('ref', ref)
        assert d.get('ref') == ref
        d.set('ref', dbref)
        assert d.get('ref') == ref

        with pytest.raises(ValidationError):
            d.set('ref', other_doc)
        not_created_doc = MyReferencedDoc()
        with pytest.raises(ValidationError):
            d.set('ref', not_created_doc)
        bad_ref = Reference(OtherDoc, other_doc.pk)
        with pytest.raises(ValidationError):
            d.set('ref', bad_ref)
Beispiel #25
0
def acceptFriendReq(id):
    current_user = mongo.db.userReg.find_one({'email': session['email']})
    pending_friend = mongo.db.userReg.find_one({
        'email':
        session['email'],
        'friend_pending':
        DBRef(collection="userReg", id=ObjectId(id))
    })

    if pending_friend:
        # adding in both users friend list
        mongo.db.userReg.update_one({'email': session['email']}, {
            '$push': {
                'friends': DBRef(collection="userReg", id=ObjectId(id))
            }
        })
        mongo.db.userReg.update_one({'_id': ObjectId(id)}, {
            '$push': {
                'friends': DBRef(collection="userReg", id=current_user['_id'])
            }
        })
        mongo.db.userReg.update_one(
            {
                'email': session['email'],
                'friend_pending': DBRef(collection="userReg", id=ObjectId(id))
            }, {'$pop': {
                'friend_pending': -1
            }})
        message = {
            'data': "null",
            'result': {
                'isError': 'false',
                'message': 'Friend Request Accepted',
                'status': 200,
            }
        }
        return jsonify(message)
    else:
        return not_found()
Beispiel #26
0
def rm_service_fromgroup():
    _json = request.json
    _group_name = _json['group_name']
    _service_name = _json['service_name']
    existing_group = mongo.db.groups.find_one({'name': _group_name})
    existing_service = mongo.db.services.find_one({
        'name': _service_name,
    })
    # check if Role already has the service
    if _group_name and existing_service:
        existing_serviced = mongo.db.groups.find_one({
            'name':
            _group_name,
            'services':
            DBRef(collection="services", id=existing_service['_id'])
        })
    if existing_service and (existing_serviced) and request.method == 'POST':
        # update role with pop the target existing service
        mongo.db.groups.update_one(
            {
                'name':
                _group_name,
                'services':
                DBRef(collection="services", id=existing_service['_id'])
            }, {'$pop': {
                'services': -1
            }})
        message = {
            'data': "null",
            'result': {
                'isError': 'false',
                'message': 'rm_service_fromgroup_successfully',
                'status': 200,
            }
        }
        # for json response
        return jsonify(message)
    else:
        return not_found()
Beispiel #27
0
 def process_formdata(self, valuelist):
     if not valuelist:
         return []
     if len(valuelist) > 1:
         oids = [clean_oid(id) for id in valuelist]
     elif isinstance(valuelist[0], basestring):
         oids = [
             ObjectId(id.strip()) for id in valuelist[0].split(',')
             if id.strip()
         ]
     else:
         raise ValueError('Unsupported form parameter: ' + valuelist)
     self.data = [DBRef(self.model.lower(), oid) for oid in oids]
    def test_dbref_to_mongo(self):
        """Make sure that calling to_mongo on a ReferenceField which
        has dbref=False, but actually actually contains a DBRef returns
        an ID of that DBRef.
        """
        class Person(Document):
            name = StringField()
            parent = ReferenceField('self', dbref=False)

        p = Person(name='Steve', parent=DBRef('person', 'abcdefghijklmnop'))
        self.assertEqual(
            p.to_mongo(),
            SON([('name', u'Steve'), ('parent', 'abcdefghijklmnop')]))
Beispiel #29
0
    def dbref(self, with_database=True, **kwargs):
        """Returns a DBRef for the current object.

        If `with_database` is False, the resulting :class:`pymongo.dbref.DBRef`
        won't have a :attr:`database` field.

        Any other parameters will be passed to the DBRef constructor, as per the mongo specs.
        """
        if not hasattr(self, '_id'):
            self._id = ObjectId()

        database = self._meta.database if with_database else None
        return DBRef(self._meta.collection, self._id, database, **kwargs)
Beispiel #30
0
def ensure_owner_collection_migration():
    """We ran into an issue with 3.0.5 where Requests and Jobs got migrated over to
    the Owner collection. This is in place to resolve that."""

    database = get_db()

    if database["owner"].count():
        logger.warning(
            "Found owner collection, migrating documents to appropriate collections. "
            "This could take a while :)")

        for doc in database["owner"].find({"_cls": "Owner.Request"}):
            try:
                del doc["_cls"]

                if doc.get("has_parent"):
                    doc["parent"] = DBRef("request", doc["parent"].id)

                database["request"].insert_one(doc)
            except Exception:
                logger.error(f"Error migrating request {doc['_id']}")

        for doc in database["owner"].find({"_cls": "Owner.Job"}):
            try:
                del doc["_cls"]

                database["job"].insert_one(doc)
            except Exception:
                logger.error(f"Error migrating job {doc['_id']}")

        for doc in database["file"].find():
            try:
                if doc["owner_type"] == "REQUEST":
                    doc["request"] = doc["owner"]
                elif doc["owner_type"] == "JOB":
                    doc["job"] = doc["owner"]
                else:
                    logger.error(
                        f"Unable to migrate file {doc['_id']}: bad owner type")
                    database["file"].delete_one({"_id": doc["_id"]})
                    continue

                doc["owner"] = None

                database["file"].replace_one({"_id": doc["_id"]}, doc)
            except Exception:
                logger.error(f"Error migrating file {doc['_id']}, removing")
                database["file"].delete_one({"_id": doc["_id"]})

        logger.info("Dropping owner collection (this is intended!)")
        database.drop_collection("owner")
Beispiel #31
0
    def __init__(self, *args, **kwargs):
        """Inits the document with given data and validates the fields
        (field validation bad idea during init?). If you define
        ``__init__`` method for your document class, make sure to call
        this
        ::

            class MyDoc(BaseDocument, dot_notation=True):
                foo = Field(str)
                bar = Field(int, required=False)

                def __init__(self, *args, **kwargs):
                    super(MyDoc, self).__init__(*args, **kwargs)
                    # do other stuff
        """
        # if input dict, merge (not updating) into kwargs
        if args and not isinstance(args[0], dict):
            raise TypeError('dict or dict subclass argument expected')
        elif args:
            for field_name, field_value in args[0].items():
                if field_name not in kwargs:
                    kwargs[field_name] = field_value
        super(BaseDocument, self).__init__()
        for field_name, field in self.nanomongo.fields.items():
            if hasattr(field, 'default_value'):
                val = field.default_value
                dict.__setitem__(self, field_name, val() if callable(val) else val)
            # attach get_<field_name>_field methods for DBRef fields
            if field.data_type in [DBRef] + DBRef.__subclasses__():
                getter_name = 'get_%s_field' % field_name
                doc_class = field.document_class if hasattr(field, 'document_class') else None
                getter = ref_getter_maker(field_name, document_class=doc_class)
                setattr(self, getter_name, six.create_bound_method(getter, self))
        for field_name in kwargs:
            if self.nanomongo.has_field(field_name):
                self.nanomongo.validate(field_name, kwargs[field_name])
                dict.__setitem__(self, field_name, kwargs[field_name])
            else:
                raise ExtraFieldError('Undefined field %s=%s in %s' %
                                      (field_name, kwargs[field_name], self.__class__))
        for field_name, field_value in self.items():
            # transform dict to RecordingDict so we can track diff in embedded docs
            if isinstance(field_value, dict):
                dict.__setitem__(self, field_name, RecordingDict(field_value))
Beispiel #32
0
 def dereference(self, session, ref, allow_none=False):
     ref = DBRef(id=ref, collection=self.type.type.get_collection_name(),
                 database=self.db)
     ref.type = self.type.type
     return session.dereference(ref, allow_none=allow_none)
Beispiel #33
0
 def dereference(self, session, ref, allow_none=False):
     """ Dereference an ObjectID to this field's underlying type """
     ref = DBRef(id=ref, collection=self.type.type.get_collection_name(),
                 database=self.db)
     ref.type = self.type.type
     return session.dereference(ref, allow_none=allow_none)