#28140: Add convenience manager method to `Permission`s
------------------------------------------+------------------------
               Reporter:  Flavio Curella  |          Owner:  nobody
                   Type:  New feature     |         Status:  new
              Component:  contrib.auth    |        Version:  1.11
               Severity:  Normal          |       Keywords:
           Triage Stage:  Unreviewed      |      Has patch:  0
    Needs documentation:  0               |    Needs tests:  0
Patch needs improvement:  0               |  Easy pickings:  0
                  UI/UX:  0               |
------------------------------------------+------------------------
 When writing tests checking for permissions, it's commonplace to test if a
 user can access a particular view, check for failure, add the necessary
 permission, then check again for success:

 {{{
 def mytest(self):
     user = User.objects.create_user(
             username='regularuser',
             password='regularuser',
     )
     response = self.client.get('/mymodel/create/')
     # Usually redirects to some other page, such as `/login/`
     self.assertNotEqual(response.status_code, 200)

     # then we fetch the 'myapp.add_mymodel' permission and add it to
 `user`
     my_permission = Permission.objects.get_by_natural_key('add_mymodel',
 'myapp', 'mymodel')
     user.user_permissions.add(mypermission)

     response = self.client.get('/mymodel/create/')
     # now it works
     self.assertEqual(response.status_code, 200)

 }}}

 I'm wondering if we should have a more convenient way to add permissions.
 I'm thinking of adding a `add_perm` method to the `User` model, that
 mirrors the already existing
 
[https://docs.djangoproject.com/en/1.11/ref/contrib/auth/#django.contrib.auth.models.User.has_perm
 `has_perm`] so that instead of

 {{{
     my_permission = Permission.objects.get_by_natural_key('add_mymodel',
 'myapp', 'mymodel')
     user.user_permissions.add(mypermission)

     response = self.client.get('/mymodel/create/')
     self.assertEqual(response.status_code, 200)
 }}}

 we can have:

 {{{
     user.add_perm('myapp.add_mymodel')
     response = self.client.get('/mymodel/create/')
     self.assertEqual(response.status_code, 200)
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28140>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/051.d845f4033093b256986a6bc700a57f73%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to