core: cache user's is_superuser

This commit is contained in:
Jens Langhammer 2020-09-25 23:59:06 +02:00
parent 03d58b439f
commit 6e4ce8dbaa
1 changed files with 3 additions and 2 deletions

View File

@ -9,6 +9,7 @@ from django.db import models
from django.db.models import Q, QuerySet from django.db.models import Q, QuerySet
from django.forms import ModelForm from django.forms import ModelForm
from django.http import HttpRequest from django.http import HttpRequest
from django.utils.functional import cached_property
from django.utils.timezone import now from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from guardian.mixins import GuardianUserMixin from guardian.mixins import GuardianUserMixin
@ -87,7 +88,7 @@ class User(GuardianUserMixin, AbstractUser):
final_attributes.update(group.attributes) final_attributes.update(group.attributes)
return final_attributes return final_attributes
@property @cached_property
def is_superuser(self) -> bool: def is_superuser(self) -> bool:
"""Get supseruser status based on membership in a group with superuser status""" """Get supseruser status based on membership in a group with superuser status"""
return self.pb_groups.filter(is_superuser=True).exists() return self.pb_groups.filter(is_superuser=True).exists()
@ -95,7 +96,7 @@ class User(GuardianUserMixin, AbstractUser):
@property @property
def is_staff(self) -> bool: def is_staff(self) -> bool:
"""superuser == staff user""" """superuser == staff user"""
return self.is_superuser return self.is_superuser # type: ignore
def set_password(self, password, signal=True): def set_password(self, password, signal=True):
if self.pk and signal: if self.pk and signal: