-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
78 lines (62 loc) · 1.99 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import json
from django.db import models
from django.db.utils import ProgrammingError
from django.conf import settings
from django.core.cache import cache as django_cache
from apps.utils.base_model import BaseModel
class AppSetting(BaseModel):
CACHE_KEY = 'app-settings'
class Meta:
verbose_name_plural = 'App Settings'
ordering = ('key',)
key = models.CharField(max_length=50, unique=True)
value = models.CharField(max_length=1000, blank=True)
enabled = models.BooleanField(default=True)
@classmethod
def delete_cache(cls):
django_cache.delete(cls.CACHE_KEY)
@classmethod
def get(cls, key, *args):
"""
args[0] is optional default return value
"""
data = django_cache.get(cls.CACHE_KEY)
if not data:
"""
before initial migration, AppSetting table will not exist (i.e. during docker build).
just ignore.
"""
try:
data = {s.key: s.value for s in cls.objects.filter(enabled=True)}
django_cache.set(cls.CACHE_KEY, data)
except ProgrammingError:
return None
if key in data:
value = data[key]
return cls._typeify(value)
else:
if hasattr(settings, key):
return getattr(settings, key)
elif len(args):
return args[0]
else:
raise AppSetting.DoesNotExist
@classmethod
def _typeify(cls, string_value):
try:
int(string_value)
return int(string_value)
except ValueError:
pass
if string_value.lower() == 'true':
return True
if string_value.lower() == 'false':
return False
if string_value.lower() == 'none':
return None
try:
struct = json.loads(string_value)
return struct
except ValueError:
pass
return string_value