Compare commits

...

2 commits

Author SHA1 Message Date
78be4d2728 cache roles on characters 2024-01-10 10:24:14 +01:00
2fec5b32da fix cap points for prestige 2024-01-10 10:20:14 +01:00
5 changed files with 52 additions and 4 deletions

View file

@ -25,7 +25,7 @@
<excludeFolder url="file://$MODULE_DIR$/.ruff_cache" /> <excludeFolder url="file://$MODULE_DIR$/.ruff_cache" />
<excludeFolder url="file://$MODULE_DIR$/.venv" /> <excludeFolder url="file://$MODULE_DIR$/.venv" />
</content> </content>
<orderEntry type="jdk" jdkName="Python 3.11 (charasheet)" jdkType="Python SDK" /> <orderEntry type="jdk" jdkName="Python 3.12 (charasheet)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
<component name="PyDocumentationSettings"> <component name="PyDocumentationSettings">

View file

@ -3,5 +3,5 @@
<component name="Black"> <component name="Black">
<option name="sdkName" value="Python 3.11 (charasheet)" /> <option name="sdkName" value="Python 3.11 (charasheet)" />
</component> </component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (charasheet)" project-jdk-type="Python SDK" /> <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (charasheet)" project-jdk-type="Python SDK" />
</project> </project>

View file

@ -130,7 +130,7 @@ class Capability(DocumentedModel, TimeStampedModel, models.Model):
@property @property
def capability_points_cost(self) -> int: def capability_points_cost(self) -> int:
if self.rank in [1, 2]: if self.path.category == Path.Category.PROFILE and self.rank in [1, 2]:
return 1 return 1
return 2 return 2

View file

@ -14,6 +14,7 @@ from django_extensions.db.models import TimeStampedModel
from character.models import Capability, Path from character.models import Capability, Path
from character.models.dice import Dice from character.models.dice import Dice
from character.models.equipment import Weapon from character.models.equipment import Weapon
from character.models.utils import cache_on_instance
from common.models import DocumentedModel, UniquelyNamedModel from common.models import DocumentedModel, UniquelyNamedModel
@ -413,7 +414,13 @@ class Character(models.Model):
@property @property
def capability_points_used(self) -> int: def capability_points_used(self) -> int:
return sum(cap.capability_points_cost for cap in self.capabilities.only("rank")) return sum(
cap.capability_points_cost
for cap in self.capabilities.select_related("path").only(
"rank",
"path__category",
)
)
@property @property
def capability_points_remaining(self) -> int: def capability_points_remaining(self) -> int:
@ -465,12 +472,15 @@ class Character(models.Model):
pk__in=self.states.all().values_list("pk", flat=True), pk__in=self.states.all().values_list("pk", flat=True),
) )
@cache_on_instance()
def managed_by(self, user): def managed_by(self, user):
return self in Character.objects.managed_by(user) return self in Character.objects.managed_by(user)
@cache_on_instance()
def mastered_by(self, user): def mastered_by(self, user):
return self in Character.objects.mastered_by(user) return self in Character.objects.mastered_by(user)
@cache_on_instance()
def owned_by(self, user): def owned_by(self, user):
return self in Character.objects.owned_by(user) return self in Character.objects.owned_by(user)

View file

@ -0,0 +1,38 @@
import functools
def cache_on_instance():
def wrapper(func):
@functools.wraps(func)
def inner(self, arg):
cache_name = func.__name__
if (cached := _get_cache_value(self, cache_name, arg)) is not None:
return cached
res = func(self, arg)
_set_cache_value(self, cache_name, arg, res)
return res
return inner
return wrapper
def _get_cache_value(obj, cache_name: str, key: str) -> any:
cache_name += "_cache"
cache = getattr(obj, cache_name, None)
if cache is None:
cache = {}
setattr(obj, cache_name, cache)
return None
return cache.get(key)
def _set_cache_value(obj, cache_name: str, key: str, value: any):
cache_name += "_cache"
cache = getattr(obj, cache_name)
if cache is None:
cache = {}
setattr(obj, cache_name, cache)
cache[key] = value