def wrapper(cls): arg_decs = [] if cls.select_for_update is not None: # pragma: no branch arg_decs = [arg.contexts(transaction.atomic)] arg_decs += [ arg.defaults( **{ cls.objects_arg: arg.first( 'objects', daf.contrib.single_list('object'), cls.objects_arg, ) }), arg.defaults( **{ cls.objects_arg: djarg.qset( cls.objects_arg, qset=cls.queryset, select_for_update=cls.select_for_update, ) }), super().wrapper, ] return arg.s(*arg_decs)
def wrapper(cls): return arg.s( arg.contexts(trapped_errors=daf.contrib.raise_trapped_errors), arg.defaults( objects=arg.first( 'objects', daf.contrib.single_list('object'), daf.contrib.single_list(cls.object_arg), ) ), arg.defaults(objects=djarg.qset('objects', qset=cls.queryset)), arg.parametrize(**{cls.object_arg: arg.val('objects')}), arg.contexts(daf.contrib.trap_errors), super().wrapper, )
def test_s_arg_wrapper(): """Tests the arg.s wrapper utility Simulates the same test scenario as test_validators_with_defaults() """ def fails(arg1): """A validator that always passes""" if arg1 == 'UPPER': raise ValueError def my_func(arg1): return arg1 my_func_runner = arg.s( arg.defaults(arg1=arg.val('arg1').upper()), arg.validators(fails) ) with pytest.raises(ValueError): my_func_runner(my_func)(arg1='upper') assert my_func_runner(my_func)(arg1='lower') == 'LOWER' assert isinstance(arg.s()(my_func), arg.Args) assert arg.s()(my_func)('hello') == 'hello'
class GrantStaffObjectView(djarg.views.SuccessMessageMixin, djarg.views.ObjectFormView): model = User func = arg.defaults(user=arg.val('object'), granter=arg.val('request').user)(grant_staff_access) template_name = 'tests/grant_staff_access.html' form_class = GrantAccessObjectForm success_url = '.'
def test_grant_staff_access_drf_action(api_client, mocker): """Run the GrantStaffAccess DRF action""" actor = ddf.G(auth_models.User, is_superuser=True) actor.set_password('password') actor.save() user = ddf.G(auth_models.User, is_staff=False) grant_staff_access = daf.registry.get('tests.grant_staff_access') detail_view_url_name = ( 'user-' + grant_staff_access.interfaces['rest_framework.detail_action'].url_name) url = urls.reverse(detail_view_url_name, kwargs={'pk': user.id}) api_client.force_login(actor) # Perform a run where form validation fails resp = api_client.post(url, data={'date_granted': 'invalid'}) assert resp.status_code == 400 assert resp.json() == {'date_granted': ['Enter a valid date/time.']} # Perform a successful run resp = api_client.post(url, data={'is_staff': True}) assert resp.status_code == 200 assert resp.json() == { 'email': user.email, 'id': user.id, 'username': user.username, 'is_staff': True, } # Make sure refetching for serialization works mocker.patch.object(GrantStaffAccessObjectDRFAction, 'refetch_for_serialization', False) resp = api_client.post(url, data={'is_staff': True}) assert resp.json() == { 'email': user.email, 'id': user.id, 'username': user.username, 'is_staff': True, } # Make sure refetching for serialization works even without using # a parametrized wrapper mocker.patch.object( daf.actions.ObjectAction, 'wrapper', arg.defaults(user=arg.val('object')), ) resp = api_client.post(url, data={'is_staff': True}) assert resp.json() == { 'email': user.email, 'id': user.id, 'username': user.username, 'is_staff': True, }
def wrapper(cls): return arg.s( arg.defaults( **{ cls.objects_arg: arg.first( 'objects', daf.contrib.single_list('object'), cls.objects_arg, ) } ), arg.defaults( **{ cls.objects_arg: djarg.qset( cls.objects_arg, qset=cls.queryset ) } ), super().wrapper, )
def wrapper(cls): arg_decs = [] if cls.select_for_update is not None: # pragma: no branch arg_decs = [arg.contexts(transaction.atomic)] arg_decs += [ arg.contexts(trapped_errors=daf.contrib.raise_trapped_errors), arg.defaults(objects=arg.first( 'objects', daf.contrib.single_list('object'), daf.contrib.single_list(cls.object_arg), )), arg.defaults(objects=djarg.qset( 'objects', qset=cls.queryset, select_for_update=cls.select_for_update, )), arg.parametrize(**{cls.object_arg: arg.val('objects')}), arg.contexts(daf.contrib.trap_errors), super().wrapper, ] return arg.s(*arg_decs)
class GrantStaffObjectWizardView(djarg.views.SuccessMessageMixin, djarg.views.SessionObjectWizardView): model = User func = arg.defaults(user=arg.val('object'))(grant_staff_access) template_name = 'tests/grant_staff_access_wizard.html' form_list = [ # We no longer need the first step. The user is provided by the # object view GrantAccessStep2, GrantAccessStep3, GrantAccessStep4, ] success_url = '.' def get_success_message(self, args, results): return f'Successfully granted access to {args["object"]}.'