Example #1
0
    def post(self, request, user_id):
        post_body = request.POST
        status_code = status.HTTP_200_OK
        invalid = False
        form = PermissionBuilderForm(post_body)
        add_another = True if post_body.get('add_another') == "true" else False

        if form.is_valid():
            response = self.managed_user.add_permission(form, self.user.auth_token)

            if response.ok:
                if add_another:
                    form = PermissionBuilderForm()
                else:
                    return redirect('/users/%s/manage' % self.managed_user.user_id)
            else:
                invalid = True
                log_api_error('permission creation', response.text)
                status_code = response.status_code

        else:

            invalid = True
            status_code = status.HTTP_400_BAD_REQUEST

        context = self.__set_add_permission_context(form, invalid)

        return render(request, self.template, context, status=status_code)
Example #2
0
 def test_is_valid_returns_false_when_no_role_given(self):
     form_content = {
         'role': None,
         'permission_level': 'national',
         'region': None,
         'trust': None
     }
     form = PermissionBuilderForm(form_content)
     result = form.is_valid()
     self.assertIsFalse(result)
     self.assertEqual(form.role_error, messages.FIELD_MISSING % "a role")
Example #3
0
 def test_is_valid_returns_true_when_role_at_trust_level_given_with_trust_given(
         self):
     form_content = {
         'role': 'sa',
         'permission_level': 'trust',
         'region': None,
         'trust': '1'
     }
     form = PermissionBuilderForm(form_content)
     result = form.is_valid()
     self.assertIsTrue(result)
     self.assertEqual(form.role_error, None)
     self.assertEqual(form.permission_level_error, None)
     self.assertEqual(form.region_error, None)
     self.assertEqual(form.trust_error, None)
Example #4
0
    def post(self, request, user_id, permission_id):
        post_body = request.POST
        form = PermissionBuilderForm(post_body)

        if form.is_valid():
            response = self.managed_user.update_permission(form, permission_id, self.user.auth_token)

            if response.ok:
                return redirect('/users/%s/manage' % self.managed_user.user_id)
            else:
                invalid = True
                log_api_error('permission creation', response.text)
                status_code = response.status_code
        else:
            invalid = True
            status_code = status.HTTP_400_BAD_REQUEST

        permission = Permission.load_by_id(user_id, permission_id, self.user.auth_token)
        context = self.__set_edit_permission_context(permission, invalid, posted_form=form)

        return render(request, self.template, context, status=status_code)