1. 优化了用户选择数据在redis中的存储格式,

2. 增加用户选择数据的处理逻辑
main
RogerWork 3 months ago
parent ee839e8f61
commit 3accec70cf
  1. 3
      dspt_api/util/api/play_info.py
  2. 23
      dspt_api/util/general/handle_redis.py
  3. 9
      dspt_api/views.py

@ -1,4 +1,5 @@
from dspt_api.util.general.handle_redis import get_data_from_redis
from django_redis import get_redis_connection
import random
@ -9,7 +10,7 @@ class ApiPlayInfo:
self.ip = kwargs.get('ip')
def get_suggestion(self):
# hall_id
# play_id
request_api = {'name': '3.1.5 获取放映计划列表', 'path': 'cinema/plays'}
redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}'
result, _format, redis_data = get_data_from_redis(redis_key_api)

@ -7,22 +7,19 @@ import xmltodict
def get_data_from_redis(redis_key):
# 初始化redis
redis_conn = get_redis_connection()
if redis_conn.exists(redis_key + '_user_data'):
redis_data = json.loads(redis_conn.get(redis_key + '_user_data'))
user_data = redis_data['user_data']
print('user_data', json.dumps(user_data))
return True, redis_data['format'], user_data
if redis_conn.exists(redis_key):
redis_data = json.loads(redis_conn.get(redis_key))
resp_data = []
resp_result = 0
if redis_data['format'] == 'json':
resp = json.loads(redis_data['response_data'])
print('json-response', json.dumps(resp))
resp_result = resp['res']['status']
if str(resp_result) == '1':
resp_data = resp['res']['data']
if redis_data['format'] == 'xml':
resp = xmltodict.parse(redis_data['response_data'])
print('xml-response', json.dumps(resp))
resp_result = resp['root']['status']
if str(resp_result) == '1':
resp_data = resp['root']['data'].get('item', resp['root']['data'])
resp = redis_data['handled_data']
print('handled_data', json.dumps(redis_data['handled_data']))
resp_result = resp['res']['status']
if str(resp_result) == '1':
resp_data = resp['res']['data']
if str(resp_result) != '1':
return False, redis_data['format'], '请检查接口返回值或手动输入参数:'
if len(resp_data) == 0:

@ -21,7 +21,6 @@ from dspt_api.serializers import EcChannelSerializer, EcEnvSerializer, EcApiSeri
from dspt_api.util.suggest_params import suggest_params
from dspt_api.util.general.handle_xml_resp import HandleXmlResp
from dspt_api.util.general.format_xml import format_xml
import chardet
# Create your views here.
class EcChannelViewSet(viewsets.ModelViewSet):
@ -184,7 +183,6 @@ def send_request(request):
web_req = json.loads(request.body)
api = web_req.get('api')
redis_key_api = f'dspt_api_{user_ip}_{member_type}_{api}'
# redis_key_user_data = f'dspt_api_{user_ip}_{api}_data'
# 发送请求
req, sig = handle_request(request)
@ -218,7 +216,6 @@ def send_request(request):
}
print(data)
redis_conn.set(redis_key_api, json.dumps(data), 10 * 60 * 60)
# redis_conn.delete(redis_key_user_data)
# 插入数据
db_data = {
'ip': user_ip,
@ -247,19 +244,21 @@ def set_user_select_data(request):
req = json.loads(request.body)
api = req.get('api')
member_type = req.get('member_type')
resp_format = req.get('format')
resp_format = 'xml' if req.get('format') == 'html' else req.get('format')
user_data = req.get('user_data')
user_ip = request.META.get('REMOTE_ADDR')
# 初始化redis
redis_conn = get_redis_connection()
redis_key_user_data = f'dspt_api_{user_ip}_{member_type}_{api}_user_data'
print('user_data', user_data)
# 记录用户选择
data = {
'api': api,
'member_type': member_type,
'format': resp_format,
'user_data': user_data,
'user_data': [json.loads(user_data)],
}
print('set_user_select_data', data)
if redis_conn.exists(redis_key_user_data):
redis_conn.delete(redis_key_user_data)
if redis_conn.set(redis_key_user_data, json.dumps(data), 10 * 60 * 60):

Loading…
Cancel
Save