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

2. 增加用户选择数据的处理逻辑
main
RogerWork 3 months ago
parent ee839e8f61
commit 3accec70cf
  1. 3
      dspt_api/util/api/play_info.py
  2. 17
      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 dspt_api.util.general.handle_redis import get_data_from_redis
from django_redis import get_redis_connection
import random import random
@ -9,7 +10,7 @@ class ApiPlayInfo:
self.ip = kwargs.get('ip') self.ip = kwargs.get('ip')
def get_suggestion(self): def get_suggestion(self):
# hall_id # play_id
request_api = {'name': '3.1.5 获取放映计划列表', 'path': 'cinema/plays'} request_api = {'name': '3.1.5 获取放映计划列表', 'path': 'cinema/plays'}
redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}' 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) result, _format, redis_data = get_data_from_redis(redis_key_api)

@ -7,22 +7,19 @@ import xmltodict
def get_data_from_redis(redis_key): def get_data_from_redis(redis_key):
# 初始化redis # 初始化redis
redis_conn = get_redis_connection() 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): if redis_conn.exists(redis_key):
redis_data = json.loads(redis_conn.get(redis_key)) redis_data = json.loads(redis_conn.get(redis_key))
resp_data = [] resp_data = []
resp_result = 0 resp = redis_data['handled_data']
if redis_data['format'] == 'json': print('handled_data', json.dumps(redis_data['handled_data']))
resp = json.loads(redis_data['response_data'])
print('json-response', json.dumps(resp))
resp_result = resp['res']['status'] resp_result = resp['res']['status']
if str(resp_result) == '1': if str(resp_result) == '1':
resp_data = resp['res']['data'] 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'])
if str(resp_result) != '1': if str(resp_result) != '1':
return False, redis_data['format'], '请检查接口返回值或手动输入参数:' return False, redis_data['format'], '请检查接口返回值或手动输入参数:'
if len(resp_data) == 0: 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.suggest_params import suggest_params
from dspt_api.util.general.handle_xml_resp import HandleXmlResp from dspt_api.util.general.handle_xml_resp import HandleXmlResp
from dspt_api.util.general.format_xml import format_xml from dspt_api.util.general.format_xml import format_xml
import chardet
# Create your views here. # Create your views here.
class EcChannelViewSet(viewsets.ModelViewSet): class EcChannelViewSet(viewsets.ModelViewSet):
@ -184,7 +183,6 @@ def send_request(request):
web_req = json.loads(request.body) web_req = json.loads(request.body)
api = web_req.get('api') api = web_req.get('api')
redis_key_api = f'dspt_api_{user_ip}_{member_type}_{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) req, sig = handle_request(request)
@ -218,7 +216,6 @@ def send_request(request):
} }
print(data) print(data)
redis_conn.set(redis_key_api, json.dumps(data), 10 * 60 * 60) redis_conn.set(redis_key_api, json.dumps(data), 10 * 60 * 60)
# redis_conn.delete(redis_key_user_data)
# 插入数据 # 插入数据
db_data = { db_data = {
'ip': user_ip, 'ip': user_ip,
@ -247,19 +244,21 @@ def set_user_select_data(request):
req = json.loads(request.body) req = json.loads(request.body)
api = req.get('api') api = req.get('api')
member_type = req.get('member_type') 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_data = req.get('user_data')
user_ip = request.META.get('REMOTE_ADDR') user_ip = request.META.get('REMOTE_ADDR')
# 初始化redis # 初始化redis
redis_conn = get_redis_connection() redis_conn = get_redis_connection()
redis_key_user_data = f'dspt_api_{user_ip}_{member_type}_{api}_user_data' redis_key_user_data = f'dspt_api_{user_ip}_{member_type}_{api}_user_data'
print('user_data', user_data)
# 记录用户选择 # 记录用户选择
data = { data = {
'api': api, 'api': api,
'member_type': member_type, 'member_type': member_type,
'format': resp_format, '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): if redis_conn.exists(redis_key_user_data):
redis_conn.delete(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): if redis_conn.set(redis_key_user_data, json.dumps(data), 10 * 60 * 60):

Loading…
Cancel
Save