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_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 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 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_no_document(self): doc = SimpleQuery.get_by_example(collection=self.test_1_col, example_data={ '_key': 'dddd', }) self.assertEqual(doc, None)
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 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 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 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
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