summaryrefslogtreecommitdiff
path: root/archaeological_files/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'archaeological_files/models.py')
-rw-r--r--archaeological_files/models.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/archaeological_files/models.py b/archaeological_files/models.py
index 8f2f513d2..ae6ad726e 100644
--- a/archaeological_files/models.py
+++ b/archaeological_files/models.py
@@ -22,8 +22,11 @@ import datetime
from django.conf import settings
from django.contrib.gis.db import models
from django.db.models import Q, Count, Sum
+from django.db.models.signals import post_save, m2m_changed
from django.utils.translation import ugettext_lazy as _, ugettext
+from ishtar_common.utils import cached_label_changed
+
from ishtar_common.models import GeneralType, BaseHistorizedItem, \
HistoricalRecords, OwnPerms, Person, Organization, Department, Town, \
Dashboard, IshtarUser
@@ -100,6 +103,8 @@ class File(BaseHistorizedItem, OwnPerms):
postal_code = models.CharField(_(u"Main address - postal code"),
max_length=10, null=True, blank=True)
comment = models.TextField(_(u"Comment"), null=True, blank=True)
+ cached_label = models.CharField(_(u"Cached name"), max_length=100,
+ null=True, blank=True)
history = HistoricalRecords()
class Meta:
@@ -128,6 +133,12 @@ class File(BaseHistorizedItem, OwnPerms):
return cls.objects.count()
def __unicode__(self):
+ if self.cached_label:
+ return self.cached_label
+ self.save()
+ return self.cached_label
+
+ def _generate_cached_label(self):
items = [unicode(_('Intercommunal'))]
if self.towns.count() == 1:
items[0] = unicode(self.towns.all()[0])
@@ -175,6 +186,9 @@ class File(BaseHistorizedItem, OwnPerms):
def is_preventive(self):
return FileType.is_preventive(self.file_type.pk)
+m2m_changed.connect(cached_label_changed, sender=File.towns.through)
+post_save.connect(cached_label_changed, sender=File)
+
class FileByDepartment(models.Model):
'''
Database view for dashboard