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 test_update_document(self): SimpleQuery.update_by_example(collection=self.test_1_col, example_data={'bla': 'xxx'}, new_value={'bla': 'ttt'}) self.col1_doc2.retrieve() self.assertEqual(self.col1_doc2.bla, 'ttt')
def test_update_document(self): SimpleQuery.update_by_example(collection=self.test_1_col, example_data={ 'bla': 'xxx' }, new_value={ 'bla': 'ttt' }) self.col1_doc2.retrieve() self.assertEqual(self.col1_doc2.bla, 'ttt')
def test_replace_document(self): SimpleQuery.replace_by_example(collection=self.test_1_col, example_data={ 'bla': 'xxx' }, new_value={ 'test': 'foo' }) self.col1_doc2.retrieve() self.assertEqual(self.col1_doc2.test, 'foo')
def test_replace_document(self): SimpleQuery.replace_by_example(collection=self.test_1_col, example_data={'bla': 'xxx'}, new_value={'test': 'foo'}) self.col1_doc2.retrieve() self.assertEqual(self.col1_doc2.test, 'foo')
def test_remove_document(self): SimpleQuery.remove_by_example(collection=self.test_1_col, example_data={ 'bla': 'xxx' }) all_docs = self.test_1_col.documents() self.assertEqual(len(all_docs), 1) doc = all_docs[0] self.assertDocumentsEqual(doc, self.col1_doc1)
def test_remove_document(self): SimpleQuery.remove_by_example(collection=self.test_1_col, example_data={'bla': 'xxx'}) all_docs = self.test_1_col.documents() self.assertEqual(len(all_docs), 1) doc = all_docs[0] self.assertDocumentsEqual(doc, self.col1_doc1)
def test_get_no_document(self): doc = SimpleQuery.get_by_example(collection=self.test_1_col, example_data={ '_key': 'dddd', }) self.assertEqual(doc, None)
def test_create_document(self): trans = Transaction(collections={ 'write': [ self.operating_collection, ] }) # Uses already chosen database as usual collection = trans.collection(name=self.operating_collection) collection.create_document(data={ 'test': 'foo' }) ctrl = TransactionController() transaction_result = ctrl.start(transaction=trans) transaction_doc = create_document_from_result_dict(transaction_result['result'], self.test_1_col.api) created_doc = SimpleQuery.get_by_example(self.test_1_col, example_data={ '_id': transaction_doc.id }) self.assertDocumentsEqual(transaction_doc, created_doc)
def test_get_document_by_example(self): uid = self.col1_doc1.key doc = SimpleQuery.get_by_example(collection=self.test_1_col, example_data={ '_key': uid, }) self.assertDocumentsEqual(doc, self.col1_doc1)
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_get_all_documents(self): docs = SimpleQuery.all(collection=self.test_1_col) self.assertEqual(len(docs), 2) doc1 = docs[0] doc2 = docs[1] self.assertNotEqual(doc1, doc2)
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 get(self, **kwargs): """ """ collection = self._model_class.collection_instance doc = SimpleQuery.get_by_example(collection=collection, example_data=kwargs) if doc is None: return None model = self._create_model_from_doc(doc=doc) return model
def test_int32_doc_save(self): for x in xrange(300): doc = self.col1.create_document() doc.int32 = 2**32 doc.int32_1 = 2**32 - 1 doc.int31 = 2**31 doc.int31_1 = 2**31 - 1 doc.save() docs = SimpleQuery.all(self.col1) self.assertEqual(len(docs),300) for x in xrange(len(docs)): self.assertEqual(docs[x].int32,2**32) self.assertEqual(docs[x].int32_1,2**32 - 1) self.assertEqual(docs[x].int31,2**31) self.assertEqual(docs[x].int31_1,2**31 - 1)
def test_dec10_save(self): for i in xrange(100): doc = self.col3.create_document() doc.dec10 = 10**10; doc.dec10_1 = 10**10 - 1 doc.dec9 = 10**9 doc.dec9_1 = 10**9 - 1 doc.save() docs = SimpleQuery.all(self.col3) self.assertEqual(len(docs),100) for j in xrange(len(docs)): self.assertEqual(docs[j].dec10,10**10) self.assertEqual(docs[j].dec10_1,10**10 - 1) self.assertEqual(docs[j].dec9,10**9) self.assertEqual(docs[j].dec9_1,10**9 - 1)
def test_dec19_save(self): for i in xrange(100): doc = self.col4.create_document() doc.dec18 = 10**15; doc.dec18_1 = 10**15 - 1 doc.dec17 = 10**14 doc.dec17_1 = 10**14 - 1 doc.save() docs = SimpleQuery.all(self.col4) self.assertEqual(len(docs),100) for j in xrange(len(docs)): self.assertEqual(docs[j].dec18,10**15) self.assertEqual(docs[j].dec18_1,10**15 - 1) self.assertEqual(docs[j].dec17,10**14) self.assertEqual(docs[j].dec17_1,10**14 - 1)
def test_int64_doc_save(self): for y in xrange(300): doc = self.col2.create_document() doc.int62 = 2**52 doc.int62_1 = 2**52 - 1 doc.int63 = 2**53 doc.int63_1 = 2**53 - 1 doc.save() docs = SimpleQuery.all(self.col2) self.assertEqual(len(docs),300) for y in xrange(len(docs)): self.assertEqual(docs[y].int62,2**52) self.assertEqual(docs[y].int62_1,2**52 - 1) self.assertEqual(docs[y].int63,2**53) self.assertEqual(docs[y].int63_1,2**53 - 1)
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 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 test_create_document(self): trans = Transaction( collections={'write': [ self.operating_collection, ]}) # Uses already chosen database as usual collection = trans.collection(name=self.operating_collection) collection.create_document(data={'test': 'foo'}) ctrl = TransactionController() transaction_result = ctrl.start(transaction=trans) transaction_doc = create_document_from_result_dict( transaction_result['result'], self.test_1_col.api) created_doc = SimpleQuery.get_by_example( self.test_1_col, example_data={'_id': transaction_doc.id}) self.assertDocumentsEqual(transaction_doc, created_doc)
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
def retrieve_simply_documents(big_collection): """ """ SimpleQuery.all(collection=big_collection)
def get_default_role(): obj = Collection.get_loaded_collection(name='Roles') return SimpleQuery.get_by_example(obj, example_data={'slug': 'user'})
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