优化配置文件读取,支持文件数据库配置模板变量使用

dependabot/npm_and_yarn/fir_admin/url-parse-1.5.10
nineven 3 years ago
parent 875a3a4305
commit 518fc3101e
  1. 2
      fir_client/src/components/user/FirSuperSignBase.vue
  2. 49
      fir_ser/api/utils/sysconfig.py
  3. 4
      fir_ser/config.py

@ -133,7 +133,7 @@
active-color="#13ce66"
inactive-color="#ff4949">
</el-switch>
<el-tag style="margin-left: 10px" type="warning"> 开启之后每天凌晨自动检测该开发者状态和设备使用状态默认关闭</el-tag>
<el-tag style="margin-left: 10px" type="warning"> 开启之后每天凌晨自动检测该开发者和设备使用状态默认关闭</el-tag>
</el-form-item>
<el-form-item label="证书id" label-width="110px">
<el-input v-model="editdeveloperinfo.certid" :disabled='isedit'/>

@ -6,8 +6,10 @@
import json
import logging
import re
from django.template import Context, Template
from django.template.base import VariableNode
from rest_framework import serializers
from api.models import SystemConfig
@ -26,6 +28,11 @@ class SystemConfigSerializer(serializers.ModelSerializer):
def get_render_context(tmp: str, context: dict) -> str:
template = Template(tmp)
for node in template.nodelist:
if isinstance(node, VariableNode):
v_key = re.findall(r'<Variable Node: (.*)>', str(node))
if v_key and v_key[0].isupper():
context[v_key[0]] = getattr(Config, v_key[0])
context = Context(context)
return template.render(context)
@ -34,30 +41,33 @@ def invalid_config_cache(key='*'):
SystemConfigCache(key).del_many()
def get_value_from_db(key):
def get_render_value(value):
if value:
try:
context_dict = {}
for sys_obj_dict in SystemConfig.objects.values().all():
if re.findall('{{.*%s.*}}' % sys_obj_dict['key'], sys_obj_dict['value']):
logger.warning(f"get same render key. so continue")
continue
context_dict[sys_obj_dict['key']] = sys_obj_dict['value']
data = SystemConfigSerializer(SystemConfig.objects.filter(key=key).first()).data
value = data.get('value', '')
if value:
try:
r_text = get_render_context(value, context_dict)
data['value'] = r_text
try:
data['value'] = json.loads(r_text)
except Exception as e:
logger.warning(f"db config - json loads failed {e}")
value = get_render_context(value, context_dict)
except Exception as e:
logger.warning(f"db config - render failed {e}")
return data
return value
class ConfigCacheBase(object):
def __init__(self, px=''):
self.px = px
def get_value_from_db(self, key):
data = SystemConfigSerializer(SystemConfig.objects.filter(key=key).first()).data
if re.findall('{{.*%s.*}}' % data['key'], data['value']):
logger.warning(f"get same render key. so get default value")
data['key'] = ''
return data
def get_value(self, key, data=None):
if data is None:
data = {}
@ -65,11 +75,16 @@ class ConfigCacheBase(object):
cache_data = cache.get_storage_cache()
if cache_data is not None and cache_data.get('key', '') == key:
return cache_data.get('value')
db_data = get_value_from_db(key)
db_data = self.get_value_from_db(key)
d_key = db_data.get('key', '')
if d_key != key and data is not None:
db_data['value'] = data
db_data['value'] = json.dumps(data)
db_data['key'] = key
db_data['value'] = get_render_value(db_data['value'])
try:
db_data['value'] = json.loads(db_data['value'])
except Exception as e:
logger.warning(f"db config - json loads failed {e}")
cache.set_storage_cache(db_data, timeout=60 * 60 * 24 * 30)
return db_data.get('value')
@ -106,6 +121,10 @@ class ConfigCacheBase(object):
def FILE_UPLOAD_DOMAIN(self):
return self.get_value('FILE_UPLOAD_DOMAIN', self.API_DOMAIN)
@property
def CAN_RENDER_KEY(self):
return self.get_value('CAN_RENDER_KEY', [])
class BaseConfCache(ConfigCacheBase):
def __init__(self):

@ -258,13 +258,13 @@ class IPACONF(object):
# 默认描述文件路径或者下载路径,用户企业签名或者超级签名 跳转 [设置 - 通用 - 描述文件|设备管理] 页面
# 如果配置了path路径,则走路径,如果配置了url,则走URL,path 优先级大于url优先级
'enterprise': {
'url': MOBILEPROVISION,
'url': '{{MOBILEPROVISION}}',
# 'path': os.path.join(BASE_DIR,'files', 'embedded.mobileprovision'),
},
'supersign': {
# 超级签名,如果self 为True,则默认用自己的描述文件,否则同企业配置顺序一致,自己的配置文件有时候有问题
'self': False,
'url': MOBILEPROVISION,
'url': '{{MOBILEPROVISION}}',
# 'path': os.path.join(BASE_DIR,'files', 'embedded.mobileprovision'),
}
}

Loading…
Cancel
Save