CustomizedPushController.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # @Author : Rocky
  2. # @File : CustomizedPushController.py
  3. # @Time : 2023/10/18 16:31
  4. import logging
  5. import threading
  6. from django.http import HttpResponse
  7. from django.views import View
  8. from Model.models import CustomizedPush
  9. from Object.ResponseObject import ResponseObject
  10. from Service.CustomizedPushService import CustomizedPushObject
  11. CUSTOMIZED_PUSH_LOGGER = logging.getLogger('customized_push')
  12. class CustomizedPushView(View):
  13. def get(self, request, *args, **kwargs):
  14. request.encoding = 'utf-8'
  15. operation = kwargs.get('operation')
  16. return self.validation(request.GET, operation)
  17. def post(self, request, *args, **kwargs):
  18. request.encoding = 'utf-8'
  19. operation = kwargs.get('operation')
  20. return self.validation(request.POST, operation)
  21. def validation(self, request_dict, operation):
  22. response = ResponseObject()
  23. if operation == 'start': # 开始定制化推送
  24. return self.customized_push_start(request_dict, response)
  25. @staticmethod
  26. def customized_push_start(request_dict, response):
  27. customized_push_id = request_dict.get('customized_push_id', None)
  28. if not customized_push_id:
  29. return response.json(444)
  30. try:
  31. customized_push_qs = CustomizedPush.objects.filter(id=customized_push_id, push_satus=False).values(
  32. 'title', 'msg', 'link', 'icon_link', 'country', 'device_type', 'register_period', 'push_app')
  33. if not customized_push_qs.exists():
  34. return response.json(173)
  35. kwargs = {
  36. 'id': customized_push_id,
  37. 'title': customized_push_qs[0]['title'],
  38. 'msg': customized_push_qs[0]['msg'],
  39. 'link': customized_push_qs[0]['link'],
  40. 'icon_link': customized_push_qs[0]['icon_link'],
  41. 'country': customized_push_qs[0]['country'],
  42. 'device_type': customized_push_qs[0]['device_type'],
  43. 'register_period': customized_push_qs[0]['register_period'],
  44. 'push_app': customized_push_qs[0]['push_app']
  45. }
  46. # customized_push(**kwargs)
  47. # 异步推送消息和保存数据
  48. push_thread = threading.Thread(
  49. target=customized_push,
  50. kwargs=kwargs)
  51. push_thread.start()
  52. # 更新推送状态
  53. customized_push_qs.update(push_satus=True)
  54. return response.json(0)
  55. except Exception as e:
  56. return HttpResponse(repr(e), status=500)
  57. def customized_push(**kwargs):
  58. CUSTOMIZED_PUSH_LOGGER.info('customized_push_id:{}开始推送,kwargs:{}'.format(kwargs['id'], kwargs))
  59. user_id_list = CustomizedPushObject.query_push_user(kwargs['device_type'], kwargs['country'],
  60. kwargs['register_period'])
  61. kwargs['user_id_list'] = user_id_list
  62. CustomizedPushObject.push_and_save_sys_msg(**kwargs)