|
|
|
|
import random
|
|
|
|
|
import string
|
|
|
|
|
from functools import wraps
|
|
|
|
|
|
|
|
|
|
import jwt
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
# 导入user模型
|
|
|
|
|
from django.contrib.auth import get_user_model, login, logout
|
|
|
|
|
# views.py
|
|
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
|
from django.contrib.sessions.models import Session
|
|
|
|
|
from django.core.mail import send_mail
|
|
|
|
|
# ajcx请求以json作为前后端交互
|
|
|
|
|
from django.http.response import JsonResponse
|
|
|
|
|
from django.middleware.csrf import get_token
|
|
|
|
|
from django.shortcuts import render, HttpResponse, redirect, reverse
|
|
|
|
|
# 注册界面
|
|
|
|
|
from django.views.decorators.http import require_http_methods
|
|
|
|
|
from rest_framework import status
|
|
|
|
|
from rest_framework.authtoken.models import Token
|
|
|
|
|
# User = get_user_model()
|
|
|
|
|
from rest_framework.decorators import api_view, permission_classes
|
|
|
|
|
from rest_framework.permissions import IsAuthenticated
|
|
|
|
|
|
|
|
|
|
from .forms import RegisterForm, LoginForm, FundForm
|
|
|
|
|
from .models import CaptchaModel, Fund
|
|
|
|
|
from .serializers import FundSerializer
|
|
|
|
|
from .utils import generate_jwt_token
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# @require_http_methods(['GET'])
|
|
|
|
|
# def get_token(request):
|
|
|
|
|
# token = request.session.get('token')
|
|
|
|
|
# print('开始获取token')
|
|
|
|
|
# print(f'token{token}')
|
|
|
|
|
# print(f"会话数据2: {request.session.items()}")
|
|
|
|
|
# if token:
|
|
|
|
|
# return JsonResponse({'token': token})
|
|
|
|
|
# else:
|
|
|
|
|
# return JsonResponse({'error': 'No token found'}, status=400)
|
|
|
|
|
|
|
|
|
|
def get_csrf_token_view(request):
|
|
|
|
|
return JsonResponse({'csrfToken': get_token(request)})
|
|
|
|
|
@api_view(['POST'])
|
|
|
|
|
@permission_classes([IsAuthenticated])
|
|
|
|
|
def add_fund(request):
|
|
|
|
|
print('开始调用')
|
|
|
|
|
serializer = FundSerializer(data=request.data)
|
|
|
|
|
if serializer.is_valid():
|
|
|
|
|
fund = serializer.save(user=request.user)
|
|
|
|
|
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
|
|
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
|
def delete_fund(request, fund_id):
|
|
|
|
|
fund = Fund.objects.get(id=fund_id, user=request.user) # 确保只能删除属于用户的基金
|
|
|
|
|
if fund:
|
|
|
|
|
fund.delete()
|
|
|
|
|
return redirect('funds_list')
|
|
|
|
|
return render(request, 'error.html', {'message': '无权进行此操作'})
|
|
|
|
|
|
|
|
|
|
def validate_session(request):
|
|
|
|
|
sessionid = request.GET.get('sessionid')
|
|
|
|
|
if not sessionid:
|
|
|
|
|
return JsonResponse({'error': 'No sessionid provided'}, status=400)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
session = Session.objects.get(session_key=sessionid)
|
|
|
|
|
user_id = session.get_decoded().get('_auth_user_id')
|
|
|
|
|
user = User.objects.get(pk=user_id)
|
|
|
|
|
return JsonResponse({'username': user.username})
|
|
|
|
|
except Session.DoesNotExist:
|
|
|
|
|
return JsonResponse({'error': 'Invalid sessionid'}, status=400)
|
|
|
|
|
except User.DoesNotExist:
|
|
|
|
|
return JsonResponse({'error': 'User not found'}, status=400)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# @login_required
|
|
|
|
|
# def get_current_user(request):
|
|
|
|
|
# # 从请求头中获取令牌
|
|
|
|
|
# token = request.headers.get('Authorization')
|
|
|
|
|
#
|
|
|
|
|
# if not token:
|
|
|
|
|
# return JsonResponse({'error': 'Authorization header is missing'}, status=401)
|
|
|
|
|
#
|
|
|
|
|
# try:
|
|
|
|
|
# # 验证令牌
|
|
|
|
|
# decoded_token = jwt.decode(token, settings.SECRET_KEY, algorithms=['HS256'])
|
|
|
|
|
# user_id = decoded_token.get('user_id')
|
|
|
|
|
# if user_id:
|
|
|
|
|
# # 如果令牌有效,返回用户数据
|
|
|
|
|
# user = User.objects.get(pk=user_id)
|
|
|
|
|
# return JsonResponse({
|
|
|
|
|
# 'user_id': user.id,
|
|
|
|
|
# 'username': user.username,
|
|
|
|
|
# 'email': user.email
|
|
|
|
|
# })
|
|
|
|
|
# else:
|
|
|
|
|
# return JsonResponse({'error': 'Invalid token'}, status=401)
|
|
|
|
|
# except jwt.ExpiredSignatureError:
|
|
|
|
|
# return JsonResponse({'error': 'Token has expired'}, status=401)
|
|
|
|
|
# except jwt.InvalidTokenError:
|
|
|
|
|
# return JsonResponse({'error': 'Invalid token'}, status=401)
|
|
|
|
|
# Create your views here.
|
|
|
|
|
@login_required
|
|
|
|
|
def get_current_user(request):
|
|
|
|
|
sessionid = request.COOKIES.get('sessionid')
|
|
|
|
|
print(f'sessionid2:{sessionid}')
|
|
|
|
|
if not sessionid:
|
|
|
|
|
return JsonResponse({'error': 'No sessionid provided'}, status=400)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
session = Session.objects.get(session_key=sessionid)
|
|
|
|
|
user_id = session.get_decoded().get('_auth_user_id')
|
|
|
|
|
user = User.objects.get(pk=user_id)
|
|
|
|
|
return JsonResponse({'user_id': user.id, 'username': user.username})
|
|
|
|
|
except Session.DoesNotExist:
|
|
|
|
|
return JsonResponse({'error': 'Invalid sessionid'}, status=400)
|
|
|
|
|
except User.DoesNotExist:
|
|
|
|
|
return JsonResponse({'error': 'User not found'}, status=400)
|
|
|
|
|
|
|
|
|
|
def index(request):
|
|
|
|
|
return render(request, '初始页面.html')
|
|
|
|
|
@login_required
|
|
|
|
|
def streamlit_test(request):
|
|
|
|
|
sessionid = request.GET.get('sessionid')
|
|
|
|
|
print(sessionid)
|
|
|
|
|
return render(request, 'stream_test.html', {'sessionid': sessionid})
|
|
|
|
|
# return render(request, 'stream_test.html')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# def login(request):
|
|
|
|
|
# print('开始登录')
|
|
|
|
|
# return render(request, '登录界面.html')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@require_http_methods(['GET', 'POST'])
|
|
|
|
|
def lg_login(request):
|
|
|
|
|
print('开始登录')
|
|
|
|
|
if request.method == 'GET':
|
|
|
|
|
return render(request, '登录界面.html')
|
|
|
|
|
else:
|
|
|
|
|
form = LoginForm(request.POST)
|
|
|
|
|
if form.is_valid():
|
|
|
|
|
email = form.cleaned_data.get('email')
|
|
|
|
|
password = form.cleaned_data.get('password')
|
|
|
|
|
remember = form.cleaned_data.get('remember')
|
|
|
|
|
user = User.objects.filter(email=email).first()
|
|
|
|
|
if user and user.check_password(password):
|
|
|
|
|
# 登录
|
|
|
|
|
login(request, user)
|
|
|
|
|
# 判断是否需要记住我
|
|
|
|
|
if not remember:
|
|
|
|
|
print('不记住')
|
|
|
|
|
# 如果没有点击记住我,那么就要设置过期时间为0,即浏览器关闭后就会过期
|
|
|
|
|
request.session.set_expiry(0)
|
|
|
|
|
else:
|
|
|
|
|
print('记住两周')
|
|
|
|
|
request.session.set_expiry(1209600) # 设置会话两周(1209600秒)过期
|
|
|
|
|
# 如果点击了,那么就什么都不做,使用默认的2周的过期时间
|
|
|
|
|
# return render(request, 'stream_test.html', {'form': form})
|
|
|
|
|
# return redirect('/streamlit')
|
|
|
|
|
print(f'session:{request.session.session_key}')
|
|
|
|
|
return redirect(f'/streamlit/?sessionid={request.session.session_key}')
|
|
|
|
|
else:
|
|
|
|
|
print('邮箱或密码错误!')
|
|
|
|
|
# form.add_error('email', '邮箱或者密码错误!')
|
|
|
|
|
# return render(request, 'login.html', context={"form": form})
|
|
|
|
|
return render(request, '登录界面.html', {'form': form})
|
|
|
|
|
else:
|
|
|
|
|
# 表单无效,返回表单错误
|
|
|
|
|
return render(request, '登录界面.html', {'form': form})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# @require_http_methods(['GET', 'POST'])
|
|
|
|
|
# def lg_login(request):
|
|
|
|
|
# if request.method == 'GET':
|
|
|
|
|
# return render(request, '登录界面.html', {'form': LoginForm()})
|
|
|
|
|
# else:
|
|
|
|
|
# form = LoginForm(request.POST)
|
|
|
|
|
# if form.is_valid():
|
|
|
|
|
# email = form.cleaned_data.get('email')
|
|
|
|
|
# password = form.cleaned_data.get('password')
|
|
|
|
|
# remember = form.cleaned_data.get('remember')
|
|
|
|
|
# user = User.objects.filter(email=email).first()
|
|
|
|
|
# if user and user.check_password(password):
|
|
|
|
|
# # 登录
|
|
|
|
|
# login(request, user)
|
|
|
|
|
# # 生成Token
|
|
|
|
|
# token, created = Token.objects.get_or_create(user=user)
|
|
|
|
|
# # 生成Token
|
|
|
|
|
# # 存储token到会话中
|
|
|
|
|
# print(f'token:{token.key}')
|
|
|
|
|
# request.session['token'] = token.key
|
|
|
|
|
# print(f"存储的token: {request.session['token']}")
|
|
|
|
|
# print(f"会话数据: {request.session.items()}")
|
|
|
|
|
# # 判断是否需要记住我
|
|
|
|
|
# if not remember:
|
|
|
|
|
# request.session.set_expiry(0) # 浏览器关闭后会话过期
|
|
|
|
|
# else:
|
|
|
|
|
# request.session.set_expiry(1209600) # 两周(1209600秒)过期
|
|
|
|
|
# # 返回Token到前端
|
|
|
|
|
# # return JsonResponse({'token': token.key})
|
|
|
|
|
# return redirect(f'/streamlit/?token={token.key}')
|
|
|
|
|
# else:
|
|
|
|
|
#
|
|
|
|
|
# return render(request, '登录界面.html', {'form': form, 'error': '邮箱或者密码错误!'})
|
|
|
|
|
# else:
|
|
|
|
|
# return render(request, '登录界面.html', {'form': form, 'error': '表单无效!'})
|
|
|
|
|
# @require_http_methods(['GET', 'POST'])
|
|
|
|
|
# def lg_login(request):
|
|
|
|
|
# print('开始登录')
|
|
|
|
|
# if request.method == 'GET':
|
|
|
|
|
# csrf_token = get_token(request)
|
|
|
|
|
# response = JsonResponse({'csrfToken': csrf_token})
|
|
|
|
|
# response.set_cookie('csrftoken', csrf_token)
|
|
|
|
|
# return response
|
|
|
|
|
# else:
|
|
|
|
|
# form = LoginForm(request.POST)
|
|
|
|
|
# if form.is_valid():
|
|
|
|
|
# print('表单有效')
|
|
|
|
|
# email = form.cleaned_data.get('email')
|
|
|
|
|
# password = form.cleaned_data.get('password')
|
|
|
|
|
# remember = form.cleaned_data.get('remember')
|
|
|
|
|
# user = User.objects.filter(email=email).first()
|
|
|
|
|
# if user and user.check_password(password):
|
|
|
|
|
# login(request, user)
|
|
|
|
|
# if not remember:
|
|
|
|
|
# request.session.set_expiry(0) # 浏览器关闭后会话过期
|
|
|
|
|
# else:
|
|
|
|
|
# request.session.set_expiry(1209600) # 两周(1209600秒)过期
|
|
|
|
|
# return JsonResponse({'sessionid': request.session.session_key, 'csrfToken': csrf_token})
|
|
|
|
|
# return render(request, '登录界面.html', {'form': form, 'error': '邮箱或者密码错误!'})
|
|
|
|
|
# else:
|
|
|
|
|
# return render(request, '登录界面.html', {'form': form, 'error': '表单无效!'})
|
|
|
|
|
def lg_logout(request):
|
|
|
|
|
logout(request)
|
|
|
|
|
return render(request, '登录界面.html')
|
|
|
|
|
|
|
|
|
|
# 装饰器
|
|
|
|
|
@require_http_methods(['GET', 'POST'])
|
|
|
|
|
def register(request):
|
|
|
|
|
print('开始注册')
|
|
|
|
|
if request.method == 'GET':
|
|
|
|
|
return render(request, '注册界面.html')
|
|
|
|
|
else:
|
|
|
|
|
form = RegisterForm(request.POST)
|
|
|
|
|
if form.is_valid():
|
|
|
|
|
print('注册成功')
|
|
|
|
|
email = form.cleaned_data.get('email')
|
|
|
|
|
username = form.cleaned_data.get('username')
|
|
|
|
|
password = form.cleaned_data.get('password')
|
|
|
|
|
User.objects.create_user(email=email, username=username, password=password)
|
|
|
|
|
return render(request, '登录界面.html', {'form': form})
|
|
|
|
|
else:
|
|
|
|
|
print('注册失败')
|
|
|
|
|
print(form.errors)
|
|
|
|
|
return render(request, '注册界面.html', {'form': form})
|
|
|
|
|
|
|
|
|
|
def sender_email_captcha(request):
|
|
|
|
|
email = request.GET.get('email')
|
|
|
|
|
if not email:
|
|
|
|
|
return JsonResponse({"code": 400, "message": "必须传递邮箱"})
|
|
|
|
|
captcha = "".join(random.sample(string.digits, k=4))
|
|
|
|
|
CaptchaModel.objects.update_or_create(email=email, defaults={'captcha': captcha})
|
|
|
|
|
send_mail("流感基金关联分析系统", message=f"您的注册验证码是{captcha}", recipient_list=[email], from_email=None)
|
|
|
|
|
return JsonResponse({"code": 200, "message": "邮箱验证码发送成功"})
|
|
|
|
|
# 生成验证码
|
|
|
|
|
# ee2
|
|
|
|
|
|
|
|
|
|
#更新数据
|
|
|
|
|
def update(request):
|
|
|
|
|
import os
|
|
|
|
|
print("Current working directory:", os.getcwd())
|
|
|
|
|
import sys
|
|
|
|
|
# print("\n".join(sys.path))
|
|
|
|
|
sys.path.append('D:\\python\\pythonProject\\流感预测集成系统(django)\\liugan_yuce')
|
|
|
|
|
# for item in os.listdir('.'):
|
|
|
|
|
# print(item)
|
|
|
|
|
from .liugan_zhoubao_spider import get_liuganzhoubao
|
|
|
|
|
from .beijing_zhoubao_spider import get_beijing_zhoubao
|
|
|
|
|
from .get_baidu_index import get_baidu_index
|
|
|
|
|
from .tiantian_jijin_spider import get_tiantian_jijin_data,get_fund_data_by_code
|
|
|
|
|
from .stock_data_spider import get_stock_data
|
|
|
|
|
get_liuganzhoubao()
|
|
|
|
|
get_beijing_zhoubao()
|
|
|
|
|
get_baidu_index()
|
|
|
|
|
get_tiantian_jijin_data()
|
|
|
|
|
get_stock_data()
|
|
|
|
|
# get_fund_data_by_code('000001')
|
|
|
|
|
return HttpResponse('数据更新成功')
|