|
|
import os
|
|
|
|
|
|
import django
|
|
|
import requests
|
|
|
|
|
|
os.chdir('D:/python/djangoProject/test_Bootstrap')
|
|
|
# 设置DJANGO_SETTINGS_MODULE环境变量
|
|
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'test_Bootstrap.settings')
|
|
|
# 初始化Django设置
|
|
|
django.setup()
|
|
|
# 初始化Django设置
|
|
|
django.setup()
|
|
|
import streamlit as st
|
|
|
from st_pages import add_page_title, add_indentation
|
|
|
from app_test.other_pages.test_research import search_fund_by_id
|
|
|
from app_test.models import Fund
|
|
|
from django.contrib.auth.models import User
|
|
|
from requests.cookies import RequestsCookieJar
|
|
|
# 获取 URL 参数并初始化 session state
|
|
|
# 在应用的最顶部获取 URL 参数并初始化 session state
|
|
|
# 从会话中获取token并存储到session state中
|
|
|
# 爬虫爬取token尝试
|
|
|
add_page_title()
|
|
|
add_indentation()
|
|
|
# 初始化 session state 中的键
|
|
|
if 'fund_list' not in st.session_state:
|
|
|
st.session_state['fund_list'] = ["国投瑞银医疗保健", "华宝医药生物混合", "中银医疗保健混合"]
|
|
|
if 'message' not in st.session_state:
|
|
|
st.session_state['message'] = ''
|
|
|
if 'temp_fund_input' not in st.session_state:
|
|
|
st.session_state['temp_fund_input'] = ''
|
|
|
if 'fund_input' not in st.session_state:
|
|
|
st.session_state['fund_input'] = ''
|
|
|
if 'trigger_rerun' not in st.session_state:
|
|
|
st.session_state['trigger_rerun'] = False
|
|
|
# 基金列表显示
|
|
|
# 使用 HTML 和内联CSS来增加字体大小
|
|
|
st.markdown("""
|
|
|
<div style='font-size: 34px; font-weight: bold;'>
|
|
|
我的基金
|
|
|
</div>
|
|
|
""", unsafe_allow_html=True)
|
|
|
st.markdown("#####")
|
|
|
for fund in st.session_state['fund_list']:
|
|
|
col1, col2 = st.columns([4, 1])
|
|
|
col1.write(fund)
|
|
|
if col2.button("×", key=f"remove_{fund}"):
|
|
|
st.session_state['fund_list'].remove(fund)
|
|
|
st.experimental_rerun()
|
|
|
# 添加自定义 CSS 样式
|
|
|
st.markdown("""
|
|
|
<style>
|
|
|
.placeholder {
|
|
|
color: rgba(0, 0, 0, 0.5); /* 半透明颜色 */
|
|
|
}
|
|
|
</style>
|
|
|
""", unsafe_allow_html=True)
|
|
|
# 基金选择列表
|
|
|
fund_all = ['cat', 'dog', 'rabbit', 'bird']
|
|
|
|
|
|
django.setup()
|
|
|
|
|
|
def validate_fund():
|
|
|
print('开始验证基金')
|
|
|
fund_code = st.session_state.get('fund_input', '')
|
|
|
print(f'fund_code: {fund_code}')
|
|
|
fund_list = ["国投瑞银医疗保健", "华宝医药生物混合", "中银医疗保健混合"]
|
|
|
fund_name = search_fund_by_id(fund_code) # 假设有这个函数来搜索基金代码
|
|
|
if fund_name:
|
|
|
if any(f['fund_code'] == fund_code for f in fund_list): # 检查基金是否已在列表中
|
|
|
st.session_state['message'] = '基金已存在'
|
|
|
else:
|
|
|
try:
|
|
|
csrf_token = get_csrf_token(token)
|
|
|
except ValueError as e:
|
|
|
st.session_state['message'] = str(e)
|
|
|
return
|
|
|
|
|
|
data = {'fund_id': fund_code, 'fund_name': fund_name}
|
|
|
headers = {
|
|
|
'Authorization': f'Token {token}', # 使用Token认证
|
|
|
'Content-Type': 'application/json',
|
|
|
'X-CSRFToken': csrf_token,
|
|
|
}
|
|
|
response = requests.post('http://localhost:8000/api/add-fund/', json=data, headers=headers)
|
|
|
if response.status_code == 201:
|
|
|
st.session_state['message'] = '添加成功'
|
|
|
else:
|
|
|
st.session_state['message'] = f'添加失败: {response.status_code} - {response.text}'
|
|
|
else:
|
|
|
st.session_state['message'] = '未发现该基金'
|
|
|
|
|
|
|
|
|
|
|
|
def get_csrf_token(token):
|
|
|
response = requests.get('http://localhost:8000/api/csrf-token/', headers={'Authorization': f'Token {token}'})
|
|
|
if response.status_code == 200:
|
|
|
return response.json()['csrfToken']
|
|
|
else:
|
|
|
raise ValueError("Could not fetch CSRF token")
|
|
|
# 验证基金函数
|
|
|
def validate_fund():
|
|
|
print('开始验证基金')
|
|
|
fund_code = st.session_state.get('fund_input', '')
|
|
|
print(f'基金代码: {fund_code}')
|
|
|
token = st.session_state.get('token')
|
|
|
print(f'token: {token}')
|
|
|
session = st.session_state.get('session')
|
|
|
|
|
|
if not token:
|
|
|
st.session_state['message'] = '请先登录'
|
|
|
return
|
|
|
|
|
|
fund_list = ["国投瑞银医疗保健", "华宝医药生物混合", "中银医疗保健混合"]
|
|
|
|
|
|
if fund_code:
|
|
|
fund_name = search_fund_by_id(fund_code) # 假设有这个函数来搜索基金代码
|
|
|
if fund_name:
|
|
|
if any(f['fund_code'] == fund_code for f in fund_list): # 检查基金是否已在列表中
|
|
|
st.session_state['message'] = '基金已存在'
|
|
|
else:
|
|
|
try:
|
|
|
csrf_token = get_csrf_token(token)
|
|
|
except ValueError as e:
|
|
|
st.session_state['message'] = str(e)
|
|
|
return
|
|
|
|
|
|
data = {'fund_id': fund_code, 'fund_name': fund_name}
|
|
|
headers = {
|
|
|
'Authorization': f'Token {token}', # 使用Token认证
|
|
|
'Content-Type': 'application/json',
|
|
|
'X-CSRFToken': csrf_token,
|
|
|
}
|
|
|
response = session.post('http://127.0.0.1:8000/api/add-fund/', json=data, headers=headers)
|
|
|
if response.status_code == 201:
|
|
|
st.session_state['message'] = '添加成功'
|
|
|
else:
|
|
|
st.session_state['message'] = f'添加失败: {response.status_code} - {response.text}'
|
|
|
else:
|
|
|
st.session_state['message'] = '未发现该基金'
|
|
|
else:
|
|
|
st.session_state['message'] = '请输入基金代码并确保已登录'
|
|
|
|
|
|
def get_csrf_token(token):
|
|
|
session = st.session_state.get('session')
|
|
|
response = session.get('http://127.0.0.1:8000/api/csrf-token/', headers={'Authorization': f'Token {token}'})
|
|
|
if response.status_code == 200:
|
|
|
return response.json()['csrfToken']
|
|
|
else:
|
|
|
raise ValueError("无法获取CSRF token")
|
|
|
def search_funds_of_user(user_id):
|
|
|
# 这里需要实现从数据库或API查询用户的基金列表
|
|
|
# 返回值应该是一个列表,每个元素都是一个包含 'fund_code' 和其他信息的字典
|
|
|
return requests.get(f'https://api.example.com/user/{user_id}/funds').json()
|
|
|
|
|
|
|
|
|
# 初始化 session state
|
|
|
# 初始化session state
|
|
|
if 'token' not in st.session_state:
|
|
|
st.session_state['token'] = None
|
|
|
if 'fund_input' not in st.session_state:
|
|
|
st.session_state['fund_input'] = ''
|
|
|
if 'message' not in st.session_state:
|
|
|
st.session_state['message'] = ''
|
|
|
if 'trigger_rerun' not in st.session_state:
|
|
|
st.session_state['trigger_rerun'] = False
|
|
|
|
|
|
# 基金代码输入框和反馈信息
|
|
|
col1, col2 = st.columns([4, 1])
|
|
|
with col1:
|
|
|
# 使用 HTML 和 CSS 来定制标题样式
|
|
|
st.markdown("""
|
|
|
<style>
|
|
|
.big-bold {
|
|
|
font-size: 24px; /* 设置字体大小 */
|
|
|
font-weight: bold; /* 字体加粗 */
|
|
|
}
|
|
|
</style>
|
|
|
<p class='big-bold'>添加基金</p>
|
|
|
""", unsafe_allow_html=True)
|
|
|
|
|
|
# 创建一个输入框,使用 placeholder 参数设置提示语
|
|
|
fund_code = st.text_input('基金代码', placeholder='请输入基金代码', key='fund_input', on_change=validate_fund,
|
|
|
label_visibility='collapsed')
|
|
|
|
|
|
# 显示反馈信息并在显示后重置
|
|
|
if st.session_state['message']:
|
|
|
st.write(st.session_state['message'])
|
|
|
st.session_state['message'] = '' # 重置消息以避免重复显示
|
|
|
|
|
|
# 页面刷新逻辑
|
|
|
if st.session_state['trigger_rerun']:
|
|
|
st.session_state['trigger_rerun'] = False
|
|
|
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|