views.py 7.1 KB

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