Skip to content

Commit

Permalink
Stats: Move generic cache key calculation to objects
Browse files Browse the repository at this point in the history
This way the code can be reused in other features as well.

Issue #49
  • Loading branch information
nijel committed Jul 28, 2020
1 parent 84effb6 commit 5c9e59a
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 8 deletions.
3 changes: 2 additions & 1 deletion weblate/lang/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from weblate.langdata.plurals import EXTRAPLURALS
from weblate.logger import LOGGER
from weblate.trans.defines import LANGUAGE_CODE_LENGTH, LANGUAGE_NAME_LENGTH
from weblate.trans.mixins import CacheKeyMixin
from weblate.trans.util import sort_choices, sort_objects
from weblate.utils.templatetags.icons import icon
from weblate.utils.validators import validate_plural_formula
Expand Down Expand Up @@ -441,7 +442,7 @@ def setup_lang(sender, **kwargs):
Language.objects.setup(False)


class Language(models.Model):
class Language(models.Model, CacheKeyMixin):
code = models.SlugField(
max_length=LANGUAGE_CODE_LENGTH,
unique=True,
Expand Down
6 changes: 6 additions & 0 deletions weblate/trans/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,9 @@ def get_user_display(self, icon: bool = True):

def get_user_text_display(self):
return get_user_display(self.user, icon=False, link=True)


class CacheKeyMixin:
@cached_property
def cache_key(self):
return "{}-{}".format(self.__class__.__name__, self.pk)
4 changes: 2 additions & 2 deletions weblate/trans/models/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
)
from weblate.trans.exceptions import FileParseError
from weblate.trans.fields import RegexField
from weblate.trans.mixins import PathMixin, URLMixin
from weblate.trans.mixins import CacheKeyMixin, PathMixin, URLMixin
from weblate.trans.models.alert import ALERTS, ALERTS_IMPORT
from weblate.trans.models.change import Change
from weblate.trans.models.translation import Translation
Expand Down Expand Up @@ -234,7 +234,7 @@ def prefetch_source_stats(self):
return self


class Component(FastDeleteMixin, models.Model, URLMixin, PathMixin):
class Component(FastDeleteMixin, models.Model, URLMixin, PathMixin, CacheKeyMixin):
name = models.CharField(
verbose_name=gettext_lazy("Component name"),
max_length=COMPONENT_NAME_LENGTH,
Expand Down
4 changes: 2 additions & 2 deletions weblate/trans/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from weblate.lang.models import Language, get_english_lang
from weblate.memory.tasks import import_memory
from weblate.trans.defines import PROJECT_NAME_LENGTH
from weblate.trans.mixins import PathMixin, URLMixin
from weblate.trans.mixins import CacheKeyMixin, PathMixin, URLMixin
from weblate.utils.data import data_dir
from weblate.utils.db import FastDeleteMixin
from weblate.utils.site import get_site_url
Expand Down Expand Up @@ -60,7 +60,7 @@ def prefetch_project_flags(projects):
return projects


class Project(FastDeleteMixin, models.Model, URLMixin, PathMixin):
class Project(FastDeleteMixin, models.Model, URLMixin, PathMixin, CacheKeyMixin):
ACCESS_PUBLIC = 0
ACCESS_PROTECTED = 1
ACCESS_PRIVATE = 100
Expand Down
4 changes: 2 additions & 2 deletions weblate/trans/models/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
from weblate.trans.checklists import TranslationChecklist
from weblate.trans.defines import FILENAME_LENGTH
from weblate.trans.exceptions import FileParseError, PluralFormsMismatch
from weblate.trans.mixins import LoggerMixin, URLMixin
from weblate.trans.mixins import CacheKeyMixin, LoggerMixin, URLMixin
from weblate.trans.models.change import Change
from weblate.trans.models.suggestion import Suggestion
from weblate.trans.models.unit import (
Expand Down Expand Up @@ -118,7 +118,7 @@ def filter_access(self, user):
)


class Translation(models.Model, URLMixin, LoggerMixin):
class Translation(models.Model, URLMixin, LoggerMixin, CacheKeyMixin):
component = models.ForeignKey("Component", on_delete=models.deletion.CASCADE)
language = models.ForeignKey(Language, on_delete=models.deletion.CASCADE)
plural = models.ForeignKey(Plural, on_delete=models.deletion.CASCADE)
Expand Down
2 changes: 1 addition & 1 deletion weblate/utils/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def has_review(self):

@cached_property
def cache_key(self):
return "stats-{}-{}".format(self._object.__class__.__name__, self._object.pk)
return "stats-{}".format(self._object.cache_key)

def __getattr__(self, name):
if self._data is None:
Expand Down

0 comments on commit 5c9e59a

Please sign in to comment.