From 6e4ce8dbaabbc4bdb3a8ac747e2989f55532161d Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Fri, 25 Sep 2020 23:59:06 +0200 Subject: [PATCH] core: cache user's is_superuser --- passbook/core/models.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/passbook/core/models.py b/passbook/core/models.py index 19d31b026..0bf89dde0 100644 --- a/passbook/core/models.py +++ b/passbook/core/models.py @@ -9,6 +9,7 @@ from django.db import models from django.db.models import Q, QuerySet from django.forms import ModelForm from django.http import HttpRequest +from django.utils.functional import cached_property from django.utils.timezone import now from django.utils.translation import gettext_lazy as _ from guardian.mixins import GuardianUserMixin @@ -87,7 +88,7 @@ class User(GuardianUserMixin, AbstractUser): final_attributes.update(group.attributes) return final_attributes - @property + @cached_property def is_superuser(self) -> bool: """Get supseruser status based on membership in a group with superuser status""" return self.pb_groups.filter(is_superuser=True).exists() @@ -95,7 +96,7 @@ class User(GuardianUserMixin, AbstractUser): @property def is_staff(self) -> bool: """superuser == staff user""" - return self.is_superuser + return self.is_superuser # type: ignore def set_password(self, password, signal=True): if self.pk and signal: