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
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
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)
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
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()
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()
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()
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()
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)
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)
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)
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)
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)
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)
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
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()
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
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()
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
def get_default_role(): obj = Collection.get_loaded_collection(name='Roles') return SimpleQuery.get_by_example(obj, example_data={'slug': 'user'})
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