diff options
Diffstat (limited to 'ishtar_common/models.py')
-rw-r--r-- | ishtar_common/models.py | 52 |
1 files changed, 34 insertions, 18 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 460612e17..6c4ec2e49 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -28,6 +28,7 @@ import tempfile import copy from django.conf import settings +from django.core.cache import cache from django.core.exceptions import ObjectDoesNotExist, ValidationError from django.core.files.uploadedfile import SimpleUploadedFile from django.core.validators import validate_slug @@ -45,7 +46,8 @@ from django.contrib import admin from simple_history.models import HistoricalRecords as BaseHistoricalRecords -from ooo_replace import ooo_replace +from ishtar_common.ooo_replace import ooo_replace +from ishtar_common.utils import get_cache def post_save_user(sender, **kwargs): user = kwargs['instance'] @@ -85,6 +87,8 @@ class ValueGetter(object): values['VALUES'] = u'\n'.join( [u"%s: %s" % (k, v) for k, v in sorted(value_list, key=lambda x:x[0])]) + for global_var in GlobalVar.objects.all(): + values[global_var.slug] = global_var.value or "" return values @classmethod @@ -525,7 +529,6 @@ class BaseHistorizedItem(models.Model): items.append('00000000') return u"-".join([unicode(item) for item in items]) - class ShortMenuItem(object): def get_short_menu_class(self): return '' @@ -539,26 +542,39 @@ class LightHistorizedItem(BaseHistorizedItem): super(LightHistorizedItem, self).save(*args, **kwargs) return True -class Wizard(models.Model): - url_name = models.CharField(_(u"URL name"), max_length=128, unique=True) +class GlobalVar(models.Model): + slug = models.SlugField(_(u"Variable name"), unique=True) + description = models.TextField(_(u"Description of the variable"), + null=True, blank=True) + value = models.TextField(_(u"Value"), null=True, blank=True) class Meta: - verbose_name = _(u"Wizard") - ordering = ['url_name'] + verbose_name = _(u"Global variable") + verbose_name_plural = _(u"Global variables") + ordering = ['slug'] def __unicode__(self): - return unicode(self.url_name) - -class WizardStep(models.Model): - order = models.IntegerField(_(u"Order")) - wizard = models.ForeignKey(Wizard, verbose_name=_(u"Wizard")) - url_name = models.CharField(_(u"URL name"), max_length=128) - name = models.CharField(_(u"Label"), max_length=128) - class Meta: - verbose_name = _(u"Wizard step") - ordering = ['wizard', 'order'] + return unicode(self.slug) - def __unicode__(self): - return u"%s » %s" % (unicode(self.wizard), unicode(self.name)) + @classmethod + def get_cache(cls, slug): + cache_key, value = get_cache(cls, slug) + if value: + return value + try: + obj = cls.objects.get(slug=slug) + cache.set(cache_key, obj.value, settings.CACHE_TIMEOUT) + return obj.value + except cls.DoesNotExist: + return None + +def cached_globalvar_changed(sender, **kwargs): + if not kwargs['instance']: + return + var = kwargs['instance'] + cache_key, value = get_cache(GlobalVar, var.slug) + cache.set(cache_key, var.value, settings.CACHE_TIMEOUT) + +post_save.connect(cached_globalvar_changed, sender=GlobalVar) class UserDashboard: def __init__(self): |