#32782: Optimize _get_user_permissions by using set comprehensions
-------------------------------------+-------------------------------------
               Reporter:  Abhyudai   |          Owner:  nobody
                   Type:             |         Status:  new
  Cleanup/optimization               |
              Component:             |        Version:  3.2
  Uncategorized                      |
               Severity:  Normal     |       Keywords:  permissions, auth
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  1
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 The intended patch

 {{{
 diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py
 index 5f092f0ae8..8f7b65322c 100644
 --- a/django/contrib/auth/models.py
 +++ b/django/contrib/auth/models.py
 @@ -191,13 +191,13 @@ class UserManager(BaseUserManager):

  # A few helper functions for common logic between User and AnonymousUser.
  def _user_get_permissions(user, obj, from_name):
 -    permissions = set()
      name = 'get_%s_permissions' % from_name
 -    for backend in auth.get_backends():
 -        if hasattr(backend, name):
 -            permissions.update(getattr(backend, name)(user, obj))
 -    return permissions
 -
 +    return {
 +        permission
 +        for backend in auth.get_backends()
 +        if hasattr(backend, name)
 +        for permission in getattr(backend, name)(user, obj)
 +    }

  def _user_has_perm(user, perm, obj):
      """

 }}}


 Since the function `_get_user_permissions` is used by almost all
 permission related methods, this change could potentially reduce the time
 taken for it.

 If it seems acceptable, I would love to send a pull request.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32782>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/053.a8bdf6d5500f902c3a69f58a9c83dc28%40djangoproject.com.

Reply via email to