views.py 7.0 KB

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