views.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. from django.core.mail import send_mail
  2. from django.http import JsonResponse
  3. from django.contrib.auth import authenticate, login, logout
  4. from django.views.decorators.csrf import csrf_exempt, ensure_csrf_cookie
  5. from .form import UserRegisterForm
  6. from .models import Account
  7. from django.contrib.auth.models import User
  8. @ensure_csrf_cookie
  9. def user_login(request):
  10. if request.method == 'POST':
  11. data = request.POST
  12. # 检测账号密码是否匹配数据库中的一个用户
  13. # 如果均匹配,则返回此User对象
  14. user = authenticate(username=data['username'], password=data['password'])
  15. if user:
  16. login(request, user)
  17. res = {
  18. 'code': '402',
  19. 'message': 'Login Successfully'
  20. }
  21. return JsonResponse(res)
  22. else:
  23. res = {
  24. 'code': '403',
  25. 'message': 'The Username Or Password Is Incorrect'
  26. }
  27. return JsonResponse(res)
  28. else:
  29. # 请求方法错误,请使用POST
  30. res = {
  31. 'code': '401',
  32. 'message': 'Please Use POST'
  33. }
  34. return JsonResponse(res)
  35. def user_is_logged(request):
  36. if request.user.is_authenticated:
  37. res = {
  38. 'code': '402',
  39. 'message': 'You\'ve Logged In'
  40. }
  41. return JsonResponse(res)
  42. else:
  43. res = {
  44. 'code': '401',
  45. 'message': 'You\'re Not Logged in'
  46. }
  47. return JsonResponse(res)
  48. @ensure_csrf_cookie
  49. def user_register(request):
  50. if request.method == 'POST':
  51. user_register_form = UserRegisterForm(data=request.POST)
  52. if user_register_form.is_valid():
  53. # 新建一个user,但是不提交
  54. new_user = user_register_form.save(commit=False)
  55. new_user.set_password(request.POST['password'])
  56. # 保存
  57. new_user.save()
  58. account = Account()
  59. account.user = new_user
  60. account.profile = '这个人很懒,没有写个人简介'
  61. account.save()
  62. res = {
  63. 'code': '402',
  64. 'message': 'Registered Successfully'
  65. }
  66. return JsonResponse(res)
  67. else:
  68. res = {
  69. 'code': '403',
  70. 'message': 'Data Formatting Error'
  71. }
  72. return JsonResponse(res)
  73. else:
  74. # 请求方法错误,请使用POST
  75. res = {
  76. 'code': '401',
  77. 'message': 'Please Use POST'
  78. }
  79. return JsonResponse(res)
  80. @ensure_csrf_cookie
  81. def user_logout(request):
  82. logout(request)
  83. res = {
  84. 'code': '402',
  85. 'message': 'Logout Successfully'
  86. }
  87. return JsonResponse(res)
  88. @csrf_exempt
  89. def user_check(request):
  90. if request.method == 'POST':
  91. user = User.objects.filter(username=request.POST['username'], email=request.POST['email']).get()
  92. if user:
  93. print('233ok1')
  94. send = send_mail('Shellmiao的博客——重置账号密码',
  95. user.password,
  96. 'shellmiao@shellmiao.com',
  97. [user.email])
  98. print('233ok2')
  99. if send == 1:
  100. res = {
  101. 'code': '402',
  102. 'message': 'Email Sent Successfully'
  103. }
  104. return JsonResponse(res)
  105. else:
  106. res = {
  107. 'code': '403',
  108. 'message': 'Failed To Send Mail'
  109. }
  110. return JsonResponse(res)
  111. else:
  112. # 请求方法错误,请使用POST
  113. res = {
  114. 'code': '401',
  115. 'message': 'Please Use POST'
  116. }
  117. return JsonResponse(res)
  118. @csrf_exempt
  119. def user_confirm(request):
  120. if request.method == 'POST':
  121. user = User.objects.filter(password=request.POST['code']).get()
  122. if user:
  123. res = {
  124. 'code': '402',
  125. 'message': 'Confirm Successfully'
  126. }
  127. return JsonResponse(res)
  128. else:
  129. res = {
  130. 'code': '403',
  131. 'message': 'Failed To Confirm'
  132. }
  133. return JsonResponse(res)
  134. else:
  135. # 请求方法错误,请使用POST
  136. res = {
  137. 'code': '401',
  138. 'message': 'Please Use POST'
  139. }
  140. return JsonResponse(res)
  141. @csrf_exempt
  142. def user_reset(request):
  143. if request.method == 'POST':
  144. user = User.objects.filter(password=request.POST['code']).get()
  145. if user:
  146. user.set_password(request.POST['password'])
  147. user.save()
  148. res = {
  149. 'code': '402',
  150. 'message': 'Reset Successfully'
  151. }
  152. return JsonResponse(res)
  153. else:
  154. res = {
  155. 'code': '403',
  156. 'message': 'Failed To Reset'
  157. }
  158. return JsonResponse(res)
  159. else:
  160. # 请求方法错误,请使用POST
  161. res = {
  162. 'code': '401',
  163. 'message': 'Please Use POST'
  164. }
  165. return JsonResponse(res)