2018-01-11 12:10:27 +00:00
|
|
|
import json
|
|
|
|
|
2016-09-03 11:05:50 +00:00
|
|
|
from django.db import models
|
2018-01-22 03:38:40 +00:00
|
|
|
from django.db.utils import ProgrammingError, OperationalError
|
2018-01-11 12:10:27 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2019-12-05 07:09:25 +00:00
|
|
|
from django.core.cache import cache
|
2018-01-11 12:10:27 +00:00
|
|
|
|
2019-12-18 07:37:53 +00:00
|
|
|
from common.utils import signer
|
2018-09-03 03:24:25 +00:00
|
|
|
|
2018-01-11 12:10:27 +00:00
|
|
|
|
|
|
|
class SettingQuerySet(models.QuerySet):
|
|
|
|
def __getattr__(self, item):
|
2020-04-29 06:32:51 +00:00
|
|
|
queryset = list(self)
|
|
|
|
instances = [i for i in queryset if i.name == item]
|
2018-01-11 12:10:27 +00:00
|
|
|
if len(instances) == 1:
|
|
|
|
return instances[0]
|
|
|
|
else:
|
|
|
|
return Setting()
|
|
|
|
|
|
|
|
|
|
|
|
class SettingManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
|
|
|
return SettingQuerySet(self.model, using=self._db)
|
|
|
|
|
|
|
|
|
|
|
|
class Setting(models.Model):
|
|
|
|
name = models.CharField(max_length=128, unique=True, verbose_name=_("Name"))
|
|
|
|
value = models.TextField(verbose_name=_("Value"))
|
2018-01-20 14:22:09 +00:00
|
|
|
category = models.CharField(max_length=128, default="default")
|
2018-09-03 03:24:25 +00:00
|
|
|
encrypted = models.BooleanField(default=False)
|
2018-01-11 12:10:27 +00:00
|
|
|
enabled = models.BooleanField(verbose_name=_("Enabled"), default=True)
|
|
|
|
comment = models.TextField(verbose_name=_("Comment"))
|
|
|
|
|
|
|
|
objects = SettingManager()
|
2019-12-05 07:09:25 +00:00
|
|
|
cache_key_prefix = '_SETTING_'
|
2018-01-11 12:10:27 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
@classmethod
|
|
|
|
def get(cls, item):
|
|
|
|
cached = cls.get_from_cache(item)
|
|
|
|
if cached is not None:
|
|
|
|
return cached
|
|
|
|
instances = cls.objects.filter(name=item)
|
|
|
|
if len(instances) == 1:
|
|
|
|
s = instances[0]
|
|
|
|
s.refresh_setting()
|
|
|
|
return s.cleaned_value
|
|
|
|
return None
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_from_cache(cls, item):
|
|
|
|
key = cls.cache_key_prefix + item
|
|
|
|
cached = cache.get(key)
|
|
|
|
return cached
|
2018-09-03 03:24:25 +00:00
|
|
|
|
2018-01-12 07:43:26 +00:00
|
|
|
@property
|
2018-01-20 14:22:09 +00:00
|
|
|
def cleaned_value(self):
|
2018-01-12 07:43:26 +00:00
|
|
|
try:
|
2018-09-03 03:24:25 +00:00
|
|
|
value = self.value
|
|
|
|
if self.encrypted:
|
|
|
|
value = signer.unsign(value)
|
2018-12-21 07:47:52 +00:00
|
|
|
if not value:
|
|
|
|
return None
|
2018-09-03 03:24:25 +00:00
|
|
|
value = json.loads(value)
|
|
|
|
return value
|
2018-01-12 07:43:26 +00:00
|
|
|
except json.JSONDecodeError:
|
|
|
|
return None
|
|
|
|
|
2018-01-20 14:22:09 +00:00
|
|
|
@cleaned_value.setter
|
|
|
|
def cleaned_value(self, item):
|
|
|
|
try:
|
|
|
|
v = json.dumps(item)
|
2018-09-03 03:24:25 +00:00
|
|
|
if self.encrypted:
|
|
|
|
v = signer.sign(v)
|
2018-01-20 14:22:09 +00:00
|
|
|
self.value = v
|
|
|
|
except json.JSONDecodeError as e:
|
|
|
|
raise ValueError("Json dump error: {}".format(str(e)))
|
|
|
|
|
2018-01-11 12:10:27 +00:00
|
|
|
@classmethod
|
|
|
|
def refresh_all_settings(cls):
|
2018-01-22 03:38:40 +00:00
|
|
|
try:
|
|
|
|
settings_list = cls.objects.all()
|
|
|
|
for setting in settings_list:
|
|
|
|
setting.refresh_setting()
|
|
|
|
except (ProgrammingError, OperationalError):
|
|
|
|
pass
|
2018-01-11 12:10:27 +00:00
|
|
|
|
|
|
|
def refresh_setting(self):
|
2019-12-05 07:09:25 +00:00
|
|
|
key = self.cache_key_prefix + self.name
|
|
|
|
cache.set(key, self.cleaned_value, None)
|
2018-01-12 07:43:26 +00:00
|
|
|
|
2018-01-11 12:10:27 +00:00
|
|
|
class Meta:
|
2019-03-04 07:38:59 +00:00
|
|
|
db_table = "settings_setting"
|
2019-02-21 11:22:23 +00:00
|
|
|
verbose_name = _("Setting")
|