from django.shortcuts import render, redirect from django.http import JsonResponse, HttpResponse from django.core.mail import send_mail, EmailMultiAlternatives from django.template.loader import render_to_string from django.utils.html import strip_tags from django.views.decorators.csrf import csrf_exempt from django.views.decorators.http import require_http_methods from django.contrib import messages from django import forms from hertz_studio_django_captcha.captcha_generator import HertzCaptchaGenerator import json from django.conf import settings import random import string class HertzCaptchaForm(forms.Form): """Hertz验证码表单""" captcha_input = forms.CharField( max_length=10, widget=forms.TextInput(attrs={ 'placeholder': '请输入验证码', 'class': 'form-control', 'autocomplete': 'off' }), label='验证码' ) captcha_id = forms.CharField(widget=forms.HiddenInput(), required=False) def captcha_demo(request): """ 验证码演示页面 展示多种验证码功能的使用方法 """ # 获取请求的验证码类型 captcha_type = request.GET.get('type', 'random_char') # 初始化验证码生成器 captcha_generator = HertzCaptchaGenerator() if request.method == 'POST': # 检查是否是Ajax请求 if request.headers.get('X-Requested-With') == 'XMLHttpRequest': try: data = json.loads(request.body) action = data.get('action') if action == 'refresh': # 刷新验证码 captcha_data = captcha_generator.generate_captcha() return JsonResponse({ 'success': True, 'data': captcha_data }) elif action == 'verify': # 验证验证码 captcha_id = data.get('captcha_id', '') user_input = data.get('user_input', '') is_valid = captcha_generator.verify_captcha(captcha_id, user_input) if is_valid: return JsonResponse({ 'success': True, 'valid': True, 'message': f'验证成功!验证码类型: {captcha_type}' }) else: return JsonResponse({ 'success': True, 'valid': False, 'message': '验证码错误,请重新输入' }) except json.JSONDecodeError: return JsonResponse({ 'success': False, 'error': '请求数据格式错误' }) else: # 普通表单提交处理 form = HertzCaptchaForm(request.POST) username = request.POST.get('username', '') captcha_id = request.POST.get('captcha_id', '') captcha_input = request.POST.get('captcha_input', '') # 验证验证码 is_valid = captcha_generator.verify_captcha(captcha_id, captcha_input) if is_valid and username: # 生成新的验证码用于显示 initial_captcha = captcha_generator.generate_captcha() return render(request, 'captcha_demo.html', { 'form': HertzCaptchaForm(), 'success_message': f'验证成功!用户名: {username},验证码类型: {captcha_type}', 'captcha_unavailable': False, 'current_type': captcha_type, 'initial_captcha': initial_captcha, 'captcha_types': { 'random_char': '随机字符验证码', 'math': '数学运算验证码', 'word': '单词验证码' } }) # GET请求或表单验证失败时,生成初始验证码 form = HertzCaptchaForm() initial_captcha = captcha_generator.generate_captcha() return render(request, 'captcha_demo.html', { 'form': form, 'captcha_unavailable': False, 'current_type': captcha_type, 'initial_captcha': initial_captcha, 'captcha_types': { 'random_char': '随机字符验证码', 'math': '数学运算验证码', 'word': '单词验证码' } }) def websocket_demo(request): """WebSocket演示页面""" return render(request, 'websocket_demo.html') def websocket_test(request): """ WebSocket简单测试页面 """ return render(request, 'websocket_test.html') # 测试热重启功能 - 添加注释触发文件变化 def email_demo(request): """邮件系统演示页面""" if request.method == 'GET': return render(request, 'email_demo.html') elif request.method == 'POST': try: # 获取表单数据 email_type = request.POST.get('email_type', 'welcome') recipient_email = request.POST.get('recipient_email') recipient_name = request.POST.get('recipient_name', '用户') custom_subject = request.POST.get('subject', '') custom_message = request.POST.get('message', '') if not recipient_email: return JsonResponse({ 'success': False, 'message': '请输入收件人邮箱地址' }) # 根据邮件类型生成内容 email_content = generate_email_content(email_type, recipient_name, custom_subject, custom_message) # 发送邮件 success = send_demo_email( recipient_email=recipient_email, subject=email_content['subject'], html_content=email_content['html_content'], text_content=email_content['text_content'] ) if success: return JsonResponse({ 'success': True, 'message': f'邮件已成功发送到 {recipient_email}' }) else: return JsonResponse({ 'success': False, 'message': '邮件发送失败,请检查邮件配置' }) except Exception as e: return JsonResponse({ 'success': False, 'message': f'发送失败:{str(e)}' }) def generate_email_content(email_type, recipient_name, custom_subject='', custom_message=''): """根据邮件类型生成邮件内容""" email_templates = { 'welcome': { 'subject': '🎉 欢迎加入 Hertz Server Django!', 'html_template': f'''
亲爱的 {recipient_name},
欢迎您注册成为我们的用户!我们很高兴您能加入我们的大家庭。
在这里,您可以享受到:
如果您有任何问题,请随时联系我们。
祝您使用愉快!
此致
Hertz Server Django 团队
亲爱的 {recipient_name},
您有一条新的系统通知:
您的账户设置已更新,如果这不是您的操作,请立即联系我们。
系统会持续为您提供安全保障,如有疑问请联系客服。
此致
Hertz Server Django 团队
亲爱的 {recipient_name},
感谢您注册 Hertz Server Django!请点击下面的按钮验证您的邮箱地址:
如果按钮无法点击,请复制以下链接到浏览器:
http://127.0.0.1:8000/verify?token=demo_token
如果您没有注册账户,请忽略此邮件。此验证链接将在24小时后失效。
此致
Hertz Server Django 团队
亲爱的 {recipient_name},
此致
Hertz Server Django 团队