summaryrefslogtreecommitdiff
path: root/ishtar_common/models.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2018-08-08 12:28:23 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2018-08-13 18:26:03 +0200
commitfeb3e70384b7c3468a35300274a015592528559b (patch)
treef4a2213a5dc3593508edcd5af0ab1310b1e2e49d /ishtar_common/models.py
parent19130981f08faba30336fb57bdd78677753863c7 (diff)
downloadIshtar-feb3e70384b7c3468a35300274a015592528559b.tar.bz2
Ishtar-feb3e70384b7c3468a35300274a015592528559b.zip
Organization: cached label management
Diffstat (limited to 'ishtar_common/models.py')
-rw-r--r--ishtar_common/models.py20
1 files changed, 17 insertions, 3 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py
index 1aaa44b59..4b663aa23 100644
--- a/ishtar_common/models.py
+++ b/ishtar_common/models.py
@@ -2499,6 +2499,8 @@ class Organization(Address, Merge, OwnPerms, ValueGetter):
name = models.CharField(_(u"Name"), max_length=500)
organization_type = models.ForeignKey(OrganizationType,
verbose_name=_(u"Type"))
+ cached_label = models.TextField(_(u"Cached name"), null=True, blank=True,
+ db_index=True)
history = HistoricalRecords()
class Meta:
@@ -2519,11 +2521,20 @@ class Organization(Address, Merge, OwnPerms, ValueGetter):
self.town or "")
def __unicode__(self):
+ if self.cached_label:
+ return self.cached_label
+ self.save()
+ return self.cached_label
+
+ def _generate_cached_label(self):
if self.name:
return self.name
- return u"{} - {} - {}".format(self.organization_type,
- self.address or "",
- self.town or "")
+ attrs = ["organization_type", "address", "town"]
+ items = [unicode(getattr(self, attr))
+ for attr in attrs if getattr(self, attr)]
+ if not items:
+ items = [unicode(_(u"unknown organization"))]
+ return u" - ".join(items)
def generate_merge_key(self):
self.merge_key = slugify(self.name if self.name else '')
@@ -2542,6 +2553,9 @@ class Organization(Address, Merge, OwnPerms, ValueGetter):
return slugify(u"-".join(values))
+post_save.connect(cached_label_changed, sender=Organization)
+
+
class PersonType(GeneralType):
class Meta:
verbose_name = _(u"Person type")