views.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import json
  2. from django.http import HttpResponse
  3. from django.views.decorators.csrf import csrf_exempt
  4. from .form import UserNameForm
  5. from django.contrib.auth.decorators import login_required
  6. from django.shortcuts import render
  7. from django.contrib.auth.models import User
  8. from .models import Friends, FriendRequest
  9. DEBUG = True
  10. # 批处理流程
  11. # def more_clean(request, form, html):
  12. # if request.method == 'POST':
  13. # user_login_form = form(request.POST)
  14. # if user_login_form.is_valid():
  15. # # 清洗出合法的数据
  16. # data = user_login_form.cleaned_data
  17. # return data
  18. # elif request.method == 'GET':
  19. # if DEBUG:
  20. # form = form()
  21. # context = {'form': form}
  22. # return render(request, html, context)
  23. #
  24. # else:
  25. # return HttpResponse(status=400)
  26. # 获取好友列表
  27. @csrf_exempt
  28. @login_required(login_url='/account/login/')
  29. def friends_list(request):
  30. if request.method == 'POST':
  31. # 查找双向好友关系
  32. friends = Friends.objects.filter(user__username=request.user.username)
  33. response = []
  34. for friend in friends:
  35. response.append(friend.friend.username)
  36. friends = Friends.objects.filter(friend__username=request.user.username)
  37. for friend in friends:
  38. response.append(friend.user.username)
  39. return HttpResponse(json.dumps(response), status=200)
  40. elif request.method == 'GET':
  41. if DEBUG:
  42. return render(request, 'get_user_list.html')
  43. else:
  44. return HttpResponse(status=400)
  45. # 添加好友(通过ws发送给被请求方信息,返回等待对方回应中)
  46. @csrf_exempt
  47. @login_required(login_url='/account/login/')
  48. def add_friend(request):
  49. if request.method == 'POST':
  50. user_name_form = UserNameForm(request.POST)
  51. if user_name_form.is_valid():
  52. data = user_name_form.cleaned_data
  53. # 查找是否有此人
  54. user = User.objects.filter(username=data['username'])
  55. if user:
  56. # 查找双向好友关系
  57. friend_1 = Friends.objects.filter(user__username=data['username'])
  58. friend_2 = Friends.objects.filter(friend__username=data['username'])
  59. if friend_1 or friend_2:
  60. # 二者已是好友关系
  61. return HttpResponse(status=421)
  62. else:
  63. friend_request = FriendRequest.objects.filter(requester__username=request.user.username)
  64. friend_request = friend_request.filter(receiver__username=data['username'])
  65. if friend_request:
  66. return HttpResponse(status=200)
  67. else:
  68. new_friend_request = FriendRequest()
  69. new_friend_request.requester = request.user
  70. new_friend_request.receiver = User.objects.filter(username=data['username']).get()
  71. new_friend_request.save()
  72. return HttpResponse(status=200)
  73. else:
  74. return HttpResponse(status=422)
  75. else:
  76. return HttpResponse(status=400)
  77. elif request.method == 'GET':
  78. if DEBUG:
  79. return render(request, 'add_friend.html')
  80. else:
  81. return HttpResponse(status=400)
  82. @csrf_exempt
  83. @login_required(login_url='/account/login/')
  84. def get_friend_request(request):
  85. # 查找目前有哪些请求
  86. friend_requests = FriendRequest.objects.filter(receiver__username=request.user.username)
  87. response = []
  88. for friend_request in friend_requests:
  89. response.append(friend_request.requester.username)
  90. return HttpResponse(json.dumps(response), status=200)
  91. # 由于ws不是很确定,先闲置
  92. # 同意添加好友,
  93. @login_required(login_url='/account/login/')
  94. def accept_friend(request):
  95. if request.method == "POST":
  96. user_name_form = UserNameForm(request.POST)
  97. if user_name_form.is_valid():
  98. data = user_name_form.cleaned_data
  99. friend_requests = FriendRequest.objects.filter(receiver__username=request.user.username)
  100. friend_requests = friend_requests.filter(requester__username=data['username'])
  101. if friend_requests:
  102. Friends.objects.create(user=User.objects.filter(username=data['username']).get(),
  103. friend=User.objects.filter(username=request.user.username).get(),
  104. status=1)
  105. for friend_request in friend_requests:
  106. friend_request.delete()
  107. return HttpResponse(status=200)
  108. else:
  109. return HttpResponse(status=400)
  110. else:
  111. return HttpResponse(status=400)
  112. elif request.method == 'GET':
  113. if DEBUG:
  114. return render(request, 'accept_friend.html')
  115. else:
  116. return HttpResponse(status=400)
  117. # 获取详细好友信息
  118. # @csrf_exempt
  119. # def friendDetail(request, pk):
  120. # data = more_clean(request, UserRequestForm)
  121. # try:
  122. # loginUserid = int(data["username"])
  123. # friendsTemp = friends.objects.get(whomfriend=loginUserid, userid=pk)
  124. #
  125. # except friends.DoesNotExist:
  126. # response = {'result': "do not exist"}
  127. # return HttpResponse(json.dumps(response))
  128. #
  129. # if len(friendsTemp):
  130. # response = {"data": friendsTemp, "result": "好友列表"}
  131. # return HttpResponse(json.dumps(response))
  132. # else:
  133. # response = {"result": "wrong"}
  134. # return HttpResponse(json.dumps(response))
  135. @csrf_exempt
  136. def delete_friend(request):
  137. if request.method == 'POST':
  138. user_name_form = UserNameForm(request.POST)
  139. if user_name_form.is_valid():
  140. data = user_name_form.cleaned_data
  141. try:
  142. temp_friends_1 = Friends.objects.filter(
  143. user=User.objects.filter(username=data["username"]).get(), friend=request.user)
  144. temp_friends_2 = Friends.objects.filter(
  145. friend=User.objects.filter(username=data["username"]).get(), user=request.user)
  146. except Friends.DoesNotExist:
  147. return HttpResponse(status=422)
  148. if not temp_friends_1 and not temp_friends_2:
  149. return HttpResponse(status=400)
  150. if temp_friends_1:
  151. for temp_friend in temp_friends_1:
  152. temp_friend.delete()
  153. if temp_friends_2:
  154. for temp_friend in temp_friends_2:
  155. temp_friend.delete()
  156. return HttpResponse(status=200)
  157. else:
  158. return HttpResponse(status=400)
  159. elif request.method == 'GET':
  160. if DEBUG:
  161. return render(request, 'delete_friend.html')