From 6c5aba62ecd3f35170902870c038fefda5cbced9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20Kl=C3=BCnder?= Date: Fri, 29 Nov 2024 16:24:35 +0100 Subject: [PATCH] cache as_operations --- src/c3nav/editor/models/changeset.py | 23 ++++++++++++++++++++++- src/c3nav/editor/views/base.py | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/c3nav/editor/models/changeset.py b/src/c3nav/editor/models/changeset.py index 38f9afdd..264ed9e9 100644 --- a/src/c3nav/editor/models/changeset.py +++ b/src/c3nav/editor/models/changeset.py @@ -3,6 +3,7 @@ from contextlib import contextmanager from django.apps import apps from django.conf import settings +from django.core.cache import cache from django.core.exceptions import FieldDoesNotExist from django.db import models, transaction from django.urls import reverse @@ -12,7 +13,8 @@ from django.utils.translation import gettext_lazy as _ from django.utils.translation import ngettext_lazy from django_pydantic_field import SchemaField -from c3nav.editor.changes import ChangedObjectCollection +from c3nav.editor.changes import ChangedObjectCollection, ChangeProblems +from c3nav.editor.operations import DatabaseOperationCollection from c3nav.editor.tasks import send_changeset_proposed_notification from c3nav.mapdata.models import LocationSlug, MapUpdate from c3nav.mapdata.models.locations import LocationRedirect @@ -454,3 +456,22 @@ class ChangeSet(models.Model): @property def style(self): return self.STATE_STYLES[self.state] + + def get_changes_as_operations(self) -> ChangedObjectCollection.ChangesAsOperations: + """ + preferably don't use this one but use as_operations or problems + """ + cache_key = '%s:changes_as_operations' % self.cache_key_by_changes + changes_as_operations = cache.get(cache_key) + if changes_as_operations: + changes_as_operations = self.changes.as_operations + cache.set(cache_key, changes_as_operations, 900) + return changes_as_operations + + @property + def as_operations(self) -> DatabaseOperationCollection: + return self.get_changes_as_operations().operations + + @property + def problems(self) -> ChangeProblems: + return self.get_changes_as_operations().problems diff --git a/src/c3nav/editor/views/base.py b/src/c3nav/editor/views/base.py index 22ba2add..835a88e4 100644 --- a/src/c3nav/editor/views/base.py +++ b/src/c3nav/editor/views/base.py @@ -73,7 +73,7 @@ def accesses_mapdata(func): # For non-direct editing, we will interact with the changeset with maybe_lock_changeset_to_edit(request=request): # Turn the changes from the changeset into a list of operations - operations, problems = request.changeset.changes.as_operations # todo: cache this + operations = request.changeset.as_operations # Enable the overlay manager, temporarily applying the changeset changes # commit is set to false, meaning all changes will be reset once we leave the manager