CustomizedPushController.py 3.0 KB

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