Adding cache to HistoryEntry values_diff
parent
5267c65d60
commit
ce99a949c7
|
@ -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),
|
||||
),
|
||||
]
|
|
@ -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"]
|
||||
|
|
Loading…
Reference in New Issue