views.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. from django.shortcuts import render
  2. from django.http import HttpResponse
  3. from django.views.decorators.csrf import csrf_exempt
  4. from django.contrib.auth.models import User
  5. from django.contrib.auth.decorators import login_required
  6. import json
  7. from friends.models import Friends
  8. from .form import StoreDataForm
  9. from .models import Message
  10. from django.http import JsonResponse
  11. from django.db.models import Q
  12. DEBUG = True
  13. # 批处理流程
  14. # def more_clean(request, form):
  15. # if request.method == 'POST':
  16. # user_login_form = form(request.POST)
  17. # if user_login_form.is_valid():
  18. # # 清洗出合法的数据
  19. # data = user_login_form.cleaned_data
  20. # return data
  21. # else:
  22. # response = {'result': 'illegal input'}
  23. # return HttpResponse(json.dumps(response))
  24. #
  25. # 存储消息到表中
  26. @csrf_exempt
  27. @login_required(login_url='/account/login/')
  28. def send_message(request):
  29. if request.method == 'POST':
  30. store_data_form = StoreDataForm(request.POST)
  31. if store_data_form.is_valid():
  32. data = store_data_form.cleaned_data
  33. user = User.objects.filter(username=data['toUsername'])
  34. if user:
  35. temp_friends_1 = Friends.objects.filter(
  36. user=User.objects.filter(username=data["toUsername"]).get(), friend=request.user)
  37. temp_friends_2 = Friends.objects.filter(
  38. friend=User.objects.filter(username=data["toUsername"]).get(), user=request.user)
  39. if temp_friends_1 or temp_friends_2:
  40. Message.objects.create(
  41. fromUser=request.user,
  42. toUser=user.get(),
  43. plaintext=data['plaintext'],
  44. )
  45. return HttpResponse(status=200)
  46. else:
  47. return HttpResponse(status=423)
  48. else:
  49. return HttpResponse(status=422)
  50. else:
  51. return HttpResponse(status=400)
  52. elif request.method == 'GET':
  53. if DEBUG:
  54. return render(request, 'send_message.html')
  55. else:
  56. return HttpResponse(status=400)
  57. # try:
  58. # Message.objects.create(
  59. # date=post_data["date"],
  60. # belongUserid=postData["belongUserid"],
  61. # fromUserid=postData["fromUserid"],
  62. # toUserid=postData["toUserid"],
  63. # EphemeralPub=postData["EphemeralPub"],
  64. # plaintext=postData["plaintext"],
  65. # EphemeralPri=postData["EphemeralPri"])
  66. # response = {"result": "添加成功"}
  67. # return JsonResponse(response)
  68. # except:
  69. # response = {"result": "添加失败!"}
  70. # return JsonResponse(response)
  71. # 适用于设备的漫游消息
  72. @csrf_exempt
  73. @login_required(login_url='/account/login/')
  74. def filter_messages(request):
  75. if request.method == 'POST':
  76. data = request.POST
  77. user = User.objects.filter(username=data['username'])
  78. if user:
  79. temp_friends_1 = Friends.objects.filter(
  80. user=User.objects.filter(username=data["toUsername"]).get(), friend=request.user)
  81. temp_friends_2 = Friends.objects.filter(
  82. friend=User.objects.filter(username=data["toUsername"]).get(), user=request.user)
  83. if temp_friends_1 or temp_friends_2:
  84. # to_user = User.objects.filter(username=data['username'])
  85. # messages = Message.objects.filter(
  86. # (Q(fromUser=to_user) & Q(toUser=request.user)) |
  87. # (Q(fromUser=request.user) & Q(toUser=to_user))
  88. # ).order_by("date")
  89. response = []
  90. messages = Message.objects.filter(fromUser__username=data['username'],
  91. toUser__username=request.user.username)
  92. for message in messages:
  93. message_dict = {'from_user_name': message.fromUser.username, 'plaintext': message.plaintext,
  94. 'to_user_name': request.user.username}
  95. response.append(message_dict)
  96. messages = Message.objects.filter(toUser__username=data['username'],
  97. fromUser__username=request.user.username)
  98. for message in messages:
  99. message_dict = {'from_user_name': message.fromUser.username, 'plaintext': message.plaintext,
  100. 'to_user_name': request.user.username}
  101. response.append(message_dict)
  102. return HttpResponse(json.dumps(response), status=200)
  103. else:
  104. return HttpResponse(status=423)
  105. else:
  106. return HttpResponse(status=422)
  107. elif request.method == 'GET':
  108. if DEBUG:
  109. return render(request, 'filter_message.html')
  110. else:
  111. return HttpResponse(status=400)
  112. # try:
  113. # loginUserid = data["fromUsername"]
  114. # messagesTemp = list(Message.objects.filter(
  115. #
  116. # (Q(fromUserid=pk) & Q(toUserid=loginUserid) & Q(belongUserid=loginUserid)) |
  117. # (Q(fromUserid=loginUserid) & Q(toUserid=pk) & Q(belongUserid=loginUserid))
  118. # ).order_by("date"))
  119. # response = {"result": "已发送", "data": messagesTemp}
  120. # return JsonResponse(response)
  121. # except Message.DoesNotExist:
  122. # response = {"result": "该用户不存在或者没有消息记录"}
  123. # return JsonResponse(response)
  124. # 发送信息
  125. @login_required(login_url='/account/login/')
  126. def request_for_message(request):
  127. messages = Message.objects.filter(toUser__username=request.user.username, is_received=False)
  128. response = []
  129. for message in messages:
  130. message_dict = {'from_user_name': message.fromUser.username, 'plaintext': message.plaintext,
  131. 'to_user_name': request.user.username, 'message_id': message.message_id}
  132. response.append(message_dict)
  133. return HttpResponse(json.dumps(response), status=200)
  134. # 成功接收消息
  135. @login_required(login_url='/account/login/')
  136. def receive_successfully(request):
  137. if request.method == 'POST':
  138. data = request.POST
  139. try:
  140. message = Message.objects.filter(message_id=data['message_id']).get()
  141. message.is_received = True
  142. message.save()
  143. return HttpResponse(status=200)
  144. except Message.DoesNotExist:
  145. return HttpResponse(status=423)
  146. elif request.method == 'GET':
  147. if DEBUG:
  148. return render(request, 'receive_successfully.html')
  149. else:
  150. return HttpResponse(status=400)