From 2fbcb914175895dae77a615bbad3e0465c671606 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Sat, 18 Aug 2012 06:39:40 -0400 Subject: [PATCH] Improve TranslatableLabelMixin to allow any amount of translatable fields --- apps/common/models.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/apps/common/models.py b/apps/common/models.py index 16c798d82b..bd8a82604b 100644 --- a/apps/common/models.py +++ b/apps/common/models.py @@ -63,24 +63,33 @@ class AutoAdminSingleton(Singleton): class TranslatableLabelMixin(models.Model): - _labels = {} + _translatable_registry = {} - @property - def label(self): - try: - return self.__class__._labels[self.pk] - except KeyError: - return unicode(self.__class__) + class NotConfigured(Exception): + pass + + def __getattr__(self, attr): + if attr in self.__class__.translatables: + try: + return self.__class__._translatable_registry[self.pk][attr] + except KeyError: + return u'' + else: + raise AttributeError('\'%s\' object has no attribute \'%s\'' % (self.__class__, attr)) def __setattr__(self, attr, value): - if attr == 'label': - self.__class__._labels[self.pk] = value + if not hasattr(self.__class__, 'translatables'): + raise self.__class__.NotConfigured('Must specify a list of translatable class attributes') + + if attr in self.__class__.translatables: + self.__class__._translatable_registry[self.pk][attr] = value else: return super(TranslatableLabelMixin, self).__setattr__(attr, value) - - def __unicode__(self): - return unicode(self.label) + def __init__(self, *args, **kwargs): + super(TranslatableLabelMixin, self).__init__(*args, **kwargs) + self.__class__._translatable_registry.setdefault(self.pk, {}) + class Meta: abstract = True