[Backport] Adding cache to HistoryEntry values_diff

remotes/origin/issue/4217/improving-mail-design
Jesús Espino 2016-10-11 14:15:46 +02:00 committed by David Barragán Merino
parent 48914e1856
commit c219fbdc59
2 changed files with 32 additions and 2 deletions

View File

@ -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),
),
]

View File

@ -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"]