Make use of cached_property in User model

This commit is contained in:
Skia 2017-02-24 01:50:14 +01:00
parent c66b9b0512
commit 1e95fb6873

View File

@ -10,6 +10,8 @@ from django.conf import settings
from django.db import transaction from django.db import transaction
from django.contrib.staticfiles.storage import staticfiles_storage from django.contrib.staticfiles.storage import staticfiles_storage
from django.utils.html import escape from django.utils.html import escape
from django.utils.functional import cached_property
from phonenumber_field.modelfields import PhoneNumberField from phonenumber_field.modelfields import PhoneNumberField
from datetime import datetime, timedelta, date from datetime import datetime, timedelta, date
@ -230,25 +232,25 @@ class User(AbstractBaseUser):
return True return True
return self.groups.filter(name=group_name).exists() return self.groups.filter(name=group_name).exists()
@property @cached_property
def is_root(self): def is_root(self):
return self.is_superuser or self.groups.filter(id=settings.SITH_GROUP_ROOT_ID).exists() return self.is_superuser or self.groups.filter(id=settings.SITH_GROUP_ROOT_ID).exists()
@property @cached_property
def is_board_member(self): def is_board_member(self):
from club.models import Club from club.models import Club
return Club.objects.filter(unix_name=settings.SITH_MAIN_CLUB['unix_name']).first().get_membership_for(self) return Club.objects.filter(unix_name=settings.SITH_MAIN_CLUB['unix_name']).first().get_membership_for(self)
@property @cached_property
def is_launderette_manager(self): def is_launderette_manager(self):
from club.models import Club from club.models import Club
return Club.objects.filter(unix_name=settings.SITH_LAUNDERETTE_MANAGER['unix_name']).first().get_membership_for(self) return Club.objects.filter(unix_name=settings.SITH_LAUNDERETTE_MANAGER['unix_name']).first().get_membership_for(self)
@property @cached_property
def is_banned_alcohol(self): def is_banned_alcohol(self):
return self.is_in_group(settings.SITH_GROUP_BANNED_ALCOHOL_ID) return self.is_in_group(settings.SITH_GROUP_BANNED_ALCOHOL_ID)
@property @cached_property
def is_banned_counter(self): def is_banned_counter(self):
return self.is_in_group(settings.SITH_GROUP_BANNED_COUNTER_ID) return self.is_in_group(settings.SITH_GROUP_BANNED_COUNTER_ID)
@ -422,11 +424,11 @@ class User(AbstractBaseUser):
escape(self.get_display_name()), escape(self.get_display_name()),
) )
@property @cached_property
def subscribed(self): def subscribed(self):
return self.is_in_group(settings.SITH_MAIN_MEMBERS_GROUP) return self.is_in_group(settings.SITH_MAIN_MEMBERS_GROUP)
@property @cached_property
def forum_infos(self): def forum_infos(self):
try: try:
return self._forum_infos return self._forum_infos
@ -666,12 +668,12 @@ class SithFile(models.Model):
else: else:
return super(SithFile, self).__getattribute__(attr) return super(SithFile, self).__getattribute__(attr)
@property @cached_property
def as_picture(self): def as_picture(self):
from sas.models import Picture from sas.models import Picture
return Picture.objects.filter(id=self.id).first() return Picture.objects.filter(id=self.id).first()
@property @cached_property
def as_album(self): def as_album(self):
from sas.models import Album from sas.models import Album
return Album.objects.filter(id=self.id).first() return Album.objects.filter(id=self.id).first()