def test_basic_callback(self): def callback(o, arg, data=None): self.assertEqual(o.context, 'event.context:before') self.assertEqual(arg, 'eventData') self.assertEqual(data, 'userData') on('event.context:before', callback, 'userData')
def connect_to_create_instance(self, entity): """ Create new contact records when creating a new instance of given entity. """ # scope variable is shared between callbacks scope = {} # catch the incoming data to use after entity instance is created def catch_incoming_data(o, data): scope = {} scope['memberData'] = None if 'members' in data: scope['membertData'] = data.pop('members', []) # create contacts from given data and associate with entity instance def create_members(o, instance): members = [] for personId in scope['memberData']: member = {'progenitor': instance.moniker(), 'person': personId} members.append(member) serializer = MemberSerializer(data=members, many=True) if serializer.is_valid(): recordSet = serializer.save() # register callbacks on(entity + '.create:before', catch_incoming_data) on(entity + '.create:success', create_contacts)
def connect_to_create_instance(self, entity): """ Create new contact records when creating a new instance of given entity. """ # scope variable is shared between callbacks scope = {} # catch the incoming data to use after entity instance is created def catch_incoming_data(o, data): scope['contactData'] = None if 'contacts' in data: scope['contactData'] = data.pop('contacts', []) # create contacts from given data and associate with entity instance def create_contacts(o, instance): for contact in scope['contactData']: contact['progenitor'] = instance.moniker() serializer = ContactSerializer(data=scope['contactData'], many=True) if serializer.is_valid(): recordSet = serializer.save() # register callbacks on(entity + '.create:before', catch_incoming_data) on(entity + '.create:success', create_contacts)
def create_contacts(o,instance): for contact in scope['contactData']: contact['progenitor'] = instance.moniker() serializer = ContactSerializer(data=scope['contactData'], many=True) if serializer.is_valid(): recordSet = serializer.save() # register callbacks on(entity+'.create:before',catch_incoming_data) on(entity+'.create:success',create_contacts)
def connect_to_people(): def before(request, data, scope): scope = {} scope['concats'] = data.pop('contacts') def success(request, instance, scope): for i in trigger.contactData: i.progenitor = instance.moniker() serializer = ContactSerializer(data=breath.contactData, multiple=True) on('agape.authentication.user.create:before', before, scope) on('agape.authentication.user.create:success', success, scope)
def connect_to_delete_instance(entity): # scope variable is shared between callbacks scope = {} def before(o,instance): scope['query'] = Contacts.objects.filter(progenitor=instance.moniker()) def delete_contacts(o,instance): scope['query'].delete() on(entity+'.delete:before',before) on(entity+'.delete:success',delete_contacts)
def connect_to_delete_instance(entity): # scope variable is shared between callbacks scope = {} # get the moniker of the entity being deleted def before(o, instance): scope['query'] = Files.objects.filter( progenitor=instance.moniker()) # delete all files associated with the moniker def delete_files(o, instance): scope['query'].delete() # attach signals on(entity + '.delete:before', before) on(entity + '.delete:success', delete_files)
def test_create_signals(request): scope = {} def on_request(observer,request,*args,**kwargs): scope['request'] = True def on_before(observer,data): scope['before'] = True def on_success(observer,instance): scope['success'] = True def on_serialize(observer,data): scope['serialize'] = True on('user.create:request',on_request )
def connect_to_delete_instance(entity): """ When an entity(e.g., an organization) is deleted, remove all of the associated events """ # scope variable is shared between callbacks scope = {} # find all the events associated with the entity def before(o, instance): scope['query'] = Event.objects.filter( progenitor=instance.moniker()) # perform the delete operation def delete_children(o, instance): scope['query'].delete() # connect to the delete signals on(entity + '.delete:before', before) on(entity + '.delete:success', delete_children)
def test_destroy_callback(self): scope = {'counter': 0} def callback(context, modifier): self.assertEqual(o.context, 'event.context:after') scope['counter'] = scope['counter'] + modifier o = on('event.context:after', callback) trigger('event.context:after', 1) self.assertEqual(scope['counter'], 1) o.destroy() trigger('event.context:after', 1) self.assertEqual(scope['counter'], 1)
def on_user_create_before(event, data): print('CREATE BEFORE') scope['organization_data'] = data.pop('organization', None) scope['person_data'] = data.pop('person_data', None) def on_user_create_success(event, user, *args, **kwargs): # create an organization if scope.get('organization_data'): data = scope.get('organization_data') serializer = OrganizationSerializer(data=data) serializer.is_valid(raise_exception=True) scope['organization'] = serializer.save() print('CREATE SUCCESS') # give this person all privileges on organization # from guardian.shortcuts import assign_perm # # assign_perm('view_organization', user, scope['organization']) # assign_perm('change_organization', user, scope['organization']) # assign_perm('delete_organization', user, scope['organization']) on('user.create:before', on_user_create_before) on('user.create:success', on_user_create_success)
def test_signals(self): scope = {} def on_create_before(self, data): scope['on_create_before'] = True def on_create_success(self, instance): scope['on_create_success'] = True def on_retrieve_before(self, data): scope['on_retrieve_before'] = True def on_retrieve_success(self, instance): scope['on_retrieve_success'] = True def on_update_before(self, data): scope['on_update_before'] = True def on_update_success(self, instance): scope['on_update_success'] = True def on_delete_before(self, data): scope['on_delete_before'] = True def on_delete_success(self, instance): scope['on_delete_success'] = True on(self.entity + '.create:before', on_create_before) on(self.entity + '.create:success', on_create_success) on(self.entity + '.retrieve:before', on_retrieve_before) on(self.entity + '.retrieve:success', on_retrieve_success) on(self.entity + '.update:before', on_update_before) on(self.entity + '.update:success', on_update_success) on(self.entity + '.delete:before', on_delete_before) on(self.entity + '.delete:success', on_delete_success) # create new record response = self.client.post(self.api_end_point, self.create_data) self.assertEqual(response.status_code, 201, "Created new instance") self.assertTrue(scope['on_create_before'], '.create:before') self.assertTrue(scope['on_create_success'], '.create:before') # rerieve the record instance_id = response.data.get('id') uri = "{}{}/".format(self.api_end_point, instance_id) self.assertEqual(response.status_code, 201, "Retrieved") # self.assertTrue(scope['on_retrieve_before'], '.retrieve:before') self.assertTrue(scope['on_retrieve_success'], '.retrieve:before') # update a record response = self.client.patch(uri, json.dumps(self.update_data), content_type='application/json') self.assertEqual(response.status_code, 200, "Updated instance") self.assertTrue(scope['on_update_before'], '.update:before') self.assertTrue(scope['on_update_success'], '.update:before') # delete response = self.client.delete(uri) self.assertEqual(response.status_code, 204, "Deleted") self.assertTrue(scope['on_delete_before'], '.delete:before') self.assertTrue(scope['on_delete_success'], '.delete:before')