123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- from django.core.mail import send_mail
- from django.http import JsonResponse
- from django.contrib.auth import authenticate, login, logout
- from django.views.decorators.csrf import csrf_exempt, ensure_csrf_cookie
- from .form import UserRegisterForm
- from .models import Account
- from django.contrib.auth.models import User
- @ensure_csrf_cookie
- def user_login(request):
- if request.method == 'POST':
- data = request.POST
- # 检测账号密码是否匹配数据库中的一个用户
- # 如果均匹配,则返回此User对象
- user = authenticate(username=data['username'], password=data['password'])
- if user:
- login(request, user)
- res = {
- 'code': '402',
- 'message': 'Login Successfully'
- }
- return JsonResponse(res)
- else:
- res = {
- 'code': '403',
- 'message': 'The Username Or Password Is Incorrect'
- }
- return JsonResponse(res)
- else:
- # 请求方法错误,请使用POST
- res = {
- 'code': '401',
- 'message': 'Please Use POST'
- }
- return JsonResponse(res)
- def user_is_logged(request):
- if request.user.is_authenticated:
- res = {
- 'code': '402',
- 'message': 'You\'ve Logged In'
- }
- return JsonResponse(res)
- else:
- res = {
- 'code': '401',
- 'message': 'You\'re Not Logged in'
- }
- return JsonResponse(res)
- @ensure_csrf_cookie
- def user_register(request):
- if request.method == 'POST':
- user_register_form = UserRegisterForm(data=request.POST)
- if user_register_form.is_valid():
- # 新建一个user,但是不提交
- new_user = user_register_form.save(commit=False)
- new_user.set_password(request.POST['password'])
- # 保存
- new_user.save()
- account = Account()
- account.user = new_user
- account.profile = '这个人很懒,没有写个人简介'
- account.save()
- res = {
- 'code': '402',
- 'message': 'Registered Successfully'
- }
- return JsonResponse(res)
- else:
- res = {
- 'code': '403',
- 'message': 'Data Formatting Error'
- }
- return JsonResponse(res)
- else:
- # 请求方法错误,请使用POST
- res = {
- 'code': '401',
- 'message': 'Please Use POST'
- }
- return JsonResponse(res)
- @ensure_csrf_cookie
- def user_logout(request):
- logout(request)
- res = {
- 'code': '402',
- 'message': 'Logout Successfully'
- }
- return JsonResponse(res)
- @csrf_exempt
- def user_check(request):
- if request.method == 'POST':
- user = User.objects.filter(username=request.POST['username'], email=request.POST['email']).get()
- if user:
- print('233ok1')
- send = send_mail('Shellmiao的博客——重置账号密码',
- user.password,
- 'shellmiao@shellmiao.com',
- [user.email])
- print('233ok2')
- if send == 1:
- res = {
- 'code': '402',
- 'message': 'Email Sent Successfully'
- }
- return JsonResponse(res)
- else:
- res = {
- 'code': '403',
- 'message': 'Failed To Send Mail'
- }
- return JsonResponse(res)
- else:
- # 请求方法错误,请使用POST
- res = {
- 'code': '401',
- 'message': 'Please Use POST'
- }
- return JsonResponse(res)
- @csrf_exempt
- def user_confirm(request):
- if request.method == 'POST':
- user = User.objects.filter(password=request.POST['code']).get()
- if user:
- res = {
- 'code': '402',
- 'message': 'Confirm Successfully'
- }
- return JsonResponse(res)
- else:
- res = {
- 'code': '403',
- 'message': 'Failed To Confirm'
- }
- return JsonResponse(res)
- else:
- # 请求方法错误,请使用POST
- res = {
- 'code': '401',
- 'message': 'Please Use POST'
- }
- return JsonResponse(res)
- @csrf_exempt
- def user_reset(request):
- if request.method == 'POST':
- user = User.objects.filter(password=request.POST['code']).get()
- if user:
- user.set_password(request.POST['password'])
- user.save()
- res = {
- 'code': '402',
- 'message': 'Reset Successfully'
- }
- return JsonResponse(res)
- else:
- res = {
- 'code': '403',
- 'message': 'Failed To Reset'
- }
- return JsonResponse(res)
- else:
- # 请求方法错误,请使用POST
- res = {
- 'code': '401',
- 'message': 'Please Use POST'
- }
- return JsonResponse(res)
|