From ce99a949c7fb6447c66b5ee230450d80ea42d6b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Tue, 11 Oct 2016 14:15:46 +0200 Subject: [PATCH] Adding cache to HistoryEntry values_diff --- .../0013_historyentry_values_diff_cache.py | 21 +++++++++++++++++++ taiga/projects/history/models.py | 13 ++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 taiga/projects/history/migrations/0013_historyentry_values_diff_cache.py diff --git a/taiga/projects/history/migrations/0013_historyentry_values_diff_cache.py b/taiga/projects/history/migrations/0013_historyentry_values_diff_cache.py new file mode 100644 index 00000000..1fe73a96 --- /dev/null +++ b/taiga/projects/history/migrations/0013_historyentry_values_diff_cache.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.2 on 2016-10-11 12:17 +from __future__ import unicode_literals + +from django.db import migrations +import django_pgjson.fields + + +class Migration(migrations.Migration): + + dependencies = [ + ('history', '0012_auto_20160629_1036'), + ] + + operations = [ + migrations.AddField( + model_name='historyentry', + name='values_diff_cache', + field=django_pgjson.fields.JsonField(blank=True, default=None, null=True), + ), + ] diff --git a/taiga/projects/history/models.py b/taiga/projects/history/models.py index 4697875c..c4155eee 100644 --- a/taiga/projects/history/models.py +++ b/taiga/projects/history/models.py @@ -60,6 +60,9 @@ class HistoryEntry(models.Model): # Stores the last diff diff = JsonField(null=True, blank=True, default=None) + # Stores the values_diff cache + values_diff_cache = JsonField(null=True, blank=True, default=None) + # Stores the last complete frozen object snapshot snapshot = JsonField(null=True, blank=True, default=None) @@ -135,8 +138,11 @@ class HistoryEntry(models.Model): if user: version["user"] = UserSerializer(user).data - @cached_property + @property def values_diff(self): + if self.values_diff_cache is not None: + return self.values_diff_cache + result = {} users_keys = ["assigned_to", "owner"] @@ -286,7 +292,10 @@ class HistoryEntry(models.Model): result[key] = value - return result + self.values_diff_cache = result + # Update values_diff_cache without dispatching signals + HistoryEntry.objects.filter(pk=self.pk).update(values_diff_cache=self.values_diff_cache) + return self.values_diff_cache class Meta: ordering = ["created_at"]