Compare commits

..

No commits in common. "78be4d2728b139a4b501e6b52fe64c25a352ad09" and "acd0ade1ad39d4c95edf3f84d11b5ddf564865b0" have entirely different histories.

5 changed files with 4 additions and 52 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.12 (charasheet)" jdkType="Python SDK" /> <orderEntry type="jdk" jdkName="Python 3.11 (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.12 (charasheet)" project-jdk-type="Python SDK" /> <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (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.path.category == Path.Category.PROFILE and self.rank in [1, 2]: if self.rank in [1, 2]:
return 1 return 1
return 2 return 2

View file

@ -14,7 +14,6 @@ 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
@ -414,13 +413,7 @@ class Character(models.Model):
@property @property
def capability_points_used(self) -> int: def capability_points_used(self) -> int:
return sum( return sum(cap.capability_points_cost for cap in self.capabilities.only("rank"))
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:
@ -472,15 +465,12 @@ 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

@ -1,38 +0,0 @@
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