Ejemplo n.º 1
0
    def init(cls):
        """
        """

        name = cls.get_collection_name()

        # Set type
        try:
            collection_type = getattr(cls, 'collection_type')
        except:
            collection_type = 2

        # Create collection
        try:
            cls.collection_instance = Collection.create(name=name, type=collection_type)
        except:
            cls.collection_instance = Collection.get_loaded_collection(name=name)

        try:
            if not isinstance(cls.objects, CollectionModelManager):
                cls.objects = cls.objects(cls)
        except:
            pass # This is the case if init was called more than once

        # Create meta data for collection
        cls._model_meta_data = cls.MetaDataObj()

        # Go through all fields
        for attribute in cls.get_collection_fields():
            attribute.on_init(cls)
Ejemplo n.º 2
0
    def on_destroy(self, model_class):
        """
        """

        if not self.related_name is None:
            relation_name = self._get_relation_collection_name(model_class)
            Collection.remove(name=relation_name)
Ejemplo n.º 3
0
    def on_destroy(self, model_class):
        """
        """

        if not self.related_name is None:
            relation_name = self._get_relation_collection_name(model_class)
            Collection.remove(name=relation_name)
Ejemplo n.º 4
0
    def test_create_and_delete_collection_without_extra_db(self):

        collection_name = 'test_foo_123'

        col = Collection.create(name=collection_name)

        self.assertIsNotNone(col)

        Collection.remove(name=collection_name)
Ejemplo n.º 5
0
    def test_create_and_delete_collection_without_extra_db(self):

        collection_name = 'test_foo_123'

        col = Collection.create(name=collection_name)

        self.assertIsNotNone(col)

        Collection.remove(name=collection_name)
Ejemplo n.º 6
0
    def destroy(cls):
        """
        """

        # Go through all fields
        for attribute in cls.get_collection_fields():
            attribute.on_destroy(cls)

        name = cls.get_collection_name()
        Collection.remove(name=name)
Ejemplo n.º 7
0
    def destroy(cls):
        """
        """

        # Go through all fields
        for attribute in cls.get_collection_fields():
            attribute.on_destroy(cls)

        name = cls.get_collection_name()
        Collection.remove(name=name)
Ejemplo n.º 8
0
    def tearDown(self):
        # Delete index
        self.geo_index.delete()
        self.hash_index.delete()
        self.fulltext_index.delete()
        self.skiplist_index.delete()

        # They need to be deleted
        Collection.remove(name=self.test_1_col.name)

        Database.remove(name=self.database_name)
Ejemplo n.º 9
0
    def tearDown(self):
        # Delete index
        self.geo_index.delete()
        self.hash_index.delete()
        self.fulltext_index.delete()
        self.skiplist_index.delete()

        # They need to be deleted
        Collection.remove(name=self.test_1_col.name)

        Database.remove(name=self.database_name)
Ejemplo n.º 10
0
    def init(cls):
        """
        """

        # TODO: Deal with super classes

        name = cls.get_collection_name()

        # Set type
        try:
            collection_type = getattr(cls, 'collection_type')
        except:
            collection_type = 2

        # TODO: Database is not set for the collection

        # Create collection
        try:
            cls.collection_instance = Collection.create(name=name, type=collection_type)
        except:
            cls.collection_instance = Collection.get_loaded_collection(name=name)

        try:
            if not isinstance(cls.objects, CollectionModelManager):
                cls.objects = cls.objects(cls)
        except:
            pass # This is the case if init was called more than once

        # Create meta data for collection
        cls._model_meta_data = cls.MetaDataObj()

        # Go through all fields
        fields_dict = cls.get_collection_fields_dict()
        for attribute_name in fields_dict:
            attribute = fields_dict[attribute_name]
            # Trigger init event
            attribute.on_init(cls, attribute_name)

        # Go through all index
        model_index_list = cls.get_model_fields_index()
        for index_attribute_name in model_index_list:
            # Save created index
            index_obj = model_index_list[index_attribute_name]
            created_index = Index(collection=cls.collection_instance, index_type_obj=index_obj)
            # Reset class attribute
            setattr(cls, index_attribute_name, created_index)
            # Save index
            created_index.save()

            if not created_index.index_type_obj.is_new:
                created_index.overwrite()
Ejemplo n.º 11
0
    def on_init(self, model_class):
        """
        """

        if not self.related_name is None:
            relation_name = self._get_relation_collection_name(model_class)

            try:
                self.relation_collection = Collection.create(name=relation_name, database=Client.instance().database, type=3)
            except:
                self.relation_collection = Collection.get_loaded_collection(name=relation_name)

            fields = self.relation_class._model_meta_data._fields
            fields[self.related_name] = ManyToManyField(to=model_class, related_name=None)
Ejemplo n.º 12
0
    def init(cls):
        """
        """

        name = cls.get_collection_name()

        # Set type
        try:
            collection_type = getattr(cls, 'collection_type')
        except:
            collection_type = 2

        # TODO: Database is not set for the collection

        # Create collection
        try:
            cls.collection_instance = Collection.create(name=name,
                                                        type=collection_type)
        except:
            cls.collection_instance = Collection.get_loaded_collection(
                name=name)

        try:
            if not isinstance(cls.objects, CollectionModelManager):
                cls.objects = cls.objects(cls)
        except:
            pass  # This is the case if init was called more than once

        # Create meta data for collection
        cls._model_meta_data = cls.MetaDataObj()

        # Go through all fields
        for attribute in cls.get_collection_fields():
            # Trigger init event
            attribute.on_init(cls)

        # Go through all index
        model_index_list = cls.get_model_fields_index()
        for index_attribute_name in model_index_list:
            # Save created index
            index_obj = model_index_list[index_attribute_name]
            created_index = Index(collection=cls.collection_instance,
                                  index_type_obj=index_obj)
            # Reset class attribute
            setattr(cls, index_attribute_name, created_index)
            # Save index
            created_index.save()

            if not created_index.index_type_obj.is_new:
                created_index.overwrite()
Ejemplo n.º 13
0
    def test_get_collection(self):

        collection_name = 'test_foo_123'

        col = Collection.create(name=collection_name)

        self.assertIsNotNone(col)

        retrieved_col = Collection.get_loaded_collection(name=collection_name)

        self.assertEqual(col.id, retrieved_col.id)
        self.assertEqual(col.name, retrieved_col.name)
        self.assertEqual(col.type, retrieved_col.type)

        Collection.remove(name=collection_name)
Ejemplo n.º 14
0
    def test_get_collection(self):

        collection_name = 'test_foo_123'

        col = Collection.create(name=collection_name)

        self.assertIsNotNone(col)

        retrieved_col = Collection.get_loaded_collection(name=collection_name)

        self.assertEqual(col.id, retrieved_col.id)
        self.assertEqual(col.name, retrieved_col.name)
        self.assertEqual(col.type, retrieved_col.type)

        Collection.remove(name=collection_name)
Ejemplo n.º 15
0
    def test_getting_new_info_for_collection(self):

        collection_name = 'test_foo_123'

        col = Collection.create(name=collection_name)

        retrieved_col = Collection.get_loaded_collection(name=collection_name)
        retrieved_col.set_data(waitForSync=True)
        retrieved_col.save()

        col.get()

        self.assertEqual(col.waitForSync, True)

        Collection.remove(name=collection_name)
Ejemplo n.º 16
0
    def test_getting_new_info_for_collection(self):

        collection_name = 'test_foo_123'

        col = Collection.create(name=collection_name)

        retrieved_col = Collection.get_loaded_collection(name=collection_name)
        retrieved_col.set_data(waitForSync=True)
        retrieved_col.save()

        col.get()

        self.assertEqual(col.waitForSync, True)

        Collection.remove(name=collection_name)
Ejemplo n.º 17
0
    def setUp(self):

        self.database_name = 'testcase_index_222_123'
        self.db = Database.create(name=self.database_name)

        self.operating_collection = 'bar_extra'
        self.test_1_col = Collection.create(name=self.operating_collection)
Ejemplo n.º 18
0
    def setUp(self):

        self.database_name = 'testcase_transaction_123'
        self.db = Database.create(name=self.database_name)

        self.operating_collection = 'foo_test'
        self.test_1_col = Collection.create(name=self.operating_collection)
Ejemplo n.º 19
0
    def setUp(self):

        self.database_name = 'testcase_index_222_123'
        self.db = Database.create(name=self.database_name)

        self.operating_collection = 'bar_extra'
        self.test_1_col = Collection.create(name=self.operating_collection)
Ejemplo n.º 20
0
    def setUp(self):

        self.database_name = 'testcase_transaction_123'
        self.db = Database.create(name=self.database_name)

        self.operating_collection = 'foo_test'
        self.test_1_col = Collection.create(name=self.operating_collection)
Ejemplo n.º 21
0
 def create(self, validated_data):
     #Init Arango Models
     AwsCredentials.init()
     Companies.init()
     account_obj = AwsCredentials.objects.get(
         _id=str(validated_data['account_id']))
     obj = Collection.get_loaded_collection(name='ScheduleIntervalSettings')
     SimpleQuery.update_by_example(
         obj, {
             'account_id': str(account_obj.document),
             'service': str(validated_data['service']),
         }, {
             'service': validated_data['service'],
             'repeat_delay': validated_data['repeat_delay']
         })
     response = SimpleQuery.get_by_example(
         obj,
         example_data={
             'account_id': str(account_obj.document),
             'service': str(validated_data['service'])
         })
     headers = {"Content-Type": "application/json"}
     url = FOXX_BASE_URL + "execute/" + urllib.quote_plus(
         str(response)) + "/" + str(self.context['user']) + "/edit"
     response = requests.get(url, headers=headers)
     return response
Ejemplo n.º 22
0
    def validate(self, data):
        collection = Collection.get_loaded_collection(name='AwsCredentials')
        obj = SimpleQuery.get_by_example(collection,
                                         example_data={
                                             'company':
                                             self.context['company'],
                                             'external_id': data['external_id']
                                         })
        if obj:
            raise serializers.ValidationError(
                "External id already exist for a company")

        obj = SimpleQuery.get_by_example(collection,
                                         example_data={
                                             'company':
                                             self.context['company'],
                                             'role_arn': data['role_arn']
                                         })
        if obj:
            raise serializers.ValidationError("Role Arn already exist")

        obj = SimpleQuery.get_by_example(collection,
                                         example_data={
                                             'company':
                                             self.context['company'],
                                             'account_name':
                                             data['account_name']
                                         })
        if obj:
            raise serializers.ValidationError(
                "Account name already exist for a company")
        return data
Ejemplo n.º 23
0
def company(self):
    collection = Collection.get_loaded_collection(name='UserCompanies')
    obj = SimpleQuery.get_by_example(collection,
                                     example_data={
                                         'user': int(self.id),
                                         'active': True
                                     })
    return obj.company
Ejemplo n.º 24
0
    def test_remove_document_from_collection(self):

        collection_name = 'test_remove_document_from_collection'

        col = Collection.create(name=collection_name)
        doc1 = col.create_document()
        doc1.save()

        all_documents = col.documents()
        self.assertEqual(len(all_documents), 1)
        doc = all_documents[0]

        doc.delete()

        all_documents = col.documents()
        self.assertEqual(len(all_documents), 0)

        Collection.remove(name=collection_name)
Ejemplo n.º 25
0
    def test_different_document_revisions(self):

        collection_name = 'test_revision_documents'

        col = Collection.create(name=collection_name)
        doc1 = col.create_document()
        doc1.save()

        all_documents = col.documents()
        self.assertEqual(len(all_documents), 1)
        doc = all_documents[0]

        self.assertEqual(doc.revision, doc1.revision)

        doc.foo = 'bar'
        doc.save()

        self.assertNotEqual(doc.revision, doc1.revision)

        Collection.remove(name=collection_name)
Ejemplo n.º 26
0
    def on_init(self, model_class):
        """
        """

        if not self.related_name is None:
            relation_name = self._get_relation_collection_name(model_class)

            try:
                self.relation_collection = Collection.create(name=relation_name, database=Client.instance().database, type=3)
            except:
                self.relation_collection = Collection.get_loaded_collection(name=relation_name)

            fields = self.relation_class._model_meta_data._fields
            otherside_field = ManyToManyField(to=model_class, related_name=None)
            fields[self.related_name] = otherside_field

            # Configure other side field
            otherside_field.related_queryset = self.relation_class.objects.all()
            otherside_field.relation_collection = self.relation_collection

            self.related_queryset = self.relation_class.objects.all()
Ejemplo n.º 27
0
    def test_basic_creation_with_default(self):


        class EndModel(CollectionModel):

            test_field = CharField()


        class StartModel(CollectionModel):
            collection_name = 'never_to_be_seen_again'

            others = ManyToManyField(to=EndModel, related_name='starters')

        EndModel.init()
        StartModel.init()

        end_model1 = EndModel()
        end_model1.test_field = 'foo'
        end_model1.save()

        end_model2 = EndModel()
        end_model2.test_field = 'bar'
        end_model2.save()

        start_model = StartModel()
        start_model.others = [end_model1, end_model2]
        start_model.save()

        relation_collection_name = start_model.get_field(name='others')._get_relation_collection_name(StartModel)
        col = Collection.get_loaded_collection(name=relation_collection_name)
        relation_documents = col.documents()

        self.assertEqual(len(relation_documents), 2)

        rel1 = relation_documents[0]
        rel2 = relation_documents[1]

        # From is always the same
        self.assertEqual(rel1._from, start_model.document.id)
        self.assertEqual(rel2._from, start_model.document.id)

        is_first_the_first_end_model = rel1._to == end_model1.document.id
        is_first_the_second_end_model = rel1._to == end_model2.document.id
        self.assertTrue(is_first_the_first_end_model or is_first_the_second_end_model)

        is_second_the_first_end_model = rel2._to == end_model1.document.id
        is_second_the_second_end_model = rel2._to == end_model2.document.id
        self.assertTrue(is_second_the_first_end_model or is_second_the_second_end_model)

        StartModel.destroy()
        EndModel.destroy()
Ejemplo n.º 28
0
    def test_basic_creation_with_default(self):
        class EndModel(CollectionModel):

            test_field = CharField()

        class StartModel(CollectionModel):
            collection_name = 'never_to_be_seen_again'

            others = ManyToManyField(to=EndModel, related_name='starters')

        EndModel.init()
        StartModel.init()

        end_model1 = EndModel()
        end_model1.test_field = 'foo'
        end_model1.save()

        end_model2 = EndModel()
        end_model2.test_field = 'bar'
        end_model2.save()

        start_model = StartModel()
        start_model.others = [end_model1, end_model2]
        start_model.save()

        relation_collection_name = start_model.get_field(
            name='others')._get_relation_collection_name(StartModel)
        col = Collection.get_loaded_collection(name=relation_collection_name)
        relation_documents = col.documents()

        self.assertEqual(len(relation_documents), 2)

        rel1 = relation_documents[0]
        rel2 = relation_documents[1]

        # From is always the same
        self.assertEqual(rel1._from, start_model.document.id)
        self.assertEqual(rel2._from, start_model.document.id)

        is_first_the_first_end_model = rel1._to == end_model1.document.id
        is_first_the_second_end_model = rel1._to == end_model2.document.id
        self.assertTrue(is_first_the_first_end_model
                        or is_first_the_second_end_model)

        is_second_the_first_end_model = rel2._to == end_model1.document.id
        is_second_the_second_end_model = rel2._to == end_model2.document.id
        self.assertTrue(is_second_the_first_end_model
                        or is_second_the_second_end_model)

        StartModel.destroy()
        EndModel.destroy()
Ejemplo n.º 29
0
 def get(self, request, format='json'):
     """
         API View that receives a GET request.
         and will return a single random unused external id.
     """
     obj = Collection.get_loaded_collection(name='AwsExternalIds')
     document = SimpleQuery.get_by_example(obj,
                                           example_data={
                                               'used': False,
                                           },
                                           allow_multiple=True)
     if document:
         external_id = Document(random.choice(document), '',
                                'AwsExternalIds',
                                client.api).retrieve()['external_id']
     else:
         external_id = "Not found"
     return Response(external_id, status=status.HTTP_201_CREATED)
Ejemplo n.º 30
0
    def create(self, validated_data):
        #Init Arango Models
        AwsCredentials.init()
        Companies.init()

        obj = AwsCredentials()
        obj.role_arn = str(validated_data['role_arn'])
        obj.external_id = str(validated_data['external_id'])
        obj.account_name = str(validated_data['account_name'])
        obj.company = Companies.objects.get(_id=str(self.context['company']))
        obj.user = int(self.context['user'])
        obj.save()
        collection = Collection.get_loaded_collection('AwsExternalIds')
        SimpleQuery.update_by_example(collection,
                                      {'external_id': obj.external_id}, {
                                          'used': True,
                                          'company': str(obj.company.id),
                                          'user': obj.user
                                      })
        return obj.id
Ejemplo n.º 31
0
    def validate(self, data):
        collection = Collection.get_loaded_collection(
            name='ScheduleIntervalSettings')
        obj = SimpleQuery.get_by_example(collection,
                                         example_data={
                                             'account_id':
                                             str(data['account_id']),
                                             'service': str(data['service']),
                                         })
        if not obj:
            raise serializers.ValidationError(
                "A setting for the given service with this account not exist")

        AwsCredentials.init()
        Companies.init()
        aws_obj = AwsCredentials.objects.get(_id=str(data['account_id']))
        if str(aws_obj.company.id) != str(self.context['company']):
            raise serializers.ValidationError(
                'Companies account are different for Aws and currenly logged in User'
            )

        return data
Ejemplo n.º 32
0
    def tearDown(self):
        # They need to be deleted
        Collection.remove(name=self.test_1_col.name)
        Collection.remove(name=self.test_2_col.name)

        Database.remove(name=self.database_name)
Ejemplo n.º 33
0
    def create(self, validated_data):
        user = User.objects.create_user(
                    username=validated_data['email'],
                    password=validated_data['password'],
                    email=validated_data['email'],
                    first_name=validated_data['first_name'],
                    last_name=validated_data['last_name'] if 'last_name' in validated_data else '',
                    is_active=False
                )
        try:
            # Saving data into UserProfile collection
            UserProfiles.init()
            obj = UserProfiles()
            obj.user = int(user.id)
            obj.country = str(validated_data['country'])
            obj.state = str(validated_data['state'])
            obj.phone_number = str(validated_data['phone_number'])
            obj.role = str(get_default_role().id)
            obj.save()


            # Saving company into collection
            Companies.init()
            obj = Companies()
            obj.name = str(validated_data['company'])
            obj.save()

            # Assign User to Company
            collection = Collection.get_loaded_collection(name='UserCompanies')
            doc = collection.create_document()
            doc.user = int(user.id)
            doc.company =  str(obj.id)
            doc.active =  True
            doc.created_on = str(datetime.datetime.now())
            doc.save()

        except Exception as e:
            # Removing data saved into document if any error occured
            collection = Collection.get_loaded_collection(name='UserProfiles')
            obj = SimpleQuery.get_by_example(collection, example_data={
                'user': int(user.id),
            })
            if obj:
                Document(obj.id, '', 'UserProfiles', client.api).delete()

            collection = Collection.get_loaded_collection(name='Companies')
            obj = SimpleQuery.get_by_example(collection, example_data={
                'name': str(validated_data['company']),
            })
            if obj:
                Document(obj.id, '', 'Companies', client.api).delete()

            collection = Collection.get_loaded_collection(name='UserCompanies')
            obj = SimpleQuery.get_by_example(collection, example_data={
                'user': int(user.id),
                'company': str(obj.id)
            })
            if obj:
                Document(obj.id, '', 'UserCompanies', client.api).delete()
            raise Exception('Error Occured '+str(e))
        return user
Ejemplo n.º 34
0
def get_default_role():
    obj = Collection.get_loaded_collection(name='Roles')
    return SimpleQuery.get_by_example(obj, example_data={'slug': 'user'})
Ejemplo n.º 35
0
    def init(cls):
        """
        """

        # TODO: Deal with super classes

        name = cls.get_collection_name()

        # Set type
        try:
            collection_type = getattr(cls, 'collection_type')
        except:
            collection_type = 2

        # TODO: Database is not set for the collection

        # Create collection
        try:
            cls.collection_instance = Collection.create(name=name,
                                                        type=collection_type)
        except:
            cls.collection_instance = Collection.get_loaded_collection(
                name=name)

        try:
            if not isinstance(cls.objects, CollectionModelManager):
                cls.objects = cls.objects(cls)
        except:
            pass  # This is the case if init was called more than once

        cls._default_manager = cls.objects

        # Create meta data for collection
        cls._model_meta_data = cls.MetaDataObj()

        if hasattr(cls, 'Meta'):
            cls._meta = cls.Meta()
            cls._meta.model_name = name
            cls._meta.object_name = name

            # Giving other classes the chance to extend the meta data on init
            if hasattr(cls, 'extend_meta_data'):
                cls.extend_meta_data(cls, cls._meta)

        # Go through all fields
        fields_dict = cls.get_collection_fields_dict()
        for attribute_name in fields_dict:
            attribute = fields_dict[attribute_name]
            # Trigger init event
            attribute.on_init(cls, attribute_name)

        # Go through all index
        model_index_list = cls.get_model_fields_index()
        for index_attribute_name in model_index_list:
            # Save created index
            index_obj = model_index_list[index_attribute_name]
            created_index = Index(collection=cls.collection_instance,
                                  index_type_obj=index_obj)
            # Reset class attribute
            setattr(cls, index_attribute_name, created_index)
            # Save index
            created_index.save()

            if not created_index.index_type_obj.is_new:
                created_index.overwrite()
Ejemplo n.º 36
0
 def tearDown(self):
     Collection.remove(name=self.operating_collection)
     Database.remove(name=self.database_name)
Ejemplo n.º 37
0
    def tearDown(self):
	Collection.remove(name=self.collection1_name)
	Collection.remove(name=self.collection2_name)
	Collection.remove(name=self.coll3_name)
	Collection.remove(name=self.coll4_name)
	Database.remove(name=self.database_name)
Ejemplo n.º 38
0
    def validate_company(self, data):
        collection = Collection.get_loaded_collection(name='Companies')
        if SimpleQuery.get_by_example(collection, example_data= {'name': str(data)}):
            raise serializers.ValidationError("Company already exist")

        return data
Ejemplo n.º 39
0
    SimpleQuery.all(collection=big_collection)

@timer_decorator2('Retrieving %s documents via aql from the collection took %s seconds')
def retrieve_aql_documents(big_collection):
    """
    """

    all_aql_query = Query()
    all_aql_query.append_collection(collection_name=big_collection.name)
    all_aql_query.execute()

# Everything needs to be in one try-catch so we can clean up afterwards
try:

    big_collection_name = 'big_collection'
    big_collection = Collection.create(big_collection_name)

    print('All tests are using either %s documents, edges or models' % document_number)

    # Create first all documents
    create_big_number_of_documents(big_collection)

    # Get all documents normally
    retrieve_normally_documents(big_collection)

    # Get documents with simple query
    retrieve_simply_documents(big_collection)

    # Get documents with aql
    retrieve_aql_documents(big_collection)
Ejemplo n.º 40
0
 def tearDown(self):
     Collection.remove(name=self.operating_collection)
     Database.remove(name=self.database_name)